Compare commits

...

82 Commits

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

1
.gitignore vendored
View File

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

View File

@ -91,6 +91,9 @@ public class JavaTXCompiler {
public JavaTXCompiler(File sourceFile, Boolean log) throws IOException, ClassNotFoundException { public JavaTXCompiler(File sourceFile, Boolean log) throws IOException, ClassNotFoundException {
this(sourceFile); this(sourceFile);
this.log = log; this.log = log;
}
public JavaTXCompiler(File[] sourceFiles) throws IOException, ClassNotFoundException {
this(Arrays.asList(sourceFiles), null);
} }
public JavaTXCompiler(List<File> sourceFiles) throws IOException, ClassNotFoundException { public JavaTXCompiler(List<File> sourceFiles) throws IOException, ClassNotFoundException {
this(sourceFiles, null); this(sourceFiles, null);
@ -108,6 +111,7 @@ public class JavaTXCompiler {
//INSTANCE = this; //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> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
List<ClassOrInterface> importedClasses = new ArrayList<>(); List<ClassOrInterface> importedClasses = new ArrayList<>();

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -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);
}
}
}

View File

@ -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();
}
}

View File

@ -36,7 +36,7 @@ public class ASTFactory {
List<Method> methoden = new ArrayList<>(); List<Method> methoden = new ArrayList<>();
List<de.dhbwstuttgart.syntaxtree.Constructor> konstruktoren = new ArrayList<>(); List<de.dhbwstuttgart.syntaxtree.Constructor> konstruktoren = new ArrayList<>();
for(java.lang.reflect.Constructor constructor : jreClass.getConstructors()){ for(java.lang.reflect.Constructor constructor : jreClass.getConstructors()){
konstruktoren.add(createConstructor(constructor, jreClass)); createConstructor(constructor, jreClass).map(c -> konstruktoren.add(c));
} }
Set<java.lang.reflect.Method> allMethods = new HashSet<>(Arrays.asList(jreClass.getMethods())); Set<java.lang.reflect.Method> allMethods = new HashSet<>(Arrays.asList(jreClass.getMethods()));
Set<java.lang.reflect.Method> allDeclaredMethods = new HashSet<>(Arrays.asList(jreClass.getDeclaredMethods())); Set<java.lang.reflect.Method> allDeclaredMethods = new HashSet<>(Arrays.asList(jreClass.getDeclaredMethods()));
@ -87,7 +87,7 @@ public class ASTFactory {
// return createClass(classType).getType(); // return createClass(classType).getType();
//} //}
private static de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) { private static Optional<de.dhbwstuttgart.syntaxtree.Constructor> createConstructor(Constructor constructor, Class inClass) {
String name = constructor.getName(); String name = constructor.getName();
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass); RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass);
Parameter[] jreParams = constructor.getParameters(); Parameter[] jreParams = constructor.getParameters();
@ -106,10 +106,10 @@ public class ASTFactory {
int modifier = constructor.getModifiers(); int modifier = constructor.getModifiers();
if(inClass.equals(java.lang.Object.class)){ if(inClass.equals(java.lang.Object.class)){
return null; return Optional.empty();
} }
return new de.dhbwstuttgart.syntaxtree.Constructor(modifier, name,returnType, parameterList, block, gtvDeclarations, offset /*, new ArrayList<>() geloescht PL 2018-11-24 */); return Optional.of(new de.dhbwstuttgart.syntaxtree.Constructor(modifier, name,returnType, parameterList, block, gtvDeclarations, offset /*, new ArrayList<>() geloescht PL 2018-11-24 */));
} }
public static Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass, Boolean isInherited){ public static Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass, Boolean isInherited){

View File

@ -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);
}
}

View File

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

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

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

View File

@ -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() {
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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");
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}