Compare commits

..

82 Commits

Author SHA1 Message Date
26ae463e96 Improve to SC generation 2021-08-30 18:37:14 +02:00
2e1e69df9a correct naming 2021-07-24 15:39:05 +02:00
0e8012c132 resolved the todo 2021-06-11 23:26:25 +02:00
68394565b5 correct typo 2021-06-02 11:00:52 +02:00
c3b17a8f2f Add git ignore 2021-05-27 12:16:18 +02:00
423cf8b9d2 Fix Object has null Constructor 2021-05-27 12:09:46 +02:00
87fb9e5a33 merge 2021-05-27 11:46:32 +02:00
df6debec4f Merge branch 'inferWildcards' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into inferWildcards 2021-05-26 21:20:51 +02:00
59177e09ad add 2021-05-26 21:19:51 +02:00
a9281a0db2 add filter for stuff in ast 2021-05-26 21:19:39 +02:00
fe36b34a80 Add tests for source code gen 2021-05-26 20:59:25 +02:00
d3a18887c3 ignore test output 2021-05-26 20:58:29 +02:00
14821575bd Add implementation of source code manipulation 2021-05-26 20:57:14 +02:00
c1519783ba Merge branch 'bigRefactoring' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into inferWildcards 2021-05-25 15:45:32 +02:00
53bbf3c511 Merge bigRefactoring 2021-05-25 15:45:23 +02:00
41492e53ca Modify Testcase for Unresolved Constraint Exception 2021-05-21 10:24:43 +02:00
74b9b024ee Reformat 2021-05-20 11:58:07 +02:00
97483714e7 Add example for lib function 2021-05-20 11:46:50 +02:00
a7b9c2a4ee Add generate BC Code Test 2021-05-10 18:10:19 +02:00
fd7628cb40 Simplify generateConstraints 2021-05-10 18:02:23 +02:00
76a291ab41 Fixed a Bug for a null Pointer Exception 2021-05-01 14:38:44 +02:00
c5c8ffeed1 Correct gernation of oder constraints 2021-04-27 17:54:31 +02:00
ba9335e2f6 Correct matcher 2021-04-27 17:54:10 +02:00
c098a0a1b0 extract duplicate code to a separate method 2021-04-25 11:34:25 +02:00
cfce2f55ac Add generation of wildcard constraints for generic types in source 2021-04-25 11:23:12 +02:00
4009a28333 add expected type infer results. Enables gernation of bytecode 2021-04-24 14:14:19 +02:00
f3dc0cbeb7 Add diamond expression 2021-04-16 19:34:49 +02:00
cd2f030ac6 remove unused testcase 2021-04-14 10:50:01 +02:00
f548548788 Add test case for param types 2021-04-14 10:48:40 +02:00
5cf41101bf Only replace refType occurs in MethodReturn, Parameter, Field and Local
Var
2021-04-14 10:18:56 +02:00
e139f8c867 Add Test for Void 2021-04-14 10:18:15 +02:00
5a151a965c Add new test classes 2021-04-13 11:57:10 +02:00
e5f03369cc Test via assert of amount of generated constraints 2021-04-13 09:14:17 +02:00
f698c967c0 Delete outdated test class 2021-04-10 11:24:02 +02:00
1509e21214 Add Test for MapNested 2021-04-10 11:23:34 +02:00
50c05064c7 Add Test for map 2021-04-10 11:22:38 +02:00
6458a06293 Add Test for nested 2021-04-10 11:21:49 +02:00
a984e455e4 Add Test for single 2021-04-10 11:20:36 +02:00
19c9f648a8 Add Testclass for fields 2021-04-10 11:19:15 +02:00
077bd62b92 Introduce ? extends to the map of TPH. This should prevent an editing 2021-04-10 11:09:16 +02:00
de62df00e0 correct test oputput 2021-04-10 10:42:58 +02:00
78e3b76dc3 make test functions static 2021-04-10 10:42:28 +02:00
c627f9de60 Split the single testcase to two 2021-04-10 10:40:58 +02:00
ff92807b83 Add JavaDoc 2021-04-10 10:33:46 +02:00
1043b3f55f remove a test 2021-04-09 20:31:40 +02:00
88440b873a extract generation and merge to a static utility function 2021-04-09 20:25:10 +02:00
4ca0e1e5f6 Add a child class of the compiler to implement the wildcard infer
algorithm
2021-04-09 20:13:15 +02:00
cb28405fe1 made foreach over map not entry set 2021-04-09 20:04:58 +02:00
c7f91724a6 remove rawtypes 2021-04-08 18:54:16 +02:00
11b63e0a5d remove raw types and add java doc 2021-04-08 18:52:33 +02:00
5f7829191c Improve log of test 2021-04-08 18:45:14 +02:00
044d9a1860 improve log of replacement of tph 2021-04-08 18:37:42 +02:00
6fcbca1187 remove useless imports 2021-04-08 18:31:14 +02:00
572f41ffd4 Add tests for different classes 2021-04-08 18:31:02 +02:00
e0f7f95bed remove unused test rsrc 2021-04-08 18:17:02 +02:00
78c365a7be Add testclass for single wildcard test 2021-04-08 18:16:52 +02:00
bea072689b Fix bug in test class 2021-04-08 18:08:54 +02:00
81697eee64 Add iteration and replacement of nested type parameters 2021-04-08 18:08:38 +02:00
c474abd2bd add test for nested types 2021-04-08 18:01:52 +02:00
6dd02a654b Add JavaDoc 2021-04-08 17:57:41 +02:00
e5d5376ce9 remove test case of the old approche 2021-04-08 11:30:49 +02:00
0cdeee9e0d add combination of constraints 2021-04-08 11:30:07 +02:00
98b3ad1517 add replacement of param types with tph and generate consolidated
constraints
2021-04-06 18:44:30 +02:00
b59aabeea5 generate the constraints for all tph 2021-04-06 18:38:01 +02:00
26953665c9 search for all generics in ast 2021-04-06 18:26:10 +02:00
465f82e967 Add generation of constraints for ref type 2021-04-06 18:25:04 +02:00
ce8b19acae Remove visitors for the Oracle AST approche 2021-04-06 18:24:47 +02:00
7dfe546999 return the source files 2021-04-06 18:24:05 +02:00
c96c56e882 add comments 2021-04-06 17:23:04 +02:00
3e57a64f9c add comment 2021-04-06 17:22:27 +02:00
e8539a84fd Add array constructor 2021-04-04 15:40:16 +02:00
0551d25fbf Add test for JavaTX constraints generation 2021-04-04 15:39:57 +02:00
cbe2d7b0f5 extract test of unit 2021-03-27 18:06:28 +01:00
59b45b5f87 Add test method 2021-03-27 18:04:28 +01:00
ad7e7ec42b Add the placeholder mapping 2021-03-27 18:01:28 +01:00
9d2c85d686 add get of constraints 2021-03-27 17:40:01 +01:00
63b4dbcc10 correct test methods visibility 2021-03-27 17:39:42 +01:00
ca816fba85 Add JavaCompiler parsing 2021-03-27 17:29:37 +01:00
54a2dbfedc add simple test 2021-03-22 17:47:35 +01:00
07c1eeeb36 Recompile parser with ANTLR v4.8 2021-03-22 14:52:54 +01:00
69a557f1f2 Fehlerhaften code entfernen 2021-03-18 11:39:07 +01:00
88175f8b48 modified: src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java
modified:   src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java
	modified:   src/test/java/insertGenerics/TestExample42.java
2021-01-08 15:44:07 +01:00
87 changed files with 1805 additions and 1213 deletions
.gitignorepom.xml
src
main
test

1
.gitignore vendored

