Compare commits
82 Commits
targetByte
...
inferWildc
Author | SHA1 | Date | |
---|---|---|---|
|
26ae463e96 | ||
|
2e1e69df9a | ||
|
0e8012c132 | ||
|
68394565b5 | ||
|
c3b17a8f2f | ||
|
423cf8b9d2 | ||
|
87fb9e5a33 | ||
|
df6debec4f | ||
|
59177e09ad | ||
|
a9281a0db2 | ||
|
fe36b34a80 | ||
|
d3a18887c3 | ||
|
14821575bd | ||
|
c1519783ba | ||
|
53bbf3c511 | ||
|
41492e53ca | ||
|
74b9b024ee | ||
|
97483714e7 | ||
|
a7b9c2a4ee | ||
|
fd7628cb40 | ||
|
76a291ab41 | ||
|
c5c8ffeed1 | ||
|
ba9335e2f6 | ||
|
c098a0a1b0 | ||
|
cfce2f55ac | ||
|
4009a28333 | ||
|
f3dc0cbeb7 | ||
|
cd2f030ac6 | ||
|
f548548788 | ||
|
5cf41101bf | ||
|
e139f8c867 | ||
|
5a151a965c | ||
|
e5f03369cc | ||
|
f698c967c0 | ||
|
1509e21214 | ||
|
50c05064c7 | ||
|
6458a06293 | ||
|
a984e455e4 | ||
|
19c9f648a8 | ||
|
077bd62b92 | ||
|
de62df00e0 | ||
|
78e3b76dc3 | ||
|
c627f9de60 | ||
|
ff92807b83 | ||
|
1043b3f55f | ||
|
88440b873a | ||
|
4ca0e1e5f6 | ||
|
cb28405fe1 | ||
|
c7f91724a6 | ||
|
11b63e0a5d | ||
|
5f7829191c | ||
|
044d9a1860 | ||
|
6fcbca1187 | ||
|
572f41ffd4 | ||
|
e0f7f95bed | ||
|
78c365a7be | ||
|
bea072689b | ||
|
81697eee64 | ||
|
c474abd2bd | ||
|
6dd02a654b | ||
|
e5d5376ce9 | ||
|
0cdeee9e0d | ||
|
98b3ad1517 | ||
|
b59aabeea5 | ||
|
26953665c9 | ||
|
465f82e967 | ||
|
ce8b19acae | ||
|
7dfe546999 | ||
|
c96c56e882 | ||
|
3e57a64f9c | ||
|
e8539a84fd | ||
|
0551d25fbf | ||
|
cbe2d7b0f5 | ||
|
59b45b5f87 | ||
|
ad7e7ec42b | ||
|
9d2c85d686 | ||
|
63b4dbcc10 | ||
|
ca816fba85 | ||
|
54a2dbfedc | ||
|
07c1eeeb36 | ||
|
69a557f1f2 | ||
|
88175f8b48 |
1
.gitignore
vendored
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
|
||||
|
@ -91,6 +91,9 @@ public class JavaTXCompiler {
|
||||
public JavaTXCompiler(File sourceFile, Boolean log) throws IOException, ClassNotFoundException {
|
||||
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,6 +111,7 @@ public class JavaTXCompiler {
|
||||
//INSTANCE = this;
|
||||
}
|
||||
|
||||
|
||||
public ConstraintSet<Pair> getConstraints() throws ClassNotFoundException, IOException {
|
||||
List<ClassOrInterface> allClasses = new ArrayList<>();//environment.getAllAvailableClasses();
|
||||
List<ClassOrInterface> importedClasses = new ArrayList<>();
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -36,7 +36,7 @@ public class ASTFactory {
|
||||
List<Method> methoden = new ArrayList<>();
|
||||
List<de.dhbwstuttgart.syntaxtree.Constructor> konstruktoren = new ArrayList<>();
|
||||
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> allDeclaredMethods = new HashSet<>(Arrays.asList(jreClass.getDeclaredMethods()));
|
||||
@ -87,7 +87,7 @@ public class ASTFactory {
|
||||
// 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();
|
||||
RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass);
|
||||
Parameter[] jreParams = constructor.getParameters();
|
||||
@ -106,10 +106,10 @@ public class ASTFactory {
|
||||
int modifier = constructor.getModifiers();
|
||||
|
||||
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){
|
||||
|
17
src/main/java/de/dhbwstuttgart/unify2/model/Result.java
Normal file
17
src/main/java/de/dhbwstuttgart/unify2/model/Result.java
Normal 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package de.dhbwstuttgart.unify2.model;
|
||||
|
||||
public class UnifyError {
|
||||
|
||||
}
|
148
src/test/java/inferWildcards/TestInferWildcardsFields.java
Normal file
148
src/test/java/inferWildcards/TestInferWildcardsFields.java
Normal 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());
|
||||
}
|
||||
}
|
125
src/test/java/inferWildcards/TestInferWildcardsMap.java
Normal file
125
src/test/java/inferWildcards/TestInferWildcardsMap.java
Normal 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());
|
||||
}
|
||||
}
|
126
src/test/java/inferWildcards/TestInferWildcardsMapNested.java
Normal file
126
src/test/java/inferWildcards/TestInferWildcardsMapNested.java
Normal 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());
|
||||
}
|
||||
}
|
125
src/test/java/inferWildcards/TestInferWildcardsNested.java
Normal file
125
src/test/java/inferWildcards/TestInferWildcardsNested.java
Normal 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());
|
||||
}
|
||||
}
|
125
src/test/java/inferWildcards/TestInferWildcardsParamType.java
Normal file
125
src/test/java/inferWildcards/TestInferWildcardsParamType.java
Normal 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());
|
||||
}
|
||||
}
|
125
src/test/java/inferWildcards/TestInferWildcardsSingle.java
Normal file
125
src/test/java/inferWildcards/TestInferWildcardsSingle.java
Normal 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());
|
||||
}
|
||||
}
|
125
src/test/java/inferWildcards/TestInferWildcardsSingleLib.java
Normal file
125
src/test/java/inferWildcards/TestInferWildcardsSingleLib.java
Normal 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());
|
||||
}
|
||||
}
|
2
src/test/resources/inferWildcards/.gitignore
vendored
Normal file
2
src/test/resources/inferWildcards/.gitignore
vendored
Normal file
@ -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() {
|
||||
|
||||
}
|
||||
}
|
13
src/test/resources/inferWildcards/TestClassWildcardsMap.java
Normal file
13
src/test/resources/inferWildcards/TestClassWildcardsMap.java
Normal 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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user