diff --git a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java index 3a651c93..d1a8340c 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNGenerator.java @@ -21,18 +21,19 @@ import java.util.stream.Stream; import static org.objectweb.asm.Opcodes.*; /** - * //ToDo beschreiben + * Represents a Singleton-Service implementation for the {@link FunNUtilities} interface. * * @since Studienarbeit Type Erasure * @author etiennezink */ -public class FunNGenerator implements FunNUtilities{ +public final class FunNGenerator implements FunNUtilities{ - private static FunNGenerator funNGenerator = new FunNGenerator(); + private static final FunNGenerator funNGenerator = new FunNGenerator(); - public static FunNGenerator getInstance(){ - return funNGenerator; - } + /** + * @return the Singleton instance for {@link FunNGenerator} + */ + public static FunNUtilities getInstance(){ return funNGenerator; } private final String argumentGenericBase = "T"; private final String returnGeneric = "R"; @@ -43,9 +44,7 @@ public class FunNGenerator implements FunNUtilities{ private final RefType objectRefType = new RefType(new JavaClassName(objectSuperType), null); private final String objectSignature = applySignature(objectRefType); - private String applyDescriptor(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToDescriptor(true)); } - private String applySignature(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToSignature(true)); } - private String applyNameDescriptor(RefTypeOrTPHOrWildcardOrGeneric a){ return a instanceof TypePlaceholder ? "LTPH;" : String.format("L%s;", applyDescriptor(a)); } + private FunNGenerator(){} @Override public byte[] generateSuperBytecode(int numberArguments) { @@ -87,16 +86,17 @@ public class FunNGenerator implements FunNUtilities{ String genericSignature = "<"; for (RefTypeOrTPHOrWildcardOrGeneric typeArgument : parameters) { - //ToDo Etienne: Refactor if (typeArgument instanceof GenericRefType){ GenericRefType generic = (GenericRefType) typeArgument; - if(genericSignature.contains(generic.getParsedName())) continue; - genericSignature += String.format("%s:%s", generic.getParsedName(), applyDescriptor(generic)); + String signatureOfArgument = generic.getParsedName(); + if(genericSignature.contains(signatureOfArgument)) continue; + genericSignature += String.format("%s:%s", signatureOfArgument, applyDescriptor(generic)); containsGeneric = true; } else if(typeArgument instanceof TypePlaceholder){ TypePlaceholder placeholder = (TypePlaceholder) typeArgument; - if(genericSignature.contains(applySignature(placeholder).substring(1))) continue; - genericSignature += String.format("%s:%s", applySignature(placeholder).substring(1), objectSignature); + String signatureOfArgument = applySignature(placeholder).substring(1); + if(genericSignature.contains(signatureOfArgument)) continue; + genericSignature += String.format("%s:%s", signatureOfArgument, objectSignature); containsGeneric = true; } } @@ -142,8 +142,16 @@ public class FunNGenerator implements FunNUtilities{ @Override public RefTypeOrTPHOrWildcardOrGeneric getReturnType(List list) { - if(list.size() == 0) - throw new IndexOutOfBoundsException(); + if(list.size() == 0) return null; return list.get(list.size() - 1); } + + private String applyDescriptor(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToDescriptor(true)); } + private String applySignature(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToSignature(true)); } + + /** + * @param a + * @return the name for the type {@code a} which should be used in the specialized name for FunN. + */ + private String applyNameDescriptor(RefTypeOrTPHOrWildcardOrGeneric a){ return a instanceof TypePlaceholder ? "LTPH;" : String.format("L%s;", applyDescriptor(a)); } } diff --git a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNUtilities.java b/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNUtilities.java index 039b4a14..b9921ba3 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNUtilities.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/funN/FunNUtilities.java @@ -7,19 +7,74 @@ import java.io.File; import java.io.FileOutputStream; import java.util.List; +/** + * Interface which represents the functionality for specifying and generating the specified functional types (FunN). + * + * @since Studienarbeit Type Erasure + * @author etiennezink + */ public interface FunNUtilities { + /** + * @param numberArguments (excluding the return type!) + * @return the bytecode for the super FunN-interface + */ byte[] generateSuperBytecode(int numberArguments); + + /** + * @param numberArguments (excluding the return type!) + * @return the name for the super FunN-interface + */ String getSuperClassName(int numberArguments); + /** + * @param argumentTypes (excluding the return type!) + * @param returnType + * @return the bytecode for the specialized FunN-interface + */ byte[] generateSpecializedBytecode(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType); + + /** + * @param argumentTypes (excluding the return type!) + * @param returnType + * @return the name for the specialized FunN-interface + */ String getSpecializedClassName(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType); + + /** + * @param argumentTypes (excluding the return type!) + * @param returnType + * @return the descriptor for a specialized FunN-interface. + */ String getSpecializedDescriptor(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType); + + /** + * @param argumentTypes (excluding the return type!) + * @param returnType + * @return the signature for a specialized FunN-interface. + */ String getSpecializedSignature(List argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType); + /** + * @param list containing type arguments and the return type. + * @return a {@link List} containing only the arguments of the specialized FunN-interface. + */ List getArguments(List list); + + /** + * @param list containing type arguments and the return type. + * @return the return type of the {@code list} (last member) + */ RefTypeOrTPHOrWildcardOrGeneric getReturnType(List list); + /** + * Should be refactored into a central API. + * + * @param className + * @param bytecode + * @param directory + * @return {@code true} iff the file could be generated and {@code false} if not + */ @Deprecated static boolean writeClassFile(String className, byte[] bytecode, File directory) { try (FileOutputStream output = new FileOutputStream(new File(directory , className + CONSTANTS.EXTENSIONCLASS))){ @@ -31,6 +86,4 @@ public interface FunNUtilities { } return false; } - - }