@ -29,3 +29,4 @@ logFiles/**
src/main/java/de/dhbwstuttgart/parser/antlr/
src/main/java/de/dhbwstuttgart/sat/asp/parser/antlr/
/pull.sh

16
pom.xml

@ -118,15 +118,6 @@ http://maven.apache.org/maven-v4_0_0.xsd">
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
@ -138,11 +129,8 @@ http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<compilerArgs>
--enable-preview
</compilerArgs>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<tycho.version>0.23.0</tycho.version>
<mainClass>de.dhbwstuttgart.core.ConsoleInterface</mainClass>
</properties>

@ -0,0 +1,37 @@
grammar UnifyResult;
answer : 'ANSWER' (resultSetRule '.')*;
resultSetRule :
parameter
| equals
| smaller
| typeVar
| type
| otherRule
;
parameterList : '(' value (',' value)* ')';
value : NAME
| resultSetRule ;
parameter : PARAMLIST_NAME parameterList;
equals : EQUALS_NAME parameterList;
smaller : SMALLER_NAME parameterList;
typeVar : TYPEVAR_NAME parameterList;
type : TYPE_NAME parameterList;
otherRule : NAME parameterList;
//TODO: Es sollte Regeln für das Result set geben, welche sich nicht mit den anderen überdecken, dann auch nur diese im Result ausgeben
PARAMLIST_NAME : 'param';
EQUALS_NAME : 'equals';
SMALLER_NAME : 'smaller';
TYPEVAR_NAME : 'typeVar';
TYPE_NAME : 'type';
NAME : [a-zA-Z0-9_]+;
WS : [ \t\r\n\u000C]+ -> skip
;
LINE_COMMENT
: '%' ~[\r\n]* -> skip
;

@ -0,0 +1,23 @@
T__0=1
T__1=2
T__2=3
T__3=4
T__4=5
PARAMLIST_NAME=6
EQUALS_NAME=7
SMALLER_NAME=8
TYPEVAR_NAME=9
TYPE_NAME=10
NAME=11
WS=12
LINE_COMMENT=13
'ANSWER'=1
'.'=2
'('=3
','=4
')'=5
'param'=6
'equals'=7
'smaller'=8
'typeVar'=9
'type'=10

@ -0,0 +1,23 @@
T__0=1
T__1=2
T__2=3
T__3=4
T__4=5
PARAMLIST_NAME=6
EQUALS_NAME=7
SMALLER_NAME=8
TYPEVAR_NAME=9
TYPE_NAME=10
NAME=11
WS=12
LINE_COMMENT=13
'ANSWER'=1
'.'=2
'('=3
','=4
')'=5
'param'=6
'equals'=7
'smaller'=8
'typeVar'=9
'type'=10

@ -13,10 +13,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import de.dhbwstuttgart.bytecode.funN.FunNGenerator;
import de.dhbwstuttgart.bytecode.funN.FunNUtilities;
import de.dhbwstuttgart.bytecode.utilities.*;
import de.dhbwstuttgart.environment.DirectoryClassLoader;
import de.dhbwstuttgart.exceptions.NotImplementedException;
@ -592,21 +589,10 @@ public class BytecodeGenMethod implements StatementVisitor {
this.lamCounter++;
String typeErasure = createDescriptorWithTypeErasure(lambdaExpression);
//ToDo Etienne: Double Check
RefTypeOrTPHOrWildcardOrGeneric returnType = resolver.resolve(lambdaExpression.getReturnType());
List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes = lambdaExpression
.params
.getFormalparalist()
.stream()
.map(FormalParameter::getType)
.map(resolver::resolve)
.collect(Collectors.toList());
FunNUtilities funNUtilities = FunNGenerator.getInstance();
FunNUtilities.writeClassFile(funNUtilities.getSuperClassName(argumentTypes.size()),
funNUtilities.generateSuperBytecode(argumentTypes.size()), path);
FunNUtilities.writeClassFile(funNUtilities.getSpecializedClassName(argumentTypes, returnType),
funNUtilities.generateSpecializedBytecode(argumentTypes, returnType), path);
ByteCodeForFunNGenerator.generateBCForFunN(lambdaExpression, typeErasure,path);
Lambda lam = new Lambda(lambdaExpression);
String lamDesc = lam.accept(new DescriptorToString(resultSet));
// Call site, which, when invoked, returns an instance of the functional
@ -828,20 +814,8 @@ public class BytecodeGenMethod implements StatementVisitor {
} else if(!helper.isInCurrPkg(clazz)){
if(clazz.contains(CONSTANTS.$$)) {
mDesc = helper.getDescriptorOfApplyMethod(methCallType);
//ToDo Etienne: Double Check
RefTypeOrTPHOrWildcardOrGeneric returnType = resolver.resolve(methodCall.getType());
List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes = methodCall
.arglist
.getArguments()
.stream()
.map(TypableStatement::getType)
.map(resolver::resolve)
.collect(Collectors.toList());
FunNUtilities funNUtilities = FunNGenerator.getInstance();
FunNUtilities.writeClassFile(funNUtilities.getSuperClassName(argumentTypes.size()),
funNUtilities.generateSuperBytecode(argumentTypes.size()), path);
FunNUtilities.writeClassFile(funNUtilities.getSpecializedClassName(argumentTypes, returnType),
funNUtilities.generateSpecializedBytecode(argumentTypes, returnType), path);
helper.generateBCForFunN(mDesc);
// mDesc = helper.generateBCForFunN(methCallType,typesOfParams);
}else {
try {
cLoader2 = new DirectoryClassLoader(path, classLoader);

@ -153,7 +153,6 @@ public class DescriptorToString implements DescriptorVisitor, CONSTANTS {
return desc;
}
//ToDo Etienne: ändern
@Override
public String visit(Lambda lambdaExpression) {
String desc = "(";

@ -1,24 +1,17 @@
package de.dhbwstuttgart.bytecode.descriptor;
import de.dhbwstuttgart.bytecode.funN.FunNGenerator;
import de.dhbwstuttgart.bytecode.funN.FunNUtilities;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.type.TypeVisitor;
public class TypeToDescriptor implements TypeVisitor<String>{
private final boolean specializedFunN;
public TypeToDescriptor(){ this(true); }
public TypeToDescriptor(boolean specializedFunN) { this.specializedFunN = specializedFunN; }
@Override
public String visit(RefType refType) {
if (refType.getName().toString().matches("Fun\\d+\\$\\$") && specializedFunN) {
FunNUtilities funNUtilities = FunNGenerator.getInstance();
return funNUtilities.getSpecializedDescriptor(funNUtilities.getArguments(refType.getParaList()), funNUtilities.getReturnType(refType.getParaList()));
}
return refType.getName().toString().replace(".", "/");
// String t = refType.getName().toString().replace(".", "/");
// return t.equals("Fun1")?(t+"$$"):t;
@ -45,8 +38,6 @@ public class TypeToDescriptor implements TypeVisitor<String>{
//throw new NotImplementedException();
}
//ToDo Etienne: bin der Meinung hier müsste immer der Descriptor der extends-Schranke sein, z.b. <T> => Ljava/lang/Object; oder <T extends String> => Ljava/lang/String;
//Tests sind aber damit grün?! => Fehler in Tests schon vorher aufgefallen und gut möglich!
@Override
public String visit(GenericRefType genericRefType) {
return genericRefType.getParsedName().replace(".", "/");

@ -1,181 +0,0 @@
package de.dhbwstuttgart.bytecode.funN;
import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor;
import de.dhbwstuttgart.bytecode.signature.TypeToSignature;
import de.dhbwstuttgart.bytecode.utilities.CONSTANTS;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.objectweb.asm.Opcodes.*;
/**
* Represents a Singleton-Service implementation for the {@link FunNUtilities} interface.
*
* @since Studienarbeit Type Erasure
* @author etiennezink
*/
public final class FunNGenerator implements FunNUtilities{
private static final FunNGenerator funNGenerator = new FunNGenerator();
/**
* @return the Singleton instance for {@link FunNGenerator}
*/
public static FunNUtilities getInstance(){ return funNGenerator; }
private final String argumentGenericBase = "T";
private final String returnGeneric = "R";
private final String methodName = "apply";
private final int bytecodeVersion = V1_8;
private final String objectSuperType = Type.getInternalName(Object.class).replace('.','/');
private final RefType objectRefType = new RefType(new JavaClassName(objectSuperType), null);
private final String objectSignature = applySignature(objectRefType);
private FunNGenerator(){}
@Override
public byte[] generateSuperBytecode(int numberArguments) {
StringBuilder superFunNClassSignature = new StringBuilder("<");
StringBuilder superFunNMethodSignature = new StringBuilder("(");
StringBuilder superFunNMethodDescriptor = new StringBuilder("(");
for (int currentParameter = 1; currentParameter <= numberArguments; currentParameter++){
superFunNClassSignature.append(String.format("%s%d:%s",argumentGenericBase, currentParameter, objectSignature));
superFunNMethodSignature.append(applySignature( new GenericRefType(argumentGenericBase + currentParameter, null)));
superFunNMethodDescriptor.append(objectSignature);
}
superFunNClassSignature.append(String.format("%s:%s>%s", returnGeneric, objectSignature, objectSignature));
superFunNMethodSignature.append(String.format(")%s", applySignature(new GenericRefType(returnGeneric, null))));
superFunNMethodDescriptor.append(String.format(")%s", objectSignature));
ClassWriter classWriter = new ClassWriter(0);
MethodVisitor methodVisitor;
classWriter.visit(bytecodeVersion, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, getSuperClassName(numberArguments), superFunNClassSignature.toString(), objectSuperType, null);
methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, methodName, superFunNMethodDescriptor.toString(), superFunNMethodSignature.toString(), null);
methodVisitor.visitEnd();
classWriter.visitEnd();
return classWriter.toByteArray();
}
@Override
public String getSuperClassName(int numberArguments) {
return String.format("Fun%d$$", numberArguments);
}
@Override
public byte[] generateSpecializedBytecode(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType) {
Objects.requireNonNull(argumentTypes);
Objects.requireNonNull(returnType);
//generates a list of all params and substitutes the TPH
List<RefTypeOrTPHOrWildcardOrGeneric> parameters = Stream
.concat(argumentTypes.stream(), Stream.of(returnType))
.map(this::substituteTPH)
.collect(Collectors.toList());
RefType superFunN = new RefType(new JavaClassName(getSuperClassName(argumentTypes.size())), parameters , null);
StringBuilder funNClassSignature = new StringBuilder(objectSignature + (superFunN.acceptTV(new TypeToSignature(false))));
boolean containsGeneric = false;
String genericSignature = "<";
for (RefTypeOrTPHOrWildcardOrGeneric typeArgument : parameters) {
if (typeArgument instanceof GenericRefType){
GenericRefType generic = (GenericRefType) typeArgument;
String signatureOfArgument = generic.getParsedName();
if(genericSignature.contains(signatureOfArgument)) continue;
genericSignature += String.format("%s:%s", signatureOfArgument, objectSignature);
containsGeneric = true;
}
}
genericSignature += ">";
if (containsGeneric) funNClassSignature.insert(0, genericSignature);
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(bytecodeVersion, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, getSpecializedClassName(argumentTypes, returnType), funNClassSignature.toString(), objectSuperType, new String[]{getSuperClassName(argumentTypes.size())});
classWriter.visitEnd();
return classWriter.toByteArray();
}
@Override
public String getSpecializedClassName(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType) {
Objects.requireNonNull(argumentTypes);
Objects.requireNonNull(returnType);
return String.format("Fun%d$$%s%s",
argumentTypes.size(),
argumentTypes
.stream()
.map(this::applyNameDescriptor)
.collect(Collectors.joining()),
applyNameDescriptor(returnType))
.replace('/', '$')
.replace(";", "$_$");
}
@Override
public String getSpecializedDescriptor(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType) {
Objects.requireNonNull(argumentTypes);
Objects.requireNonNull(returnType);
return applyDescriptor(getSpecializedFunNRefType(argumentTypes, returnType));
}
@Override
public String getSpecializedSignature(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType) {
Objects.requireNonNull(argumentTypes);
Objects.requireNonNull(returnType);
return applySignature(getSpecializedFunNRefType(argumentTypes, returnType));
}
@Override
public List<RefTypeOrTPHOrWildcardOrGeneric> getArguments(List<RefTypeOrTPHOrWildcardOrGeneric> list) {
Objects.requireNonNull(list);
return list
.stream()
.limit(Math.max(0, list.size() - 1))
.collect(Collectors.toList());
}
@Override
public RefTypeOrTPHOrWildcardOrGeneric getReturnType(List<RefTypeOrTPHOrWildcardOrGeneric> list) {
Objects.requireNonNull(list);
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)); }
private RefTypeOrTPHOrWildcardOrGeneric substituteTPH(RefTypeOrTPHOrWildcardOrGeneric t) {
if (t instanceof TypePlaceholder) {
TypePlaceholder tph = (TypePlaceholder) t;
return new GenericRefType(tph.getName()+"$", t.getOffset());
}
return t;
}
private RefType getSpecializedFunNRefType(List<RefTypeOrTPHOrWildcardOrGeneric> argumentTypes, RefTypeOrTPHOrWildcardOrGeneric returnType){
return new RefType(new JavaClassName(getSpecializedClassName(argumentTypes, returnType)),
Stream
.concat(argumentTypes.stream(), Stream.of(returnType))
.filter(t -> t instanceof GenericRefType ||
t instanceof TypePlaceholder)
.collect(Collectors.toList()),null);
}
}

