forked from JavaTX/JavaCompilerCore
Merge branch 'targetBytecode' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into targetBytecode
This commit is contained in:
commit
55a6f3e434
9
pom.xml
9
pom.xml
@ -17,10 +17,11 @@ http://maven.apache.org/maven-v4_0_0.xsd">
|
|||||||
<version>4.11</version>
|
<version>4.11</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.antlr/antlr4 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.antlr</groupId>
|
<groupId>org.antlr</groupId>
|
||||||
<artifactId>antlr4</artifactId>
|
<artifactId>antlr4</artifactId>
|
||||||
<version>4.8-1</version>
|
<version>4.11.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>commons-io</groupId>
|
<groupId>commons-io</groupId>
|
||||||
@ -53,12 +54,14 @@ http://maven.apache.org/maven-v4_0_0.xsd">
|
|||||||
<version>3.8.0</version>
|
<version>3.8.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<compilerArgs>--enable-preview</compilerArgs>
|
<compilerArgs>--enable-preview</compilerArgs>
|
||||||
</configuration>
|
<source>19</source>
|
||||||
|
<target>19</target>
|
||||||
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.antlr</groupId>
|
<groupId>org.antlr</groupId>
|
||||||
<artifactId>antlr4-maven-plugin</artifactId>
|
<artifactId>antlr4-maven-plugin</artifactId>
|
||||||
<version>4.8-1</version>
|
<version>4.11.1</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>antlr</id>
|
<id>antlr</id>
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
import java.lang.String;
|
||||||
|
import java.lang.Integer;
|
||||||
|
|
||||||
class Generics2<B extends String>{
|
class Generics2<B extends String>{
|
||||||
<B extends Integer> B m1(B b){
|
<B extends Integer> B m1(B b){
|
||||||
return b;
|
return b;
|
||||||
|
@ -17,7 +17,11 @@ import de.dhbwstuttgart.typeinference.result.*;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author dholle
|
||||||
|
*/
|
||||||
public class ASTToTargetAST {
|
public class ASTToTargetAST {
|
||||||
|
|
||||||
public static RefType OBJECT = ASTFactory.createObjectType(); // TODO It would be better if I could call this directly but the hashcode seems to change
|
public static RefType OBJECT = ASTFactory.createObjectType(); // TODO It would be better if I could call this directly but the hashcode seems to change
|
||||||
@ -608,16 +612,37 @@ public class ASTToTargetAST {
|
|||||||
}).collect(Collectors.toSet());
|
}).collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<TargetGeneric> convert(GenericTypeVar typeVar) {
|
||||||
|
var ret = new ArrayList<TargetGeneric>();
|
||||||
|
for (var bound : typeVar.getBounds()) {
|
||||||
|
ret.add(new TargetGeneric(typeVar.getName(), convert(bound)));
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
public TargetClass convert(ClassOrInterface input) {
|
public TargetClass convert(ClassOrInterface input) {
|
||||||
currentClass = input;
|
currentClass = input;
|
||||||
var generics = sigma.generics(input);
|
Set<TargetGeneric> generics = new HashSet<>();
|
||||||
|
var genericsIter = input.getGenerics().iterator();
|
||||||
|
if (genericsIter.hasNext()) {
|
||||||
|
// Add empty set of generics to cache so that it doesn't try to calculate it later
|
||||||
|
sigma.computedGenericsOfClasses.put(input, new HashSet<>());
|
||||||
|
while (genericsIter.hasNext()) {
|
||||||
|
var next = genericsIter.next();
|
||||||
|
generics.addAll(convert(next));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Generate generics only if there are no user defined ones
|
||||||
|
generics = convert(sigma.generics(input));
|
||||||
|
}
|
||||||
|
|
||||||
TargetBlock fieldInitializer = null;
|
TargetBlock fieldInitializer = null;
|
||||||
if (input.getfieldInitializations().isPresent())
|
if (input.getfieldInitializations().isPresent())
|
||||||
fieldInitializer = convert(input.getfieldInitializations().get().block);
|
fieldInitializer = convert(input.getfieldInitializations().get().block);
|
||||||
TargetBlock finalFieldInitializer = fieldInitializer;
|
TargetBlock finalFieldInitializer = fieldInitializer;
|
||||||
|
|
||||||
return new TargetClass(input.getModifiers(), input.getClassName().toString(), convert(input.getSuperClass()),
|
return new TargetClass(input.getModifiers(), input.getClassName().toString(), convert(input.getSuperClass()),
|
||||||
convert(generics),
|
generics,
|
||||||
input.getSuperInterfaces().stream().map(this::convert).toList(),
|
input.getSuperInterfaces().stream().map(this::convert).toList(),
|
||||||
input.getConstructors().stream().map(constructor -> this.convert(constructor, finalFieldInitializer)).flatMap(List::stream).toList(),
|
input.getConstructors().stream().map(constructor -> this.convert(constructor, finalFieldInitializer)).flatMap(List::stream).toList(),
|
||||||
input.getFieldDecl().stream().map(this::convert).toList(),
|
input.getFieldDecl().stream().map(this::convert).toList(),
|
||||||
@ -630,6 +655,20 @@ public class ASTToTargetAST {
|
|||||||
.map(param -> new MethodParameter(convert(param.getType()), param.getName())).toList();
|
.map(param -> new MethodParameter(convert(param.getType()), param.getName())).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Set<TargetGeneric> collectMethodGenerics(Set<ResultPair<?, ?>> generics, Method input) {
|
||||||
|
var convertedGenerics = new HashSet<>(convert(generics));
|
||||||
|
outer:
|
||||||
|
for (GenericTypeVar typeVar : input.getGenerics()) {
|
||||||
|
for (var classGeneric : currentClass.getGenerics()) {
|
||||||
|
if (classGeneric.equals(typeVar)) {
|
||||||
|
continue outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
convertedGenerics.addAll(convert(typeVar));
|
||||||
|
}
|
||||||
|
return convertedGenerics;
|
||||||
|
}
|
||||||
|
|
||||||
private List<TargetConstructor> convert(Constructor input, TargetBlock fieldInitializer) {
|
private List<TargetConstructor> convert(Constructor input, TargetBlock fieldInitializer) {
|
||||||
sigma = all.get(0);
|
sigma = all.get(0);
|
||||||
List<TargetConstructor> result = new ArrayList<>();
|
List<TargetConstructor> result = new ArrayList<>();
|
||||||
@ -640,7 +679,8 @@ public class ASTToTargetAST {
|
|||||||
var generics = sigma.generics(currentClass, input);
|
var generics = sigma.generics(currentClass, input);
|
||||||
List<MethodParameter> params = convert(input.getParameterList());
|
List<MethodParameter> params = convert(input.getParameterList());
|
||||||
if (parameterSet.stream().noneMatch(p -> p.equals(params))) {
|
if (parameterSet.stream().noneMatch(p -> p.equals(params))) {
|
||||||
result.add(new TargetConstructor(input.modifier, convert(generics), params, convert(input.block), fieldInitializer));
|
var convertedGenerics = collectMethodGenerics(generics, input);
|
||||||
|
result.add(new TargetConstructor(input.modifier, convertedGenerics, params, convert(input.block), fieldInitializer));
|
||||||
parameterSet.add(params);
|
parameterSet.add(params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -658,9 +698,10 @@ public class ASTToTargetAST {
|
|||||||
var generics = sigma.generics(currentClass, input);
|
var generics = sigma.generics(currentClass, input);
|
||||||
List<MethodParameter> params = convert(input.getParameterList());
|
List<MethodParameter> params = convert(input.getParameterList());
|
||||||
if (parameterSet.stream().noneMatch(p -> p.equals(params))) {
|
if (parameterSet.stream().noneMatch(p -> p.equals(params))) {
|
||||||
|
var convertedGenerics = collectMethodGenerics(generics, input);
|
||||||
result.add(new TargetMethod(
|
result.add(new TargetMethod(
|
||||||
input.modifier,
|
input.modifier,
|
||||||
input.name, convert(generics), params,
|
input.name, convertedGenerics, params,
|
||||||
convert(input.getReturnType()),
|
convert(input.getReturnType()),
|
||||||
convert(input.block)
|
convert(input.block)
|
||||||
));
|
));
|
||||||
|
@ -33,7 +33,7 @@ public class ASTToTypedTargetAST {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void overloading() throws Exception {
|
public void overloading() throws Exception {
|
||||||
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Overloading.jav").toFile();
|
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Overloading.jav").toFile();
|
||||||
var compiler = new JavaTXCompiler(file);
|
var compiler = new JavaTXCompiler(file);
|
||||||
var resultSet = compiler.typeInference();
|
var resultSet = compiler.typeInference();
|
||||||
var converter = new ASTToTargetAST(resultSet);
|
var converter = new ASTToTargetAST(resultSet);
|
||||||
@ -55,7 +55,7 @@ public class ASTToTypedTargetAST {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void tphsAndGenerics() throws Exception {
|
public void tphsAndGenerics() throws Exception {
|
||||||
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Tph2.jav").toFile();
|
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Tph2.jav").toFile();
|
||||||
var compiler = new JavaTXCompiler(file);
|
var compiler = new JavaTXCompiler(file);
|
||||||
var resultSet = compiler.typeInference();
|
var resultSet = compiler.typeInference();
|
||||||
var converter = new ASTToTargetAST(resultSet);
|
var converter = new ASTToTargetAST(resultSet);
|
||||||
@ -66,7 +66,7 @@ public class ASTToTypedTargetAST {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void cycles() throws Exception {
|
public void cycles() throws Exception {
|
||||||
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Cycle.jav").toFile();
|
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Cycle.jav").toFile();
|
||||||
var compiler = new JavaTXCompiler(file);
|
var compiler = new JavaTXCompiler(file);
|
||||||
var resultSet = compiler.typeInference();
|
var resultSet = compiler.typeInference();
|
||||||
var converter = new ASTToTargetAST(resultSet);
|
var converter = new ASTToTargetAST(resultSet);
|
||||||
@ -77,7 +77,7 @@ public class ASTToTypedTargetAST {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void infimum() throws Exception {
|
public void infimum() throws Exception {
|
||||||
var file = Path.of(System.getProperty("user.dir"), "/src/test/resources/bytecode/javFiles/Infimum.jav").toFile();
|
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Infimum.jav").toFile();
|
||||||
var compiler = new JavaTXCompiler(file);
|
var compiler = new JavaTXCompiler(file);
|
||||||
var resultSet = compiler.typeInference();
|
var resultSet = compiler.typeInference();
|
||||||
var converter = new ASTToTargetAST(resultSet);
|
var converter = new ASTToTargetAST(resultSet);
|
||||||
@ -85,4 +85,26 @@ public class ASTToTypedTargetAST {
|
|||||||
|
|
||||||
var infimum = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader());
|
var infimum = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void definedGenerics() throws Exception {
|
||||||
|
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Generics.jav").toFile();
|
||||||
|
var compiler = new JavaTXCompiler(file);
|
||||||
|
var resultSet = compiler.typeInference();
|
||||||
|
var converter = new ASTToTargetAST(resultSet);
|
||||||
|
var classes = compiler.sourceFiles.get(file).getClasses();
|
||||||
|
|
||||||
|
var generics = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void definedGenerics2() throws Exception {
|
||||||
|
var file = Path.of(System.getProperty("user.dir"), "/resources/bytecode/javFiles/Generics2.jav").toFile();
|
||||||
|
var compiler = new JavaTXCompiler(file);
|
||||||
|
var resultSet = compiler.typeInference();
|
||||||
|
var converter = new ASTToTargetAST(resultSet);
|
||||||
|
var classes = compiler.sourceFiles.get(file).getClasses();
|
||||||
|
|
||||||
|
var generics2 = TestCodegen.generateClass(converter.convert(classes.get(0)), new ByteArrayClassLoader());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -226,7 +226,6 @@ public class TestGenerics {
|
|||||||
@Ignore("This doesn't work properly")
|
@Ignore("This doesn't work properly")
|
||||||
public void testThreeArgs() throws Exception {
|
public void testThreeArgs() throws Exception {
|
||||||
var result = computeGenerics("TestThreeArgs.jav");
|
var result = computeGenerics("TestThreeArgs.jav");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
Reference in New Issue
Block a user