Hinzufügen von Kommentaren zu FunNGenerator und FunNUtilities.

This commit is contained in:
Etienne Zink 2022-04-03 10:29:06 +02:00
parent 24f1b507c4
commit 643f7c0220
2 changed files with 79 additions and 18 deletions

View File

@ -21,18 +21,19 @@ import java.util.stream.Stream;
import static org.objectweb.asm.Opcodes.*; import static org.objectweb.asm.Opcodes.*;
/** /**
* //ToDo beschreiben * Represents a Singleton-Service implementation for the {@link FunNUtilities} interface.
* *
* @since Studienarbeit Type Erasure * @since Studienarbeit Type Erasure
* @author etiennezink * @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 argumentGenericBase = "T";
private final String returnGeneric = "R"; 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 RefType objectRefType = new RefType(new JavaClassName(objectSuperType), null);
private final String objectSignature = applySignature(objectRefType); private final String objectSignature = applySignature(objectRefType);
private String applyDescriptor(RefTypeOrTPHOrWildcardOrGeneric a) { return a.acceptTV(new TypeToDescriptor(true)); } private FunNGenerator(){}
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)); }
@Override @Override
public byte[] generateSuperBytecode(int numberArguments) { public byte[] generateSuperBytecode(int numberArguments) {
@ -87,16 +86,17 @@ public class FunNGenerator implements FunNUtilities{
String genericSignature = "<"; String genericSignature = "<";
for (RefTypeOrTPHOrWildcardOrGeneric typeArgument : parameters) { for (RefTypeOrTPHOrWildcardOrGeneric typeArgument : parameters) {
//ToDo Etienne: Refactor
if (typeArgument instanceof GenericRefType){ if (typeArgument instanceof GenericRefType){
GenericRefType generic = (GenericRefType) typeArgument; GenericRefType generic = (GenericRefType) typeArgument;
if(genericSignature.contains(generic.getParsedName())) continue; String signatureOfArgument = generic.getParsedName();
genericSignature += String.format("%s:%s", generic.getParsedName(), applyDescriptor(generic)); if(genericSignature.contains(signatureOfArgument)) continue;
genericSignature += String.format("%s:%s", signatureOfArgument, applyDescriptor(generic));
containsGeneric = true; containsGeneric = true;
} else if(typeArgument instanceof TypePlaceholder){ } else if(typeArgument instanceof TypePlaceholder){
TypePlaceholder placeholder = (TypePlaceholder) typeArgument; TypePlaceholder placeholder = (TypePlaceholder) typeArgument;
if(genericSignature.contains(applySignature(placeholder).substring(1))) continue; String signatureOfArgument = applySignature(placeholder).substring(1);
genericSignature += String.format("%s:%s", applySignature(placeholder).substring(1), objectSignature); if(genericSignature.contains(signatureOfArgument)) continue;
genericSignature += String.format("%s:%s", signatureOfArgument, objectSignature);
containsGeneric = true; containsGeneric = true;
} }
} }
@ -142,8 +142,16 @@ public class FunNGenerator implements FunNUtilities{
@Override @Override
public RefTypeOrTPHOrWildcardOrGeneric getReturnType(List<RefTypeOrTPHOrWildcardOrGeneric> list) { public RefTypeOrTPHOrWildcardOrGeneric getReturnType(List<RefTypeOrTPHOrWildcardOrGeneric> list) {
if(list.size() == 0) if(list.size() == 0) return null;
throw new IndexOutOfBoundsException();
return list.get(list.size() - 1); 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)); }
} }

View File

@ -7,19 +7,74 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.util.List; 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 { public interface FunNUtilities {
/**
* @param numberArguments (excluding the return type!)
* @return the bytecode for the super FunN-interface
*/
byte[] generateSuperBytecode(int numberArguments); byte[] generateSuperBytecode(int numberArguments);
/**
* @param numberArguments (excluding the return type!)
* @return the name for the super FunN-interface
*/
String getSuperClassName(int numberArguments); String getSuperClassName(int numberArguments);
/**
* @param argumentTypes (excluding the return type!)
* @param returnType
* @return the bytecode for the specialized FunN-interface
*/
byte[] generateSpecializedBytecode(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType); byte[] generateSpecializedBytecode(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType);
/**
* @param argumentTypes (excluding the return type!)
* @param returnType
* @return the name for the specialized FunN-interface
*/
String getSpecializedClassName(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType); String getSpecializedClassName(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType);
/**
* @param argumentTypes (excluding the return type!)
* @param returnType
* @return the descriptor for a specialized FunN-interface.
*/
String getSpecializedDescriptor(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType); String getSpecializedDescriptor(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType);
/**
* @param argumentTypes (excluding the return type!)
* @param returnType
* @return the signature for a specialized FunN-interface.
*/
String getSpecializedSignature(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType); String getSpecializedSignature(List<RefTypeOrTPHOrWildcardOrGeneric> 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<RefTypeOrTPHOrWildcardOrGeneric> getArguments(List<RefTypeOrTPHOrWildcardOrGeneric> list); List<RefTypeOrTPHOrWildcardOrGeneric> getArguments(List<RefTypeOrTPHOrWildcardOrGeneric> list);
/**
* @param list containing type arguments and the return type.
* @return the return type of the {@code list} (last member)
*/
RefTypeOrTPHOrWildcardOrGeneric getReturnType(List<RefTypeOrTPHOrWildcardOrGeneric> list); RefTypeOrTPHOrWildcardOrGeneric getReturnType(List<RefTypeOrTPHOrWildcardOrGeneric> 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 @Deprecated
static boolean writeClassFile(String className, byte[] bytecode, File directory) { static boolean writeClassFile(String className, byte[] bytecode, File directory) {
try (FileOutputStream output = new FileOutputStream(new File(directory , className + CONSTANTS.EXTENSIONCLASS))){ try (FileOutputStream output = new FileOutputStream(new File(directory , className + CONSTANTS.EXTENSIONCLASS))){
@ -31,6 +86,4 @@ public interface FunNUtilities {
} }
return false; return false;
} }
} }