@ -1,90 +0,0 @@
package de.dhbwstuttgart.bytecode.funN;
import de.dhbwstuttgart.bytecode.utilities.CONSTANTS;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import javax.annotation.Nonnull;
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<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);
/**
* @param argumentTypes (excluding the return type!)
* @param returnType
* @return the descriptor for a specialized FunN-interface.
*/
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);
/**
* @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);
/**
* @param list containing type arguments and the return type.
* @return the return type of the {@code list} (last member)
*/
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
static boolean writeClassFile(String className, byte[] bytecode, File directory) {
try (FileOutputStream output = new FileOutputStream(new File(directory , className + CONSTANTS.EXTENSIONCLASS))){
output.write(bytecode);
output.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}

@ -5,8 +5,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import de.dhbwstuttgart.bytecode.funN.FunNGenerator;
import de.dhbwstuttgart.bytecode.funN.FunNUtilities;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
@ -19,29 +17,18 @@ import de.dhbwstuttgart.syntaxtree.type.TypeVisitor;
public class TypeToSignature implements TypeVisitor<String> {
private List<GenericsGeneratorResult> constraints;
private final boolean specializedFunN;
public TypeToSignature() { this(new ArrayList<>(), true); }
public TypeToSignature(boolean specializedFunN) { this(new ArrayList<>(), specializedFunN); }
public TypeToSignature(List<GenericsGeneratorResult> constraints) {
this(constraints, true);
public TypeToSignature() {
this.constraints = new ArrayList<>();
}
public TypeToSignature(List<GenericsGeneratorResult> constraints, boolean specializedFunN){
public TypeToSignature(List<GenericsGeneratorResult> constraints) {
this.constraints = constraints;
this.specializedFunN = specializedFunN;
}
@Override
public String visit(RefType refType) {
if(refType.getName().toString().equals("void"))
return "V";
if (refType.getName().toString().matches("Fun\\d+\\$\\$") && specializedFunN){
FunNUtilities funNUtilities = FunNGenerator.getInstance();
return funNUtilities.getSpecializedSignature(funNUtilities.getArguments(refType.getParaList()), funNUtilities.getReturnType(refType.getParaList()));
}
// return refType.toString().replace(".", "/");
String params = "";
if(refType.getParaList().size()>0){
@ -102,20 +89,9 @@ public class TypeToSignature implements TypeVisitor<String> {
return sig;
}
/**
* Changed that the correct signature is returned:
* returns now T...; expect of only ...
* where ... is {@code genericRefType.getParsedName()}
*
* @since Studienarbeit Type Erasure
* @author etiennezink
*
* @param genericRefType
* @return
*/
@Override
public String visit(GenericRefType genericRefType) {
return String.format("T%s;", genericRefType.getParsedName()).replace(".", "/");
return genericRefType.getParsedName().replace(".", "/");
}
private Optional<GenericsGeneratorResult> getEqualTPHFromClassConstraints(List<GenericsGeneratorResult> listOfConstraints, String tph) {

@ -25,8 +25,4 @@ public class Resolver {
public String getResolvedType(RefTypeOrTPHOrWildcardOrGeneric type) {
return resultSet.resolveType(type).resolvedType.acceptTV(new TypeToDescriptor());
}
public RefTypeOrTPHOrWildcardOrGeneric resolve(RefTypeOrTPHOrWildcardOrGeneric type) {
return resultSet.resolveType(type).resolvedType;
}
}

@ -92,6 +92,9 @@ public class JavaTXCompiler {
this(sourceFile);
this.log = log;
}
public JavaTXCompiler(File[] sourceFiles) throws IOException, ClassNotFoundException {
this(Arrays.asList(sourceFiles), null);
}
public JavaTXCompiler(List<File> sourceFiles) throws IOException, ClassNotFoundException {
this(sourceFiles, null);
}
@ -108,7 +111,8 @@ public class JavaTXCompiler {
//INSTANCE = this;
}
public ConstraintSet<Pair> getConstraints() throws ClassNotFoundException, IOException {
public ConstraintSet<Pair> getConstraints() throws ClassNotFoundException, IOException {
List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
List<ClassOrInterface> importedClasses = new ArrayList<>();
ClassOrInterface objectClass = ASTFactory.createClass(

@ -0,0 +1,132 @@
package de.dhbwstuttgart.inferWildcards;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
/**
* Utilities to generate the constraints for the infer wildcards algorithm.
*
* @author Till Schnell
* @version 1.0
*/
public final class ConstraintsGenerationUtils
{
private ConstraintsGenerationUtils () {
throw new AssertionError("No ConstraintsGenerationUtils instance for you");
}
/**
* Generate the constraints for a map of type placeholder and RefType and merge
* these to the already provided constraints.
*
* @param tphMap {@link Map} of {@link TypePlaceholder} and
* {@link RefTypeOrTPHOrWildcardOrGeneric}
* @param constraints {@link ConstraintSet} over {@link Pair} to merge to
* @return The same instance {@code constraints} provided including the merged
* constraints
*/
public static ConstraintSet<Pair> generateAndMergeConstraints (
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap,
ConstraintSet<Pair> constraints) {
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
constraints.addAll(generateConstraints);
return constraints;
}
/**
* Generate the constraints for a map of type placeholder and RefType.
*
* @param tphMap {@link Map} of {@link TypePlaceholder} and
* {@link RefTypeOrTPHOrWildcardOrGeneric}
* @return {@link ConstraintSet} of {@link Pair} containing the constraints to
* infer the matching wildcard type.
*/
public static ConstraintSet<Pair> generateConstraints (
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap) {
return tphMap.entrySet().stream().map(ConstraintsGenerationUtils::generateConstraints)
.reduce(
new ConstraintSet<>(),
(cs, c) -> {
cs.addAll(c);
return cs;
});
}
/**
* Generate the constraints for a single RefType type placeholder pair to infer
* the wildcards for the generic parameter type.
*
* @param tphEntry {@link Entry} of {@link TypePlaceholder} and
* {@link RefTypeOrTPHOrWildcardOrGeneric}
* @return {@link ConstraintSet} of {@link Pair} generated containing the
* constraints.
*/
private static ConstraintSet<Pair> generateConstraints (
Map.Entry<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphEntry) {
return generateConstraints(tphEntry.getValue(), tphEntry.getKey());
}
/**
* Generate the constraints for a single RefType type placeholder pair to infer
* the wildcards for the generic parameter type.
*
* @param refType {@link RefTypeOrTPHOrWildcardOrGeneric}
* @param tph {@link TypePlaceholder}
* @return {@link ConstraintSet} of {@link Pair} generated containing the
* constraints.
*/
public static ConstraintSet<Pair> generateConstraints (RefTypeOrTPHOrWildcardOrGeneric refType,
TypePlaceholder tph) {
ConstraintSet<Pair> constraintSet = new ConstraintSet<>();
Set<Constraint<Pair>> oderConstraints = new HashSet<>();
constraintSet.addOderConstraint(oderConstraints);
// single type
ConstraintsGenerationUtils.addToOderConstraint(oderConstraints, tph, refType);
// extends type
ExtendsWildcardType extendsWildcardType = new ExtendsWildcardType(refType, new NullToken());
ConstraintsGenerationUtils.addToOderConstraint(oderConstraints, tph, extendsWildcardType);
// super type
SuperWildcardType superWildcardType = new SuperWildcardType(refType, new NullToken());
ConstraintsGenerationUtils.addToOderConstraint(oderConstraints, tph, superWildcardType);
return constraintSet;
}
private static void addToOderConstraint (Set<Constraint<Pair>> oderConstraints, TypePlaceholder tph,
RefTypeOrTPHOrWildcardOrGeneric refType) {
Constraint<Pair> c = new Constraint<>();
oderConstraints.add(c);
ConstraintsGenerationUtils.addToConstraint(c, tph, refType);
}
/**
* Generate a pair and adds it to a constraint.
*
* @param c {@link Constraint} of {@link Pair}
* @param tph {@link TypePlaceholder}
* @param type {@link RefTypeOrTPHOrWildcardOrGeneric}
*/
private static void addToConstraint (Constraint<Pair> c, TypePlaceholder tph,
RefTypeOrTPHOrWildcardOrGeneric type) {
Pair pair = new Pair(tph, type, PairOperator.EQUALSDOT);
c.add(pair);
}
}

@ -0,0 +1,40 @@
package de.dhbwstuttgart.inferWildcards;
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class FindInAstVisitor
{
public static boolean find (SourceFile file, TypePlaceholder tph) {
FindInAstVisitor findInAstVisitor = new FindInAstVisitor(tph);
return findInAstVisitor.find(file);
}
private boolean find;
private TypePlaceholder tph;
public FindInAstVisitor (TypePlaceholder tph) {
this.tph = tph;
find = false;
}
public boolean find (SourceFile file) {
CostumASTWalker walker = new CostumASTWalker();
walker.visit(file);
return find;
}
private class CostumASTWalker extends AbstractASTWalker{
@Override
public void visit (TypePlaceholder typePlaceholder) {
if (typePlaceholder.equals(tph))
find = true;
else
super.visit(typePlaceholder);
}
}
}

@ -0,0 +1,209 @@
package de.dhbwstuttgart.inferWildcards;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.ResolvedType;
import de.dhbwstuttgart.typeinference.result.ResultSet;
/**
* Extension of the JavaTX Compiler providing the capabilities to inference
* wildcard types.
* <p>
* <code>
* <ul>
* <li>? super Type</li>
* <li>? extends Type</li>
* <li>Type</li>
* </ul>
* </code>
*
* @author Till Schnell
* @version 1.0
*/
public class JavaTXCompilerWildcards
extends JavaTXCompiler
{
/**
* Generated Type placeholder and the implementation type represented.
*/
private final Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap;
public JavaTXCompilerWildcards (File... sourceFile) throws IOException, ClassNotFoundException {
super(Arrays.asList(sourceFile));
this.tphMap = TypePlaceholderReplaceUtils.generateTypePlaceholder(this);
}
public JavaTXCompilerWildcards (File sourceFile, Boolean log) throws IOException, ClassNotFoundException {
super(sourceFile, log);
this.tphMap = TypePlaceholderReplaceUtils.generateTypePlaceholder(this);
}
public JavaTXCompilerWildcards (List<File> sourceFiles) throws IOException, ClassNotFoundException {
super(sourceFiles);
this.tphMap = TypePlaceholderReplaceUtils.generateTypePlaceholder(this);
}
public JavaTXCompilerWildcards (List<File> sources, List<File> contextPath)
throws IOException, ClassNotFoundException {
super(sources, contextPath);
this.tphMap = TypePlaceholderReplaceUtils.generateTypePlaceholder(this);
}
/**
* Return the Type Placeholder generated.
*
* @return {@link Map} over {@link TypePlaceholder} and
* {@link RefTypeOrTPHOrWildcardOrGeneric}
*/
public Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> getTphMap () {
return tphMap;
}
@Override
public ConstraintSet<Pair> getConstraints () throws ClassNotFoundException, IOException {
ConstraintSet<Pair> constraints = super.getConstraints();
return ConstraintsGenerationUtils.generateAndMergeConstraints(tphMap, constraints);
}
/**
* Generate the source code for manipulated Java Generic Types to a new .java
* file.
*
* @param outputDir {@link File}
* @throws IOException if an i/o exception during file reading and
* writing occurs
* @throws ClassNotFoundException see {@link #typeInference()}
*/
public void generateSourceCode (File outputDir) throws IOException, ClassNotFoundException {
if (!outputDir.exists()) {
boolean mkdirs = outputDir.mkdirs();
if (!mkdirs)
throw new IOException("Could not create output directory at: " + outputDir.getAbsolutePath());
}
List<ResultSet> typeInference = typeInference();
if (typeInference.isEmpty())
return;
ResultSet resultSet = typeInference.get(0); // Use the first available result sets
for (Map.Entry<File, SourceFile> e : sourceFiles.entrySet()) {
List<Map.Entry<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric>> list = tphMap.entrySet().stream().filter(d -> FindInAstVisitor.find(e.getValue(), d.getKey())).sorted( (c1, c2) -> Integer.compare(c1.getValue().getOffset().getStartIndex(),
c2.getValue().getOffset().getStartIndex())).collect(Collectors.toList());
generateSourceCode(e.getKey(), outputDir, list, resultSet);
}
}
private void generateSourceCode (File inputFile, File outputDir,
List<Map.Entry<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric>> tphs,
ResultSet results)
throws IOException {
File outputFile = new File(outputDir, inputFile.getName());
Files.deleteIfExists(outputFile.toPath());
if(!outputFile.createNewFile())
throw new IOException("File could not be created at: " + outputFile.getAbsolutePath());
try(BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))){
int readIdx = 0;
for (Map.Entry<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> e : tphs) {
Token token = e.getValue().getOffset();
// read the characters before the token
int startIndex = token.getStartIndex();
char[] read = new char[startIndex - readIdx];
int i = reader.read(read);
if (i != read.length)
throw new IOException("Could not read the assumed number of character, read " + i
+ " assumed " + read.length);
readIdx += read.length;
// Write to output file
writer.write(read);
// Write the new type
ResolvedType resolveType = results.resolveType(e.getKey());
String string = resolveType.resolvedType.toString();
writer.write(string);
// Read the replaced type and count the read counter
int length = token.getStopIndex() - token.getStartIndex() + 1;
reader.read(new char[length]);
readIdx += length;
// Read the replaced nested type if the result was a nested type
Pattern pattern = Pattern.compile("<.*>");
Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
// Read the first and check if it will be a Java Generic Expression starting
// with <
char c = (char) reader.read();
readIdx++;
if (c != '<')
throw new IllegalStateException("At this position is a < expected, found " + c);
// Read and forget all content until a > on the same level
int opens = 1;
while (opens > 0) {
c = (char) reader.read();
readIdx++;
switch (c)
{
case '<':
opens++;
break;
case '>':
opens--;
break;
}
}
}
}
// Read the rest of the file.
while (true) {
char[] tmp = new char[1024];
int read = reader.read(tmp);
if (read == -1)
break;
writer.write(tmp, 0, read);
}
}
}
}

@ -0,0 +1,171 @@
package de.dhbwstuttgart.inferWildcards;
import java.util.HashMap;
import java.util.ListIterator;
import java.util.Map;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
/**
* Visitor replace type parameter of the type RefType by Type Placeholder in a
* AST.
*
* @author Till Schnell
* @version 1.0
*/
public class ReplaceTypeparamVisitor
extends AbstractASTWalker
{
/**
* Containing the replaced RefType and the mapping TPH
*/
private final Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> tphMap;
/**
* Constructor for a {@code ReplaceTypeparamVisitor}.
*/
public ReplaceTypeparamVisitor () {
this.tphMap = new HashMap<>();
}
@Override
public void visit (Field field) {
System.out.println("Field: " + field.getName());
field.accept(new ReplaceRefTypeVisitor());
super.visit(field);
}
@Override
public void visit (FormalParameter formalParameter) {
System.out.println("FormalParameter: " + formalParameter.getName());
formalParameter.accept(new ReplaceRefTypeVisitor());
super.visit(formalParameter);
}
@Override
public void visit (LocalVarDecl localVarDecl) {
System.out.println("LocalVarDecl: " + localVarDecl.getName());
localVarDecl.accept(new ReplaceRefTypeVisitor());
super.visit(localVarDecl);
}
@Override
public void visit (Method method) {
System.out.println("Method: " + method.getName());
RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType();
if (returnType != null)
returnType.accept(new ReplaceRefTypeVisitor());
super.visit(method);
}
/**
* Return the mapping of the replaced {@link RefType} and the inserted
* {@link TypePlaceholder}.
*
* @return {@link Map} of {@link TypePlaceholder} and {@link RefType}
*/
public Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> getTphMap () {
return tphMap;
}
/**
* Visitor replace each RefType occurs.
*
* @author Till Schnell
* @version 1.0
*/
private class ReplaceRefTypeVisitor
extends AbstractASTWalker
{
@Override
public void visit (RefType refType) {
// check if RefType has Parameter Types
if (!refType.getParaList().isEmpty()) {
System.out.print("Visit Type: " + refType + " -> ");
// Iterate over all Parameter Types
for (ListIterator<RefTypeOrTPHOrWildcardOrGeneric> listIterator = refType.getParaList()
.listIterator(); listIterator.hasNext();) {
RefTypeOrTPHOrWildcardOrGeneric next = listIterator.next();
// If Parameter type is RefType replace with TPH
if (next instanceof RefType) {
RefType nextRefType = (RefType) next;
// Visit replaced RefType to get all nested type parameters
// Should be done before generating parents TPH to include child TPH in the
// parent TPH mapping
this.visit(nextRefType);
generateTphAndReplaceInTree(listIterator, next);
}
else if (next instanceof GenericRefType) {
// Visit of nested type arguments not necessary as Generic Types cannot contain
// type parameters
generateTphAndReplaceInTree(listIterator, next);
}
}
System.out.println(refType);
}
// Let the parent take care about all the other stuff
super.visit(refType);
}
/**
* Generate the TPH for a {@link RefTypeOrTPHOrWildcardOrGeneric} and saves the
* mapping.
*
* @param t {@link RefTypeOrTPHOrWildcardOrGeneric}
* @return {@link TypePlaceholder} generated
*/
private TypePlaceholder generateTypePlaceholder (RefTypeOrTPHOrWildcardOrGeneric t) {
TypePlaceholder tph = TypePlaceholder.fresh(new NullToken());
tphMap.put(tph, t);
return tph;
}
/**
* Generate the TPH for a {@link RefTypeOrTPHOrWildcardOrGeneric}, saves the
* mapping and replaces the type by the type placeholder in the tree.
*
* @param listIterator {@link ListIterator} over
* {@link RefTypeOrTPHOrWildcardOrGeneric} representing the
* tree to replace in
* @param refType {@link RefTypeOrTPHOrWildcardOrGeneric} to generate type
* placeholder for and replace
*/
private void generateTphAndReplaceInTree (ListIterator<RefTypeOrTPHOrWildcardOrGeneric> listIterator,
RefTypeOrTPHOrWildcardOrGeneric refType) {
// Generate TPH
TypePlaceholder tph = generateTypePlaceholder(refType);
// Replace in AST
listIterator.set(tph);
}
}
}

@ -0,0 +1,42 @@
package de.dhbwstuttgart.inferWildcards;
import java.io.File;
import java.util.Map;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
/**
* Class providing utilities to generate the Type placeholder for parameter
* types which shall be inferred.
*
* @author Till Schnell
* @version 1.0
*/
public final class TypePlaceholderReplaceUtils
{
private TypePlaceholderReplaceUtils () {
throw new AssertionError("No TypePlaceholderReplaceUtils instance for you");
}
/**
* Return the map of the generated type placeholder for the associate parameter
* type
*
* @param compiler {@link JavaTXCompiler} to get the source file from
* @return {@link Map} over {@link TypePlaceholder} and the {@link RefType}
* replaced
*/
public static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTypePlaceholder (
JavaTXCompiler compiler) {
Map<File, SourceFile> sourceFiles = compiler.sourceFiles;
ReplaceTypeparamVisitor visitor = new ReplaceTypeparamVisitor();
sourceFiles.forEach( (k, v) -> v.accept(visitor));
return visitor.getTphMap();
}
}

@ -272,10 +272,8 @@ public class StatementGenerator {
whole+=".";
}
if(parts.length < 2 || parts[0].contentEquals("this") ){
if(parts.length < 2){
receiver = new This(offset);
} else if (parts[0].contentEquals("super")) {
receiver = new Super(offset);
}else if(receiver == null){ //Handelt es sich um keinen Statischen Klassennamen:
String part = expression.substring(0,expression.length() - (1 + parts[parts.length-1].length()));
receiver = generateLocalOrFieldVarOrClassName(part, offset);

@ -4,8 +4,6 @@ import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.typeinference.result.ResultSetVisitor;
import org.antlr.v4.runtime.Token;
import java.util.Objects;
public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric
{
private String name;
@ -35,20 +33,12 @@ public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric
visitor.visit(this);
}
/**
* @since Studienarbeit Type Erasure
* @author etiennezink
*
* @param o
* @return
*/
@Override
public boolean equals(Object o) {
if((! (o instanceof GenericRefType))) return false;
GenericRefType genericRefType = (GenericRefType) o;
return getParsedName().equals(genericRefType.getParsedName()) &&
Objects.equals(getOffset(), genericRefType.getOffset());
}
// TODO Auto-generated method stub
return false;
}
@Override
public String toString()

@ -1,78 +0,0 @@
package de.dhbwstuttgart.target.generate;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.target.tree.*;
import de.dhbwstuttgart.target.tree.expression.TargetBlock;
import de.dhbwstuttgart.target.tree.type.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ASTToTargetAST {
public TargetClass convert(ClassOrInterface input, Map<TypePlaceholder, TargetType> sigma){
List<TargetConstructor> targetConstructors = new ArrayList<>();
//TODO constructor conversion -> also reduce syntactic sugar
return new TargetClass(input.getModifiers(),input.getClassName().toString(), null,
sigma.get(input.getSuperClass()),
input.getSuperInterfaces().stream().map(it -> sigma.get(it)).collect(Collectors.toList()),
targetConstructors,
input.getFieldDecl().stream().map(it -> convert(it, sigma)).collect(Collectors.toList()),
input.getMethods().stream().map(it -> convert(it, sigma)).collect(Collectors.toList()));
}
private TargetMethod convert(Method input, Map<TypePlaceholder, TargetType> sigma) {
List<MethodParameter> params = input.getParameterList().getFormalparalist().stream()
.map(param -> new MethodParameter(convert(param.getType(), sigma), param.getName())).collect(Collectors.toList());
return new TargetMethod(input.name, params, convert(input.block));
}
private TargetBlock convert(Block block) {
return new TargetBlock(block.statements.stream().map(e -> {
StatementToTargetExpression converter = new StatementToTargetExpression();
e.accept(converter);
return converter.result;
}).collect(Collectors.toList()));
}
private TargetField convert(Field input, Map<TypePlaceholder, TargetType> sigma) {
return new TargetField(convert(input.getType(), sigma), input.getName());
}
private TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input, Map<TypePlaceholder, TargetType> sigma) {
return input.acceptTV(new TypeVisitor<>() {
@Override
public TargetType visit(RefType refType) {
return new TargetRefType(refType.getName().toString(),
refType.getParaList().stream().map((it) -> convert(it, sigma)).collect(Collectors.toList()));
}
@Override
public TargetType visit(SuperWildcardType superWildcardType) {
return new TargetSuperWildcard(convert(superWildcardType.getInnerType(), sigma));
}
@Override
public TargetType visit(TypePlaceholder typePlaceholder) {
return sigma.get(typePlaceholder);
}
@Override
public TargetType visit(ExtendsWildcardType extendsWildcardType) {
return new TargetExtendsWildcard(convert(extendsWildcardType.getInnerType(), sigma));
}
@Override
public TargetType visit(GenericRefType genericRefType) {
return new TargetGenericType(genericRefType.getParsedName());
}
});
}
}

@ -1,156 +0,0 @@
package de.dhbwstuttgart.target.generate;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.target.tree.expression.TargetExpression;
public class StatementToTargetExpression implements StatementVisitor {
public TargetExpression result;
@Override
public void visit(ArgumentList argumentList) {
}
@Override
public void visit(LambdaExpression lambdaExpression) {
}
@Override
public void visit(Assign assign) {
}
@Override
public void visit(BinaryExpr binary) {
}
@Override
public void visit(Block block) {
}
@Override
public void visit(CastExpr castExpr) {
}
@Override
public void visit(EmptyStmt emptyStmt) {
}
@Override
public void visit(FieldVar fieldVar) {
}
@Override
public void visit(ForStmt forStmt) {
}
@Override
public void visit(IfStmt ifStmt) {
}
@Override
public void visit(InstanceOf instanceOf) {
}
@Override
public void visit(LocalVar localVar) {
}
@Override
public void visit(LocalVarDecl localVarDecl) {
}
@Override
public void visit(MethodCall methodCall) {
}
@Override
public void visit(NewClass methodCall) {
}
@Override
public void visit(NewArray newArray) {
}
@Override
public void visit(Return aReturn) {
}
@Override
public void visit(ReturnVoid aReturn) {
}
@Override
public void visit(StaticClassName staticClassName) {
}
@Override
public void visit(Super aSuper) {
}
@Override
public void visit(This aThis) {
}
@Override
public void visit(WhileStmt whileStmt) {
}
@Override
public void visit(DoStmt whileStmt) {
}
@Override
public void visit(AssignToField assignLeftSide) {
}
@Override
public void visit(AssignToLocal assignLeftSide) {
}
@Override
public void visit(SuperCall superCall) {
}
@Override
public void visit(ExpressionReceiver expressionReceiver) {
}
@Override
public void visit(UnaryExpr unaryExpr) {
}
@Override
public void visit(Literal literal) {
}
}

@ -1,7 +0,0 @@
package de.dhbwstuttgart.target.tree;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.target.tree.type.TargetType;
public record GenericDeclaration(GenericRefType generic, TargetType bound) {
}

@ -1,6 +0,0 @@
package de.dhbwstuttgart.target.tree;
import de.dhbwstuttgart.target.tree.type.TargetType;
public record MethodParameter(TargetType type, String name) {
}

@ -1,11 +0,0 @@
package de.dhbwstuttgart.target.tree;
import de.dhbwstuttgart.target.tree.type.TargetRefType;
import de.dhbwstuttgart.target.tree.type.TargetType;
import java.util.List;
public record TargetClass(int modifiers, String qualifiedName, List<GenericDeclaration> generics, TargetType superType,
List<TargetType> implementingInterfaces,
List<TargetConstructor> constructors, List<TargetField> fields, List<TargetMethod> methods) {}

@ -1,9 +0,0 @@
package de.dhbwstuttgart.target.tree;
import de.dhbwstuttgart.target.tree.expression.TargetBlock;
import java.util.List;
public record TargetConstructor(List<MethodParameter> parameterTypes, TargetBlock block) {
}

@ -1,7 +0,0 @@
package de.dhbwstuttgart.target.tree;
import de.dhbwstuttgart.target.tree.type.TargetType;
public record TargetField(TargetType type, String name) {
}

@ -1,9 +0,0 @@
package de.dhbwstuttgart.target.tree;
import de.dhbwstuttgart.target.tree.expression.TargetBlock;
import java.util.List;
public record TargetMethod(String name, List<MethodParameter> parameterTypes, TargetBlock block) {
}

@ -1,4 +0,0 @@
package de.dhbwstuttgart.target.tree.expression;
public record TargetAssign(TargetExpression leftSide, TargetExpression rightSide) implements TargetExpression {
}

@ -1,6 +0,0 @@
package de.dhbwstuttgart.target.tree.expression;
import java.util.List;
public record TargetBlock(List<TargetExpression> statememts) implements TargetExpression {
}

@ -1,7 +0,0 @@
package de.dhbwstuttgart.target.tree.expression;
import de.dhbwstuttgart.target.tree.type.*;
public sealed interface TargetExpression
permits TargetBlock, TargetLambdaExpression, TargetReturn, TargetThis, TargetSuper, TargetNew, TargetAssign {
}

@ -1,8 +0,0 @@
package de.dhbwstuttgart.target.tree.expression;
import de.dhbwstuttgart.target.tree.MethodParameter;
import java.util.List;
public record TargetLambdaExpression(List<MethodParameter> params, TargetExpression block) implements TargetExpression {
}

@ -1,8 +0,0 @@
package de.dhbwstuttgart.target.tree.expression;
import de.dhbwstuttgart.target.tree.type.TargetType;
import java.util.List;
public record TargetNew(TargetType instantiatedType, List<TargetExpression> params) implements TargetExpression {
}

@ -1,4 +0,0 @@
package de.dhbwstuttgart.target.tree.expression;
public record TargetReturn(TargetExpression expression) implements TargetExpression {
}

@ -1,4 +0,0 @@
package de.dhbwstuttgart.target.tree.expression;
public record TargetSuper() implements TargetExpression {
}

@ -1,4 +0,0 @@
package de.dhbwstuttgart.target.tree.expression;
public record TargetThis() implements TargetExpression {
}

@ -1,5 +0,0 @@
package de.dhbwstuttgart.target.tree.type;
public record TargetExtendsWildcard(TargetType innerType) implements TargetType{
}

@ -1,6 +0,0 @@
package de.dhbwstuttgart.target.tree.type;
import java.util.List;
public record TargetFunNType(int N, List<TargetRefType> params) implements TargetType {
}

@ -1,4 +0,0 @@
package de.dhbwstuttgart.target.tree.type;
public record TargetGenericType(String name) implements TargetType {
}

@ -1,8 +0,0 @@
package de.dhbwstuttgart.target.tree.type;
import de.dhbwstuttgart.target.tree.type.TargetType;
import java.util.List;
public record TargetRefType(String name, List<TargetType> params) implements TargetType {
}

@ -1,4 +0,0 @@
package de.dhbwstuttgart.target.tree.type;
public record TargetSuperWildcard(TargetType innerType) implements TargetType {
}

@ -1,5 +0,0 @@
package de.dhbwstuttgart.target.tree.type;
public sealed interface TargetType
permits TargetExtendsWildcard, TargetFunNType, TargetGenericType, TargetRefType, TargetSuperWildcard {
}

@ -0,0 +1,17 @@
package de.dhbwstuttgart.unify2.model;
public class Result<A> {
private final A v;
private Result(A a, UnifyError e){
this.v = a;
}
public static <A> Result<A> of(A a){
return new Result(a, null);
}
public static <A> Result<A> error(UnifyError error){
return new Result(null, error);
}
}

@ -0,0 +1,5 @@
package de.dhbwstuttgart.unify2.model;
public class UnifyError {
}

@ -1,75 +0,0 @@
package bytecode;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import general.TestCleanUp;
import org.junit.*;
import de.dhbwstuttgart.core.JavaTXCompiler;
import static org.junit.Assert.*;
/**
* Test for the file {@code OLFun2.jav}.
* Tests if the expected overloading for the method {@code m} exists.
*
* @since Studienarbeit Type Erasure
* @author etiennezink
*/
public class OLFun2Test {
private static String path;
private static File fileToTest;
private static JavaTXCompiler compiler;
private static ClassLoader loader;
private static Class<?> classToTest;
private static Class<?> classFun1IntInt;
private static Class<?> classFun1IntDouble;
private static Class<?> classFun1DoubleDouble;
private static Class<?> classFun1DoubleInt;
private static String generatedByteCodeDirectory = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/";
@BeforeClass
public static void setUp() throws Exception {
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/OLFun2.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
compiler.generateBytecode(generatedByteCodeDirectory);
loader = new URLClassLoader(new URL[] {new URL("file://"+generatedByteCodeDirectory)});
classToTest = loader.loadClass("OLFun2");
classFun1IntInt = loader.loadClass("Fun1$$Ljava$lang$Integer$_$Ljava$lang$Integer$_$");
classFun1IntDouble = loader.loadClass("Fun1$$Ljava$lang$Integer$_$Ljava$lang$Double$_$");
classFun1DoubleDouble = loader.loadClass("Fun1$$Ljava$lang$Double$_$Ljava$lang$Double$_$");
classFun1DoubleInt = loader.loadClass("Fun1$$Ljava$lang$Double$_$Ljava$lang$Integer$_$");
}
@Test
public void mExistsWithIntegerInteger() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1IntInt);
assertNotNull(m);
}
@Test
public void mExistsWithIntegerDouble() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1IntDouble);
assertNotNull(m);
}
@Test
public void mExistsWithDoubleInteger() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1DoubleInt);
assertNotNull(m);
}
@Test
public void mExistsWithDoubleDouble() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1DoubleDouble);
assertNotNull(m);
}
@AfterClass
public static void cleanUp() {
TestCleanUp.cleanUpDirectory(new File(generatedByteCodeDirectory), f -> f.getName().contains(".class"));
}
}

@ -1,91 +1,52 @@
package bytecode;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import general.TestCleanUp;
import org.junit.*;
import de.dhbwstuttgart.core.JavaTXCompiler;
import static org.junit.Assert.*;
import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler;
/**
* Test for the file {@code OLFun.jav}.
* Tests if the expected overloading for the method {@code m} exists.
*
* @since Studienarbeit Type Erasure
* @author etiennezink
*/
public class OLFunTest {
private static String path;
private static File fileToTest;
private static JavaTXCompiler compiler;
private static ClassLoader loader;
private static Class<?> classToTest;
private static Class<?> classFun1IntInt;
private static Class<?> classFun1IntDouble;
private static Class<?> classFun1DoubleDouble;
private static Class<?> classFun1DoubleInt;
private static Class<?> classFun1StringInt;
private static Class<?> classFun1StringDouble;
private static String generatedByteCodeDirectory = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/";
@BeforeClass
public static void setUp() throws Exception {
private static String pathToClassFile;
private static Object instanceOfClass;
@Test
public void generateBC() throws Exception {
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/OLFun.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
compiler.generateBytecode(generatedByteCodeDirectory);
loader = new URLClassLoader(new URL[] {new URL("file://"+generatedByteCodeDirectory)});
compiler.generateBytecode(System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/");
pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/";
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass("OLFun");
classFun1IntInt = loader.loadClass("Fun1$$Ljava$lang$Integer$_$Ljava$lang$Integer$_$");
classFun1IntDouble = loader.loadClass("Fun1$$Ljava$lang$Integer$_$Ljava$lang$Double$_$");
classFun1DoubleDouble = loader.loadClass("Fun1$$Ljava$lang$Double$_$Ljava$lang$Double$_$");
classFun1DoubleInt = loader.loadClass("Fun1$$Ljava$lang$Double$_$Ljava$lang$Integer$_$");
classFun1StringInt = loader.loadClass("Fun1$$Ljava$lang$String$_$Ljava$lang$Integer$_$");
classFun1StringDouble = loader.loadClass("Fun1$$Ljava$lang$String$_$Ljava$lang$Double$_$");
/*
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
Method m = classToTest.getDeclaredMethod("m");
Class<?> lambda = m.invoke(instanceOfClass).getClass();
Method apply = lambda.getMethod("apply", Object.class);
// Damit man auf die Methode zugreifen kann
apply.setAccessible(true);
Integer i = 77;
Integer result = (Integer) apply.invoke(m.invoke(instanceOfClass), i);
assertEquals(77, result);
*/
}
@Test
public void mExistsWithIntegerInteger() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1IntInt, Integer.class);
assertNotNull(m);
}
@Test
public void mExistsWithIntegerDouble() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1IntDouble, Integer.class);
assertNotNull(m);
}
@Test
public void mExistsWithDoubleInteger() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1DoubleInt, Double.class);
assertNotNull(m);
}
@Test
public void mExistsWithDoubleDouble() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1DoubleDouble, Double.class);
assertNotNull(m);
}
@Test
public void mExistsWithStringInteger() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1StringInt, String.class);
assertNotNull(m);
}
@Test
public void mExistsWithStringDouble() throws Exception{
Method m = classToTest.getDeclaredMethod("m", classFun1StringDouble, String.class);
assertNotNull(m);
}
@AfterClass
public static void cleanUp(){
TestCleanUp.cleanUpDirectory(new File(generatedByteCodeDirectory), f -> f.getName().contains(".class"));
}
}

@ -1,248 +0,0 @@
package bytecode.funN;
import de.dhbwstuttgart.bytecode.funN.FunNGenerator;
import de.dhbwstuttgart.bytecode.funN.FunNUtilities;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import org.junit.BeforeClass;
import org.junit.Test;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Type;
import java.util.Arrays;
import static org.junit.Assert.*;
import static org.objectweb.asm.Opcodes.*;
public class FunNGeneratorTest {
static FunNUtilities funNGenerator;
static RefType voidType;
static RefType integerType;
static GenericRefType genericT;
static TypePlaceholder tph;
@BeforeClass
public static void setUp(){
funNGenerator = FunNGenerator.getInstance();
voidType = new RefType(new JavaClassName(Type.getInternalName(Void.class)), null);
integerType = new RefType(new JavaClassName(Type.getInternalName(Integer.class)), null);
genericT = new GenericRefType("T", null);
tph = TypePlaceholder.fresh(null);
}
@Test
public void superClassName_0(){
var superClassName = funNGenerator.getSuperClassName(0);
assertEquals("Fun0$$", superClassName);
}
@Test
public void superClassName_1(){
var superClassName = funNGenerator.getSuperClassName(1);
assertEquals("Fun1$$", superClassName);
}
@Test
public void specializedClassName_VoidVoid(){
var specializedClassName = funNGenerator.getSpecializedClassName(Arrays.asList(), voidType);
assertEquals("Fun0$$Ljava$lang$Void$_$", specializedClassName);
}
@Test
public void specializedClassName_VoidInt(){
var specializedClassName = funNGenerator.getSpecializedClassName(Arrays.asList(), integerType);
assertEquals("Fun0$$Ljava$lang$Integer$_$", specializedClassName);
}
@Test
public void specializedClassName_IntInt(){
var specializedClassName = funNGenerator.getSpecializedClassName(Arrays.asList(integerType), integerType);
assertEquals("Fun1$$Ljava$lang$Integer$_$Ljava$lang$Integer$_$", specializedClassName);
}
@Test
public void specializedClassName_IntT(){
var specializedClassName = funNGenerator.getSpecializedClassName(Arrays.asList(integerType), genericT);
assertEquals("Fun1$$Ljava$lang$Integer$_$LT$_$", specializedClassName);
}
@Test
public void specializedClassName_IntTPH(){
var specializedClassName = funNGenerator.getSpecializedClassName(Arrays.asList(integerType), TypePlaceholder.fresh(null));
assertEquals("Fun1$$Ljava$lang$Integer$_$LTPH$_$", specializedClassName);
}
@Test
public void signature_IntInt(){
var classSignature = funNGenerator.getSpecializedSignature(Arrays.asList(integerType), integerType);
assertEquals("LFun1$$Ljava$lang$Integer$_$Ljava$lang$Integer$_$;", classSignature);
}
@Test
public void signature_IntT(){
var classSignature = funNGenerator.getSpecializedSignature(Arrays.asList(integerType), genericT);
assertEquals("LFun1$$Ljava$lang$Integer$_$LT$_$<TT;>;", classSignature);
}
@Test
public void signature_TPHTPH(){
var classSignature = funNGenerator.getSpecializedSignature(Arrays.asList(tph), tph);
assertEquals(String.format("LFun1$$LTPH$_$LTPH$_$<T%s$;T%s$;>;",tph.getName(), tph.getName()), classSignature);
}
@Test
public void descriptor_IntInt(){
var classDescriptor = funNGenerator.getSpecializedDescriptor(Arrays.asList(integerType), integerType);
//does not have to contain L and ; because TypeToDescriptor returns the descriptor without these characters as well
assertEquals("Fun1$$Ljava$lang$Integer$_$Ljava$lang$Integer$_$", classDescriptor);
}
@Test
public void descriptor_IntT(){
var classDescriptor = funNGenerator.getSpecializedDescriptor(Arrays.asList(integerType), genericT);
//does not have to contain L and ; because TypeToDescriptor returns the descriptor without these characters as well
assertEquals("Fun1$$Ljava$lang$Integer$_$LT$_$", classDescriptor);
}
@Test
public void descriptor_TPHTPH(){
var classDescriptor = funNGenerator.getSpecializedDescriptor(Arrays.asList(tph), tph);
//does not have to contain L and ; because TypeToDescriptor returns the descriptor without these characters as well
assertEquals("Fun1$$LTPH$_$LTPH$_$", classDescriptor);
}
@Test
public void getArguments_Empty(){
var arguments = funNGenerator.getArguments(Arrays.asList());
assertTrue(arguments.isEmpty());
}
@Test
public void getArguments_Int(){
var arguments = funNGenerator.getArguments(Arrays.asList(integerType));
assertTrue(arguments.isEmpty());
}
@Test
public void getArguments_IntT(){
var arguments = funNGenerator.getArguments(Arrays.asList(integerType, genericT));
assertTrue(arguments.size() == 1);
assertTrue(arguments.contains(integerType));
}
@Test
public void getArguments_IntTInt(){
var arguments = funNGenerator.getArguments(Arrays.asList(integerType, genericT, integerType));
assertTrue(arguments.size() == 2);
assertTrue(arguments.contains(integerType));
assertTrue(arguments.contains(genericT));
}
@Test
public void getReturnType_Empty(){
var returnType = funNGenerator.getReturnType(Arrays.asList());
assertNull(returnType);
}
@Test
public void getReturnType_Int(){
var returnType = funNGenerator.getReturnType(Arrays.asList(integerType));
assertEquals(integerType, returnType);
}
@Test
public void getReturnType_IntT(){
var returnType = funNGenerator.getReturnType(Arrays.asList(integerType, genericT));
assertEquals(genericT, returnType);
}
@Test
public void superBytecode_0(){
var superBytecode = funNGenerator.generateSuperBytecode(0);
assertArrayEquals(superBytecodeReference_0(), superBytecode);
}
@Test
public void superBytecode_1(){
var superBytecode = funNGenerator.generateSuperBytecode(1);
assertArrayEquals(superBytecodeReference_1(), superBytecode);
}
@Test
public void superBytecode_2(){
var superBytecode = funNGenerator.generateSuperBytecode(2);
assertArrayEquals(superBytecodeReference_2(), superBytecode);
}
@Test
public void specializedBytecode_VoidInt(){
var specializedBytecode = funNGenerator.generateSpecializedBytecode(Arrays.asList(), integerType);
assertArrayEquals(specializedBytecodeReference_VoidInt(), specializedBytecode);
}
@Test
public void specializedBytecode_IntInt(){
var specializedBytecode = funNGenerator.generateSpecializedBytecode(Arrays.asList(integerType), integerType);
assertArrayEquals(specializedBytecodeReference_IntInt(), specializedBytecode);
}
@Test
public void specializedBytecode_TIntInt(){
var specializedBytecode = funNGenerator.generateSpecializedBytecode(Arrays.asList(genericT, integerType), integerType);
assertArrayEquals(specializedBytecodeReference_TIntInt(), specializedBytecode);
}
//super bytecode reference methods
private static byte[] superBytecodeReference_0() {
var classWriter = new ClassWriter(0);
classWriter.visit(V1_8, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, "Fun0$$", "<R:Ljava/lang/Object;>Ljava/lang/Object;", "java/lang/Object", null);
var methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "apply", "()Ljava/lang/Object;", "()TR;", null);
methodVisitor.visitEnd();
classWriter.visitEnd();
return classWriter.toByteArray();
}
private static byte[] superBytecodeReference_1() {
var classWriter = new ClassWriter(0);
classWriter.visit(V1_8, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, "Fun1$$", "<T1:Ljava/lang/Object;R:Ljava/lang/Object;>Ljava/lang/Object;", "java/lang/Object", null);
var methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "apply", "(Ljava/lang/Object;)Ljava/lang/Object;", "(TT1;)TR;", null);
methodVisitor.visitEnd();
classWriter.visitEnd();
return classWriter.toByteArray();
}
private static byte[] superBytecodeReference_2() {
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(V1_8, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, "Fun2$$", "<T1:Ljava/lang/Object;T2:Ljava/lang/Object;R:Ljava/lang/Object;>Ljava/lang/Object;", "java/lang/Object", null);
var methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, "apply", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", "(TT1;TT2;)TR;", null);
methodVisitor.visitEnd();
classWriter.visitEnd();
return classWriter.toByteArray();
}
//specialized bytecode reference methods
private static byte[] specializedBytecodeReference_VoidInt() {
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(V1_8, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, "Fun0$$Ljava$lang$Integer$_$", "Ljava/lang/Object;LFun0$$<Ljava/lang/Integer;>;", "java/lang/Object", new String[]{"Fun0$$"});
classWriter.visitEnd();
return classWriter.toByteArray();
}
private static byte[] specializedBytecodeReference_IntInt() {
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(V1_8, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, "Fun1$$Ljava$lang$Integer$_$Ljava$lang$Integer$_$", "Ljava/lang/Object;LFun1$$<Ljava/lang/Integer;Ljava/lang/Integer;>;", "java/lang/Object", new String[]{"Fun1$$"});
classWriter.visitEnd();
return classWriter.toByteArray();
}
private static byte[] specializedBytecodeReference_TIntInt() {
ClassWriter classWriter = new ClassWriter(0);
classWriter.visit(V1_8, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, "Fun2$$LT$_$Ljava$lang$Integer$_$Ljava$lang$Integer$_$", "<T:Ljava/lang/Object;>Ljava/lang/Object;LFun2$$<TT;Ljava/lang/Integer;Ljava/lang/Integer;>;", "java/lang/Object", new String[]{"Fun2$$"});
classWriter.visitEnd();
return classWriter.toByteArray();
}
}

@ -1,14 +0,0 @@
package general;
import java.io.File;
import java.io.FileFilter;
public class TestCleanUp {
public static void cleanUpDirectory(File directory, FileFilter fileFilter){
if(!directory.isDirectory()) throw new RuntimeException("Directory for bytecode generation is wrong!");
for (File file: directory.listFiles(fileFilter)) {
file.delete();
}
}
}

@ -0,0 +1,148 @@
package inferWildcards;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils;
import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards;
import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.PairTPHequalRefTypeOrWildcardType;
import de.dhbwstuttgart.typeinference.result.ResultPair;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class TestInferWildcardsFields
{
private String resourceFilePath;
private String resourceDirPath;
@Before
public void setup () {
resourceDirPath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards";
resourceFilePath = resourceDirPath + "/TestClassWildcardsFields.java";
}
private JavaTXCompiler getStandardCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompiler(files1);
}
private JavaTXCompilerWildcards getWildcardsCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompilerWildcards(files1);
}
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph (
JavaTXCompiler javaTXCompiler) {
System.out.println("\nReplacements:");
return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler);
}
private static List<ResultSet> generateExpectedTypeInferResult (JavaTXCompilerWildcards compiler) {
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = compiler.getTphMap();
ArrayList<ResultSet> list = new ArrayList<>();
tphMap.forEach( (tph, t) -> {
ResultPair r = new PairTPHequalRefTypeOrWildcardType(tph,
new RefType(new JavaClassName("java.lang.String"), new NullToken()));
HashSet<ResultPair> set = new HashSet<>();
set.add(r);
list.add(new ResultSet(set));
});
return list;
}
@Test
public void testGenerateTph () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate TPH --------------\n");
JavaTXCompiler javaTXCompiler = getStandardCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph = generateTph(
javaTXCompiler);
System.out.println(generateTph);
assertThat("Number of TPH is 4", 4, CoreMatchers.is(generateTph.size()));
}
@Test
public void testGeneratedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);
int size = 0;
for (Set<Constraint<Pair>> l : generateConstraints.getOderConstraints())
for (Constraint<Pair> c : l)
size += c.size();
assertThat("Number of Generated Constraints", tphMap.size() * 3, CoreMatchers.is(size));
}
@Test
public void testCombinedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Combined Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
ConstraintSet<Pair> constraints = javaTXCompilerWildcards.getConstraints();
System.out.println(constraints);
}
@Test
public void testTypeInference () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
List<ResultSet> typeInference = wildcardsCompiler.typeInference();
System.out.println(typeInference);
assertThat("Type Inference Results containing the correct Wildcard results", typeInference,
CoreMatchers.anything());
}
@Test
public void testGenrationBytecode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateBytecode(new File(resourceDirPath + "/generatedBC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
@Test
public void testGenrationSourceCode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
}

@ -0,0 +1,125 @@
package inferWildcards;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils;
import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards;
import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class TestInferWildcardsMap
{
private String resourceFilePath;
private String resourceDirPath;
@Before
public void setup () {
resourceDirPath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards";
resourceFilePath = resourceDirPath + "/TestClassWildcardsMap.java";
}
private JavaTXCompiler getStandardCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompiler(files1);
}
private JavaTXCompilerWildcards getWildcardsCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompilerWildcards(files1);
}
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph (
JavaTXCompiler javaTXCompiler) {
System.out.println("\nReplacements:");
return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler);
}
@Test
public void testGenerateTph () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate TPH --------------\n");
JavaTXCompiler javaTXCompiler = getStandardCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph = generateTph(
javaTXCompiler);
System.out.println(generateTph);
assertThat("Number of TPH is 6", 6, CoreMatchers.is(generateTph.size()));
}
@Test
public void testGeneratedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);
int size = 0;
for (Set<Constraint<Pair>> l : generateConstraints.getOderConstraints())
for (Constraint<Pair> c : l)
size += c.size();
assertThat("Number of Generated Constraints", tphMap.size() * 3, CoreMatchers.is(size));
}
@Test
public void testCombinedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Combined Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
ConstraintSet<Pair> constraints = javaTXCompilerWildcards.getConstraints();
System.out.println(constraints);
}
@Test
public void testTypeInference () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
List<ResultSet> typeInference = wildcardsCompiler.typeInference();
System.out.println(typeInference);
assertThat("Type Inference Results containing the correct Wildcard results", typeInference,
CoreMatchers.anything());
}
@Test
public void testGenrationBytecode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateBytecode(new File(resourceDirPath + "/generatedBC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
@Test
public void testGenrationSourceCode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
}

@ -0,0 +1,126 @@
package inferWildcards;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils;
import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards;
import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class TestInferWildcardsMapNested
{
private String resourceFilePath;
private String resourceDirPath;
@Before
public void setup () {
resourceDirPath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards";
resourceFilePath = resourceDirPath + "/TestClassWildcardsMapNested.java";
}
private JavaTXCompiler getStandardCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompiler(files1);
}
private JavaTXCompilerWildcards getWildcardsCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompilerWildcards(files1);
}
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph (
JavaTXCompiler javaTXCompiler) {
System.out.println("\nReplacements:");
return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler);
}
@Test
public void testGenerateTph () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate TPH --------------\n");
JavaTXCompiler javaTXCompiler = getStandardCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph = generateTph(
javaTXCompiler);
System.out.println(generateTph);
assertThat("Number of TPH is 9", 9, CoreMatchers.is(generateTph.size()));
}
@Test
public void testGeneratedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);
int size = 0;
for (Set<Constraint<Pair>> l : generateConstraints.getOderConstraints())
for (Constraint<Pair> c : l)
size += c.size();
assertThat("Number of Generated Constraints", tphMap.size() * 3, CoreMatchers.is(size));
}
@Test
public void testCombinedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Combined Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
ConstraintSet<Pair> constraints = javaTXCompilerWildcards.getConstraints();
System.out.println(constraints);
}
@Test
public void testTypeInference () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
List<ResultSet> typeInference = wildcardsCompiler.typeInference();
System.out.println(typeInference);
assertThat("Type Inference Results containing the correct Wildcard results", typeInference,
CoreMatchers.anything());
}
@Test
public void testGenrationBytecode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateBytecode(new File(resourceDirPath + "/generatedBC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
@Test
public void testGenrationSourceCode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
}

@ -0,0 +1,125 @@
package inferWildcards;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils;
import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards;
import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class TestInferWildcardsNested
{
private String resourceFilePath;
private String resourceDirPath;
@Before
public void setup () {
resourceDirPath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards";
resourceFilePath = resourceDirPath + "/TestClassWildcardsNested.java";
}
private JavaTXCompiler getStandardCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompiler(files1);
}
private JavaTXCompilerWildcards getWildcardsCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompilerWildcards(files1);
}
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph (
JavaTXCompiler javaTXCompiler) {
System.out.println("\nReplacements:");
return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler);
}
@Test
public void testGenerateTph () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate TPH --------------\n");
JavaTXCompiler javaTXCompiler = getStandardCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph = generateTph(
javaTXCompiler);
System.out.println(generateTph);
assertThat("Number of TPH is 6", 6, CoreMatchers.is(generateTph.size()));
}
@Test
public void testGeneratedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);
int size = 0;
for (Set<Constraint<Pair>> l : generateConstraints.getOderConstraints())
for (Constraint<Pair> c : l)
size += c.size();
assertThat("Number of Generated Constraints", tphMap.size() * 3, CoreMatchers.is(size));
}
@Test
public void testCombinedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Combined Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
ConstraintSet<Pair> constraints = javaTXCompilerWildcards.getConstraints();
System.out.println(constraints);
}
@Test
public void testTypeInference () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
List<ResultSet> typeInference = wildcardsCompiler.typeInference();
System.out.println(typeInference);
assertThat("Type Inference Results containing the correct Wildcard results", typeInference,
CoreMatchers.anything());
}
@Test
public void testGenrationBytecode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateBytecode(new File(resourceDirPath + "/generatedBC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
@Test
public void testGenrationSourceCode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
}

@ -0,0 +1,125 @@
package inferWildcards;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils;
import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards;
import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class TestInferWildcardsParamType
{
private String resourceFilePath;
private String resourceDirPath;
@Before
public void setup () {
resourceDirPath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards";
resourceFilePath = resourceDirPath + "/TestClassWildcardsParamType.java";
}
private JavaTXCompiler getStandardCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompiler(files1);
}
private JavaTXCompilerWildcards getWildcardsCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompilerWildcards(files1);
}
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph (
JavaTXCompiler javaTXCompiler) {
System.out.println("\nReplacements:");
return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler);
}
@Test
public void testGenerateTph () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate TPH --------------\n");
JavaTXCompiler javaTXCompiler = getStandardCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph = generateTph(
javaTXCompiler);
System.out.println(generateTph);
assertThat("Number of TPH is 4", 4, CoreMatchers.is(generateTph.size()));
}
@Test
public void testGeneratedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);
int size = 0;
for (Set<Constraint<Pair>> l : generateConstraints.getOderConstraints())
for (Constraint<Pair> c : l)
size += c.size();
assertThat("Number of Generated Constraints", tphMap.size() * 3, CoreMatchers.is(size));
}
@Test
public void testCombinedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Combined Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
ConstraintSet<Pair> constraints = javaTXCompilerWildcards.getConstraints();
System.out.println(constraints);
}
@Test
public void testTypeInference () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
List<ResultSet> typeInference = wildcardsCompiler.typeInference();
System.out.println(typeInference);
assertThat("Type Inference Results containing the correct Wildcard results", typeInference,
CoreMatchers.anything());
}
@Test
public void testGenrationBytecode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateBytecode(new File(resourceDirPath + "/generatedBC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
@Test
public void testGenrationSourceCode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
}

@ -0,0 +1,125 @@
package inferWildcards;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils;
import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards;
import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class TestInferWildcardsSingle
{
private String resourceFilePath;
private String resourceDirPath;
@Before
public void setup () {
resourceDirPath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards";
resourceFilePath = resourceDirPath + "/TestClassWildcardsSingle.java";
}
private JavaTXCompiler getStandardCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompiler(files1);
}
private JavaTXCompilerWildcards getWildcardsCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompilerWildcards(files1);
}
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph (
JavaTXCompiler javaTXCompiler) {
System.out.println("\nReplacements:");
return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler);
}
@Test
public void testGenerateTph () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate TPH --------------\n");
JavaTXCompiler javaTXCompiler = getStandardCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph = generateTph(
javaTXCompiler);
System.out.println(generateTph);
assertThat("Number of TPH is 3", 3, CoreMatchers.is(generateTph.size()));
}
@Test
public void testGeneratedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);
int size = 0;
for (Set<Constraint<Pair>> l : generateConstraints.getOderConstraints())
for (Constraint<Pair> c : l)
size += c.size();
assertThat("Number of Generated Constraints", tphMap.size() * 3, CoreMatchers.is(size));
}
@Test
public void testCombinedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Combined Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
ConstraintSet<Pair> constraints = javaTXCompilerWildcards.getConstraints();
System.out.println(constraints);
}
@Test
public void testTypeInference () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
List<ResultSet> typeInference = wildcardsCompiler.typeInference();
System.out.println(typeInference);
assertThat("Type Inference Results containing the correct Wildcard results", typeInference,
CoreMatchers.anything());
}
@Test
public void testGenrationBytecode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateBytecode(new File(resourceDirPath + "/generatedBC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
@Test
public void testGenrationSourceCode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
}

@ -0,0 +1,125 @@
package inferWildcards;
import static org.junit.Assert.assertThat;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.inferWildcards.ConstraintsGenerationUtils;
import de.dhbwstuttgart.inferWildcards.JavaTXCompilerWildcards;
import de.dhbwstuttgart.inferWildcards.TypePlaceholderReplaceUtils;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class TestInferWildcardsSingleLib
{
private String resourceFilePath;
private String resourceDirPath;
@Before
public void setup () {
resourceDirPath = System.getProperty("user.dir") + "/src/test/resources/inferWildcards";
resourceFilePath = resourceDirPath + "/TestClassWildcardsSingleLib.java";
}
private JavaTXCompiler getStandardCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompiler(files1);
}
private JavaTXCompilerWildcards getWildcardsCompiler () throws ClassNotFoundException, IOException {
File[] files1 = { new File(resourceFilePath) };
return new JavaTXCompilerWildcards(files1);
}
private static Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph (
JavaTXCompiler javaTXCompiler) {
System.out.println("\nReplacements:");
return TypePlaceholderReplaceUtils.generateTypePlaceholder(javaTXCompiler);
}
@Test
public void testGenerateTph () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate TPH --------------\n");
JavaTXCompiler javaTXCompiler = getStandardCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> generateTph = generateTph(
javaTXCompiler);
System.out.println(generateTph);
assertThat("Number of TPH is 2", 2, CoreMatchers.is(generateTph.size()));
}
@Test
public void testGeneratedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Generate Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> tphMap = javaTXCompilerWildcards
.getTphMap();
ConstraintSet<Pair> generateConstraints = ConstraintsGenerationUtils.generateConstraints(tphMap);
System.out.println(generateConstraints);
int size = 0;
for (Set<Constraint<Pair>> l : generateConstraints.getOderConstraints())
for (Constraint<Pair> c : l)
size += c.size();
assertThat("Number of Generated Constraints", tphMap.size() * 3, CoreMatchers.is(size));
}
@Test
public void testCombinedConstraints () throws ClassNotFoundException, IOException {
System.out.println("\n--------- Test Combined Constraints --------------\n");
JavaTXCompilerWildcards javaTXCompilerWildcards = getWildcardsCompiler();
ConstraintSet<Pair> constraints = javaTXCompilerWildcards.getConstraints();
System.out.println(constraints);
}
@Test
public void testTypeInference () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
List<ResultSet> typeInference = wildcardsCompiler.typeInference();
System.out.println(typeInference);
assertThat("Type Inference Results containing the correct Wildcard results", typeInference,
CoreMatchers.anything());
}
@Test
public void testGenrationBytecode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateBytecode(new File(resourceDirPath + "/generatedBC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
@Test
public void testGenrationSourceCode () throws ClassNotFoundException, IOException {
JavaTXCompilerWildcards wildcardsCompiler = getWildcardsCompiler();
wildcardsCompiler.generateSourceCode(new File(resourceDirPath + "/generatedSC"));
assertThat("Generation Succeeded", null, CoreMatchers.anything());
}
}

@ -20,17 +20,18 @@ import org.junit.Test;
*
*/
public class GeneralParserTest{
private static final String rootDirectory = System.getProperty("user.dir")+"/test/parser/";
@Test
public void run(){
List<String> filenames = new ArrayList<String>();
filenames.add("javFiles/parser/NewTest.jav");
//filenames.add("CastTest.jav");
/*
filenames.add("NewTest.jav");
filenames.add("FieldInitializationTest.jav");
filenames.add("ImportTest.jav");
filenames.add("CastTest.jav");
filenames.add("StatementsTest.jav");
//filenames.add("Methods.jav");
filenames.add("ImportTestGeneric.jav");
@ -39,14 +40,11 @@ public class GeneralParserTest{
//filenames.add("GenericFieldVarTest.jav");
filenames.add("FieldVarTest.jav");
filenames.add("StructuralTypes.jav");
filenames.add("ExtendsTest.jav");
*/
//filenames.add("PackageNameTest.jav");
// filenames.add("ExtendsTest.jav");
filenames.add("PackageNameTest.jav");
try{
for(String filename : filenames){
new JavaTXCompiler(new File(Thread.currentThread().getContextClassLoader().getResource(filename).getPath()));
}
new JavaTXCompiler(filenames.stream().map(s -> new File(rootDirectory + s)).collect(Collectors.toList()));
}catch(Exception exc){
exc.printStackTrace();
fail();

@ -1,5 +0,0 @@
package targetast;
public class ASTToTargetTest {
}

@ -4,9 +4,14 @@ import java.lang.Double;
import java.util.Vector;
import java.lang.Boolean;
public class OLFun {
//f = x -> {return x + x;};
m(f, x) {
var y = f.apply(x + x) + 1;
x = f.apply(x+x);
}
}

@ -1,13 +0,0 @@
import java.lang.String;
import java.lang.Integer;
import java.lang.Double;
import java.util.Vector;
import java.lang.Boolean;
public class OLFun2 {
m(f){
var x = 1;
var y = f.apply(x + x) + 1;
}
}

@ -0,0 +1,2 @@
/generatedSC/
/generatedBC/

@ -0,0 +1,31 @@
import java.util.ArrayList;
import java.util.List;
import java.lang.String;
class TestClassWildcardsFields
{
private List<String> field1;
//private int counter;
public TestClassWildcards () {
//field1 = new ArrayList<>();
//counter = 0;
}
/**
* Working method for Java
* @param param1
* @return
*/
public List<String> test (List<String> param1) {
List<String> localVar = field1;
field1 = param1;
//counter++;
return localVar;
}
public void foo() {
}
}

@ -0,0 +1,13 @@
import java.lang.String;
import java.lang.Object;
import java.util.Map;
import java.lang.Integer;
class TestClassWildcardsMap
{
public Map<Integer, Object> test(Map<Integer, String> input){
Map<Integer, Object> listOfObjects = input;
return listOfObjects;
}
}

@ -0,0 +1,16 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.lang.String;
import java.lang.Object;
import java.util.Map;
import java.lang.Integer;
class TestClassWildcardsMapNested
{
public Map<Integer, Collection<Object>> test(Map<Integer, List<String>> input){
Map<Integer, Collection<Object>> listOfObjects = input;
return listOfObjects;
}
}

@ -0,0 +1,15 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.lang.String;
import java.lang.Object;
import java.util.Map;
import java.lang.Integer;
class TestClassWildcardsNested
{
public List<Collection<Object>> test(List<List<String>> input){
List<Collection<Object>> listOfObjects = input;
return listOfObjects;
}
}

@ -0,0 +1,23 @@
import java.util.List;
import java.lang.String;
public final class TestClassWildcardsParamType<T1, T2> {
public final T1 first;
public final T2 second;
public TestClassWildcardsParamType(T1 first, T2 second) {
this.first = first;
this.second = second;
}
public static <T1, T2> TestClassWildcardsParamType<T1, T2> of(T1 first, T2 second) {
return new TestClassWildcardsParamType<>(first, second);
}
public static void main(String[] agrs) {
TestClassWildcardsParamType<Class<?>, String> pair = TestClassWildcardsParamType.of(List.class, "hello");
//Output of compiler
//TestClassWildcardsParamType<? extends Class<?>, String> pair = TestClassWildcardsParamType.of(List.class, "hello");
}
}

@ -0,0 +1,20 @@
import java.util.List;
import java.lang.String;
import java.lang.Object;
class TestClassWildcardsSingle
{
/**
* Non working method in normal Java
* @param input
* @return
*/
public List<Object> test (List<String> input) {
List<Object> listOfObjects = input;
Object test = listOfObjects.get(0);
String string = "Test";
input.add(string);
return listOfObjects;
}
}

@ -0,0 +1,15 @@
import java.util.List;
class TestClassWildcardsLib
{
// public <T> List<T> merge (List<T> l1, List<T> l2) {
// l2.forEach(s -> {if(!l1.contains(s)) l1.add(s);});
// return l2;
// }
public static <T> List<T> foo(List<T> dd){
T t = dd.get(1);
return dd;
}
}

@ -1,14 +0,0 @@
import de.test.Pair;
class Pairs {
setfst(fst) {
return new Pair<>(snd, fst);
}
swap () {
return new Pair<> (snd, fst); }
polyrec(p) {
return polyrec (p.swap());
}
}

@ -1,12 +0,0 @@
package de.test;
class Pair {
fst;
snd;
Pair(fst, snd) {
this.fst = fst;
this.snd = snd;
}
}

@ -1,9 +0,0 @@
public class OLFunTest{
public static void main(String[] args){
var olFun2 = new OLFun2();
olFun2.m((Integer x) -> x);
olFun2.m((Integer x) -> (Double) Double.valueOf(x));
olFun2.m((Double x) -> x);
olFun2.m((Double x) -> x.intValue());
}
}