Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20b974fd2a | ||
|
|
a504743a40 | ||
|
|
70764640d6 | ||
|
|
090263b6ca |
@@ -7,7 +7,6 @@ import java.lang.Integer;
|
|||||||
import java.lang.String;
|
import java.lang.String;
|
||||||
import java.lang.System;
|
import java.lang.System;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
public sealed interface LazyList permits Empty, Cons {
|
public sealed interface LazyList permits Empty, Cons {
|
||||||
public Integer fst();
|
public Integer fst();
|
||||||
@@ -15,11 +14,11 @@ public sealed interface LazyList permits Empty, Cons {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Der Konstruktor Cons muss lazy sein, deshalb hier Supplier<...>
|
//Der Konstruktor Cons muss lazy sein, deshalb hier Supplier<...>
|
||||||
record Cons(Integer x, Supplier<LazyList> l) implements LazyList {
|
record Cons(Integer x, Fun0$$<LazyList> l) implements LazyList {
|
||||||
public Integer fst() { return this.x; }
|
public Integer fst() { return this.x; }
|
||||||
public LazyList rest() { return this.l.get(); }
|
public LazyList rest() { return this.l.apply(); }
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Cons(" + this.x.toString() + ", " + this.l.get().toString() + ")";
|
return "Cons(" + this.x.toString() + ", " + this.l.apply().toString() + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,23 +3,29 @@ import java.lang.String;
|
|||||||
import java.lang.System;
|
import java.lang.System;
|
||||||
import java.lang.Boolean;
|
import java.lang.Boolean;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.util.function.Supplier;
|
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
import LazyList;
|
import LazyList;
|
||||||
import Cons;
|
import Cons;
|
||||||
import Empty;
|
import Empty;
|
||||||
|
|
||||||
public class Primzahlen {
|
public class Primzahlen {
|
||||||
static LazyList from(int i) { return new Cons(i, () -> from(i+1)); }
|
static LazyList from(Integer i) { return new Cons(i, () -> from(i+1)); }
|
||||||
|
|
||||||
LazyList filter(Function<Integer, Boolean> p, LazyList l) {
|
LazyList filter(Fun1$$<Integer, Boolean> p, LazyList l) {
|
||||||
return switch (l) {
|
return switch(l) {
|
||||||
case Empty() -> l;
|
case Empty e -> e;
|
||||||
case Cons(Integer x, Supplier<LazyList> l1) ->
|
case Cons(Integer x, Fun0$$<LazyList> l1) ->
|
||||||
p.apply(x) ? new Cons(x, () -> filter(p, l1.get())) : filter(p, l1.get());
|
p.apply(x) ? new Cons(x, () -> filter(p, l1.apply())) : filter(p, l1.apply());
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
|
/*LazyList filter(Fun1$$<Integer, Boolean> p, Empty()@l) {
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
LazyList filter(Fun1$$<Integer, Boolean> p, Cons(Integer x, Fun0$$<LazyList> l1)) {
|
||||||
|
return p.apply(x) ? new Cons(x, () -> filter(p, l1.apply())) : filter(p, l1.apply());
|
||||||
|
}*/
|
||||||
|
|
||||||
LazyList dropMul(Integer x, LazyList xs) {
|
LazyList dropMul(Integer x, LazyList xs) {
|
||||||
return filter(y -> (y % x != 0), xs);
|
return filter(y -> (y % x != 0), xs);
|
||||||
@@ -37,8 +43,8 @@ public class Primzahlen {
|
|||||||
if (n == 0) return new Empty();
|
if (n == 0) return new Empty();
|
||||||
else return switch (l) {
|
else return switch (l) {
|
||||||
case Empty() -> l;
|
case Empty() -> l;
|
||||||
case Cons(Integer x, Supplier<LazyList> l1) ->
|
case Cons(Integer x, Fun0$$<LazyList> l1) ->
|
||||||
new Cons(x, () -> take(n-1, l1.get()));
|
new Cons(x, () -> take(n-1, l1.apply()));
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import java.lang.Integer;
|
||||||
|
|
||||||
|
public record RecordTestInstanceVariables(Integer a, Integer b) {
|
||||||
|
public Integer getA() { return a; }
|
||||||
|
public Integer getB() { return b; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
public class StaticFail {
|
||||||
|
void nonStaticM() {}
|
||||||
|
|
||||||
|
static void staticM() {
|
||||||
|
nonStaticM();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1189,6 +1189,12 @@ public class Codegen {
|
|||||||
}
|
}
|
||||||
case TargetMethodCall call: {
|
case TargetMethodCall call: {
|
||||||
if (!call.isStatic()) {
|
if (!call.isStatic()) {
|
||||||
|
if (call.expr() instanceof TargetThis && state.isStatic) {
|
||||||
|
throw new CodeGenException(
|
||||||
|
"Attempted to call instance method " + call.name() +
|
||||||
|
" with descriptor " + call.getDescriptor() + " from a static context"
|
||||||
|
);
|
||||||
|
}
|
||||||
generate(state, call.expr());
|
generate(state, call.expr());
|
||||||
boxPrimitive(state, call.expr().type());
|
boxPrimitive(state, call.expr().type());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -714,6 +714,9 @@ public class JavaTXCompiler {
|
|||||||
new SuperWildcardType(toRefType(targetSuperWildcard.innerType()), new NullToken());
|
new SuperWildcardType(toRefType(targetSuperWildcard.innerType()), new NullToken());
|
||||||
case TargetGenericType targetGenericType -> new GenericRefType(targetGenericType.name(), new NullToken());
|
case TargetGenericType targetGenericType -> new GenericRefType(targetGenericType.name(), new NullToken());
|
||||||
case TargetPrimitiveType targetPrimitiveType -> toRefType(TargetType.toWrapper(targetPrimitiveType));
|
case TargetPrimitiveType targetPrimitiveType -> toRefType(TargetType.toWrapper(targetPrimitiveType));
|
||||||
|
case TargetFunNType targetFunNType ->
|
||||||
|
new RefType(new JavaClassName(FunNGenerator.getSuperClassName(targetFunNType.funNParams().size() - 1, targetFunNType.returnArguments())),
|
||||||
|
targetFunNType.funNParams().stream().map(JavaTXCompiler::toRefType).toList(), new NullToken());
|
||||||
case TargetSpecializedType targetSpecializedType ->
|
case TargetSpecializedType targetSpecializedType ->
|
||||||
new RefType(new JavaClassName(targetSpecializedType.name()),
|
new RefType(new JavaClassName(targetSpecializedType.name()),
|
||||||
targetSpecializedType.params().stream().map(JavaTXCompiler::toRefType).toList(), new NullToken()
|
targetSpecializedType.params().stream().map(JavaTXCompiler::toRefType).toList(), new NullToken()
|
||||||
|
|||||||
+20
-16
@@ -80,7 +80,7 @@ public class SyntaxTreeGenerator {
|
|||||||
HashMap<String, Integer> allmodifiers = new HashMap<>();
|
HashMap<String, Integer> allmodifiers = new HashMap<>();
|
||||||
// PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH
|
// PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH
|
||||||
// bekommen
|
// bekommen
|
||||||
private final Map<String, FieldEntry> fields = new HashMap<>();
|
//private final Map<String, FieldEntry> fields = new HashMap<>();
|
||||||
// PL 2019-10-23: Muss für jede Klasse neu initilisiert werden
|
// PL 2019-10-23: Muss für jede Klasse neu initilisiert werden
|
||||||
List<Statement> fieldInitializations = new ArrayList<>();
|
List<Statement> fieldInitializations = new ArrayList<>();
|
||||||
List<Statement> staticFieldInitializations = new ArrayList<>();
|
List<Statement> staticFieldInitializations = new ArrayList<>();
|
||||||
@@ -178,6 +178,7 @@ public class SyntaxTreeGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ClassOrInterface convertClass(Java17Parser.ClassDeclarationContext ctx, int modifiers) {
|
private ClassOrInterface convertClass(Java17Parser.ClassDeclarationContext ctx, int modifiers) {
|
||||||
|
var fieldDecls = new HashMap<String, FieldEntry>();
|
||||||
String className = this.pkgName + (this.pkgName.length() > 0 ? "." : "") + ctx.identifier().getText();
|
String className = this.pkgName + (this.pkgName.length() > 0 ? "." : "") + ctx.identifier().getText();
|
||||||
JavaClassName name = reg.getName(className); // Holt den Package Namen mit dazu
|
JavaClassName name = reg.getName(className); // Holt den Package Namen mit dazu
|
||||||
if (!name.toString().equals(className)) { // Kommt die Klasse schon in einem anderen Package vor?
|
if (!name.toString().equals(className)) { // Kommt die Klasse schon in einem anderen Package vor?
|
||||||
@@ -206,7 +207,7 @@ public class SyntaxTreeGenerator {
|
|||||||
List<RefType> implementedInterfaces = new ArrayList<>();
|
List<RefType> implementedInterfaces = new ArrayList<>();
|
||||||
List<RefType> permittedSubtypes = null;
|
List<RefType> permittedSubtypes = null;
|
||||||
for (ClassBodyDeclarationContext clsbodydecl : ctx.classBody().classBodyDeclaration()) {
|
for (ClassBodyDeclarationContext clsbodydecl : ctx.classBody().classBodyDeclaration()) {
|
||||||
convert(clsbodydecl, fielddecl, constructors, methods, name, superClass, generics);
|
convert(clsbodydecl, fielddecl, constructors, methods, name, superClass, generics, fieldDecls);
|
||||||
}
|
}
|
||||||
if (constructors.isEmpty()) {
|
if (constructors.isEmpty()) {
|
||||||
constructors.add(generateStandardConstructor(ctx.identifier().getText(), name, superClass, genericClassParameters, offset));
|
constructors.add(generateStandardConstructor(ctx.identifier().getText(), name, superClass, genericClassParameters, offset));
|
||||||
@@ -255,6 +256,8 @@ public class SyntaxTreeGenerator {
|
|||||||
List<Pattern> constructorParameters = new ArrayList<>();
|
List<Pattern> constructorParameters = new ArrayList<>();
|
||||||
List<Statement> constructorStatements = new ArrayList<>();
|
List<Statement> constructorStatements = new ArrayList<>();
|
||||||
|
|
||||||
|
var fieldDecl = new HashMap<String, FieldEntry>();
|
||||||
|
|
||||||
List<Java17Parser.RecordComponentContext> components = recordDeclaration.recordHeader().recordComponentList() != null ?
|
List<Java17Parser.RecordComponentContext> components = recordDeclaration.recordHeader().recordComponentList() != null ?
|
||||||
recordDeclaration.recordHeader().recordComponentList().recordComponent(): List.of();
|
recordDeclaration.recordHeader().recordComponentList().recordComponent(): List.of();
|
||||||
for (RecordComponentContext component : components) {
|
for (RecordComponentContext component : components) {
|
||||||
@@ -270,6 +273,7 @@ public class SyntaxTreeGenerator {
|
|||||||
fielddecl.add(new Field(fieldname, fieldtype, fieldmodifiers, fieldoffset));
|
fielddecl.add(new Field(fieldname, fieldtype, fieldmodifiers, fieldoffset));
|
||||||
constructorParameters.add(new FormalParameter(fieldname, fieldtype, fieldoffset));
|
constructorParameters.add(new FormalParameter(fieldname, fieldtype, fieldoffset));
|
||||||
FieldVar fieldvar = new FieldVar(new This(offset), fieldname, fieldtype, fieldoffset);
|
FieldVar fieldvar = new FieldVar(new This(offset), fieldname, fieldtype, fieldoffset);
|
||||||
|
fieldDecl.put(fieldname, new FieldEntry(fieldname, fieldtype, Modifier.PRIVATE));
|
||||||
constructorStatements.add(new Assign(new AssignToField(fieldvar), new LocalVar(fieldname, fieldtype, fieldoffset), offset));
|
constructorStatements.add(new Assign(new AssignToField(fieldvar), new LocalVar(fieldname, fieldtype, fieldoffset), offset));
|
||||||
Statement returnStatement = new Return(fieldvar, offset);
|
Statement returnStatement = new Return(fieldvar, offset);
|
||||||
methods.add(new Method(allmodifiers.get("public"), fieldname, fieldtype, new ParameterList(new ArrayList<>(), offset), new Block(Arrays.asList(returnStatement), offset), new GenericDeclarationList(new ArrayList<>(), offset), offset));
|
methods.add(new Method(allmodifiers.get("public"), fieldname, fieldtype, new ParameterList(new ArrayList<>(), offset), new Block(Arrays.asList(returnStatement), offset), new GenericDeclarationList(new ArrayList<>(), offset), offset));
|
||||||
@@ -279,7 +283,7 @@ public class SyntaxTreeGenerator {
|
|||||||
//Optional<Constructor> initializations = Optional.of(implicitConstructor);
|
//Optional<Constructor> initializations = Optional.of(implicitConstructor);
|
||||||
constructors.add(implicitConstructor);
|
constructors.add(implicitConstructor);
|
||||||
for (ClassBodyDeclarationContext bodyDeclaration : recordDeclaration.recordBody().classBodyDeclaration()) {
|
for (ClassBodyDeclarationContext bodyDeclaration : recordDeclaration.recordBody().classBodyDeclaration()) {
|
||||||
convert(bodyDeclaration, fielddecl, constructors, methods, name, superClass, generics);
|
convert(bodyDeclaration, fielddecl, constructors, methods, name, superClass, generics, fieldDecl);
|
||||||
}
|
}
|
||||||
if (!Objects.isNull(recordDeclaration.IMPLEMENTS())) {
|
if (!Objects.isNull(recordDeclaration.IMPLEMENTS())) {
|
||||||
implementedInterfaces.addAll(convert(recordDeclaration.typeList(), generics));
|
implementedInterfaces.addAll(convert(recordDeclaration.typeList(), generics));
|
||||||
@@ -288,7 +292,7 @@ public class SyntaxTreeGenerator {
|
|||||||
return new Record(modifiers, name, fielddecl, Optional.empty(), staticCtor, methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset, fileName);
|
return new Record(modifiers, name, fielddecl, Optional.empty(), staticCtor, methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void convert(ClassBodyDeclarationContext classBody, List<Field> fields, List<Constructor> constructors, List<Method> methods, JavaClassName name, RefType superClass, GenericsRegistry generics) {
|
private void convert(ClassBodyDeclarationContext classBody, List<Field> fields, List<Constructor> constructors, List<Method> methods, JavaClassName name, RefType superClass, GenericsRegistry generics, HashMap<String, FieldEntry> fieldDecls) {
|
||||||
MemberdeclContext member;
|
MemberdeclContext member;
|
||||||
if (classBody instanceof MemberdeclContext) {
|
if (classBody instanceof MemberdeclContext) {
|
||||||
member = (MemberdeclContext) classBody;
|
member = (MemberdeclContext) classBody;
|
||||||
@@ -303,11 +307,11 @@ public class SyntaxTreeGenerator {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case MemberfieldContext memberfield: {
|
case MemberfieldContext memberfield: {
|
||||||
fields.addAll(convert(memberfield.fieldDeclaration(), membermodifiers, generics));
|
fields.addAll(convert(memberfield.fieldDeclaration(), membermodifiers, generics, fieldDecls));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case MembermethodContext membermethod: {
|
case MembermethodContext membermethod: {
|
||||||
Method convertedMethod = convert(membermodifiers, membermethod.method(), name, superClass, generics);
|
Method convertedMethod = convert(membermodifiers, membermethod.method(), name, superClass, generics, fieldDecls);
|
||||||
if (convertedMethod instanceof Constructor constructor) {
|
if (convertedMethod instanceof Constructor constructor) {
|
||||||
constructors.add(constructor);
|
constructors.add(constructor);
|
||||||
} else {
|
} else {
|
||||||
@@ -316,7 +320,7 @@ public class SyntaxTreeGenerator {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case MemberconstructorContext memberconstructor: {
|
case MemberconstructorContext memberconstructor: {
|
||||||
constructors.add(convert(membermodifiers, memberconstructor.constructor(), name, superClass, generics));
|
constructors.add(convert(membermodifiers, memberconstructor.constructor(), name, superClass, generics, fieldDecls));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@@ -324,7 +328,7 @@ public class SyntaxTreeGenerator {
|
|||||||
}
|
}
|
||||||
} else if (classBody instanceof Java17Parser.ClassblockContext ctx && ctx.STATIC() != null) {
|
} else if (classBody instanceof Java17Parser.ClassblockContext ctx && ctx.STATIC() != null) {
|
||||||
// Static blocks
|
// Static blocks
|
||||||
var stmtgen = new StatementGenerator(superClass, compiler, reg, generics, this.fields, new HashMap<>());
|
var stmtgen = new StatementGenerator(superClass, compiler, reg, generics, fieldDecls, new HashMap<>());
|
||||||
var block = stmtgen.convert(((Java17Parser.ClassblockContext) classBody).block(), false);
|
var block = stmtgen.convert(((Java17Parser.ClassblockContext) classBody).block(), false);
|
||||||
staticFieldInitializations.addAll(block.statements);
|
staticFieldInitializations.addAll(block.statements);
|
||||||
}
|
}
|
||||||
@@ -434,7 +438,7 @@ public class SyntaxTreeGenerator {
|
|||||||
retType = new Void(bodydeclaration.refType().getStart());
|
retType = new Void(bodydeclaration.refType().getStart());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatementGenerator stmtgen = new StatementGenerator(superClass, compiler, reg, generics, fields, new HashMap<>());
|
StatementGenerator stmtgen = new StatementGenerator(superClass, compiler, reg, generics, new HashMap<>(), new HashMap<>());
|
||||||
ParameterList paramlist = stmtgen.convert(bodydeclaration.formalParameters().formalParameterList(), true);
|
ParameterList paramlist = stmtgen.convert(bodydeclaration.formalParameters().formalParameterList(), true);
|
||||||
MethodBodyContext body = bodydeclaration.methodBody();
|
MethodBodyContext body = bodydeclaration.methodBody();
|
||||||
Block block = null;
|
Block block = null;
|
||||||
@@ -507,7 +511,7 @@ public class SyntaxTreeGenerator {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Method convert(int modifiers, Java17Parser.MethodContext methodContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) {
|
public Method convert(int modifiers, Java17Parser.MethodContext methodContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics, Map<String, FieldEntry> fieldDecls) {
|
||||||
GenericsRegistry localgenerics = generics;
|
GenericsRegistry localgenerics = generics;
|
||||||
MethodDeclarationContext methoddeclaration;
|
MethodDeclarationContext methoddeclaration;
|
||||||
GenericDeclarationListContext genericdeclarations;
|
GenericDeclarationListContext genericdeclarations;
|
||||||
@@ -540,7 +544,7 @@ public class SyntaxTreeGenerator {
|
|||||||
retType = new Void(header.refType().getStart());
|
retType = new Void(header.refType().getStart());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
StatementGenerator stmtgen = new StatementGenerator(superClass, compiler, reg, localgenerics, fields, new HashMap<>());
|
StatementGenerator stmtgen = new StatementGenerator(superClass, compiler, reg, localgenerics, fieldDecls, new HashMap<>());
|
||||||
ParameterList paramlist = stmtgen.convert(header.formalParameters().formalParameterList(), true);
|
ParameterList paramlist = stmtgen.convert(header.formalParameters().formalParameterList(), true);
|
||||||
MethodBodyContext body = methoddeclaration.methodBody();
|
MethodBodyContext body = methoddeclaration.methodBody();
|
||||||
Block block = null;
|
Block block = null;
|
||||||
@@ -559,7 +563,7 @@ public class SyntaxTreeGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Constructor convert(int modifiers, Java17Parser.ConstructorContext constructorContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) {
|
public Constructor convert(int modifiers, Java17Parser.ConstructorContext constructorContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics, Map<String, FieldEntry> fields) {
|
||||||
GenericsRegistry localgenerics = generics;
|
GenericsRegistry localgenerics = generics;
|
||||||
GenericDeclarationListContext genericdeclarations;
|
GenericDeclarationListContext genericdeclarations;
|
||||||
GenericDeclarationList gtvDeclarations;
|
GenericDeclarationList gtvDeclarations;
|
||||||
@@ -586,7 +590,7 @@ public class SyntaxTreeGenerator {
|
|||||||
return new Constructor(modifiers, name, retType, paramlist, block, gtvDeclarations, constructordeclaration.getStart());
|
return new Constructor(modifiers, name, retType, paramlist, block, gtvDeclarations, constructordeclaration.getStart());
|
||||||
}
|
}
|
||||||
|
|
||||||
List<? extends Field> convert(Java17Parser.FieldDeclarationContext fieldDeclContext, int modifiers, GenericsRegistry generics) {
|
List<? extends Field> convert(Java17Parser.FieldDeclarationContext fieldDeclContext, int modifiers, GenericsRegistry generics, HashMap<String, FieldEntry> fields) {
|
||||||
List<Field> ret = new ArrayList<>();
|
List<Field> ret = new ArrayList<>();
|
||||||
RefTypeOrTPHOrWildcardOrGeneric fieldType;
|
RefTypeOrTPHOrWildcardOrGeneric fieldType;
|
||||||
if (fieldDeclContext.typeType() != null) {
|
if (fieldDeclContext.typeType() != null) {
|
||||||
@@ -598,9 +602,9 @@ public class SyntaxTreeGenerator {
|
|||||||
}
|
}
|
||||||
for (Java17Parser.VariableDeclaratorContext varDecl : fieldDeclContext.variableDeclarators().variableDeclarator()) {
|
for (Java17Parser.VariableDeclaratorContext varDecl : fieldDeclContext.variableDeclarators().variableDeclarator()) {
|
||||||
String fieldName = varDecl.variableDeclaratorId().getText();
|
String fieldName = varDecl.variableDeclaratorId().getText();
|
||||||
this.fields.put(fieldName, new FieldEntry(fieldName, fieldType, modifiers));
|
fields.put(fieldName, new FieldEntry(fieldName, fieldType, modifiers));
|
||||||
if (varDecl.variableInitializer() != null) {
|
if (varDecl.variableInitializer() != null) {
|
||||||
initializeField(varDecl, Modifier.isStatic(modifiers), fieldType, generics);
|
initializeField(varDecl, Modifier.isStatic(modifiers), fieldType, generics, fields);
|
||||||
}
|
}
|
||||||
ret.add(new Field(fieldName, fieldType, modifiers, varDecl.getStart()));
|
ret.add(new Field(fieldName, fieldType, modifiers, varDecl.getStart()));
|
||||||
}
|
}
|
||||||
@@ -612,7 +616,7 @@ public class SyntaxTreeGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initialize a field by creating implicit constructor.
|
// Initialize a field by creating implicit constructor.
|
||||||
private void initializeField(Java17Parser.VariableDeclaratorContext ctx, boolean isStatic, RefTypeOrTPHOrWildcardOrGeneric typeOfField, GenericsRegistry generics) {
|
private void initializeField(Java17Parser.VariableDeclaratorContext ctx, boolean isStatic, RefTypeOrTPHOrWildcardOrGeneric typeOfField, GenericsRegistry generics, Map<String, FieldEntry> fields) {
|
||||||
StatementGenerator statementGenerator = new StatementGenerator(superClass, compiler, reg, generics, fields, new HashMap<>());
|
StatementGenerator statementGenerator = new StatementGenerator(superClass, compiler, reg, generics, fields, new HashMap<>());
|
||||||
var assignment = statementGenerator.generateFieldAssignment(ctx, typeOfField);
|
var assignment = statementGenerator.generateFieldAssignment(ctx, typeOfField);
|
||||||
if (isStatic) {
|
if (isStatic) {
|
||||||
|
|||||||
@@ -80,8 +80,8 @@ public class ASTToTargetAST {
|
|||||||
this(new JavaGenerics(compiler, set), new TxGenerics(compiler, set));
|
this(new JavaGenerics(compiler, set), new TxGenerics(compiler, set));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Generics nullGenerics() {
|
public static Generics nullGenerics(JavaTXCompiler compiler) {
|
||||||
return new Generics(null, new ResultSet(Set.of()));
|
return new Generics(compiler, new ResultSet(Set.of()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +113,7 @@ public class ASTToTargetAST {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Optional<Method> findMethod(ClassOrInterface owner, String name, List<TargetType> argumentList, JavaTXCompiler compiler) {
|
public static Optional<Method> findMethod(ClassOrInterface owner, String name, List<TargetType> argumentList, JavaTXCompiler compiler) {
|
||||||
return findMethod(owner, name, argumentList, Generics.nullGenerics().javaGenerics(), compiler);
|
return findMethod(owner, name, argumentList, Generics.nullGenerics(compiler).javaGenerics(), compiler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Optional<Method> findMethod(ClassOrInterface owner, String name, List<TargetType> argumentList, IGenerics generics, JavaTXCompiler compiler) {
|
public static Optional<Method> findMethod(ClassOrInterface owner, String name, List<TargetType> argumentList, IGenerics generics, JavaTXCompiler compiler) {
|
||||||
@@ -483,7 +483,7 @@ public class ASTToTargetAST {
|
|||||||
var res = new ArrayList<MethodParameter>();
|
var res = new ArrayList<MethodParameter>();
|
||||||
for (var i = 0; i < input.getFormalparalist().size(); i++) {
|
for (var i = 0; i < input.getFormalparalist().size(); i++) {
|
||||||
var param = input.getFormalparalist().get(i);
|
var param = input.getFormalparalist().get(i);
|
||||||
var pattern = (TargetPattern) convert(param, Generics.nullGenerics().javaGenerics);
|
var pattern = (TargetPattern) convert(param, Generics.nullGenerics(compiler).javaGenerics);
|
||||||
if (pattern instanceof TargetComplexPattern) pattern = pattern.withName("__var" + i);
|
if (pattern instanceof TargetComplexPattern) pattern = pattern.withName("__var" + i);
|
||||||
res.add(new MethodParameter(pattern));
|
res.add(new MethodParameter(pattern));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
|
|
||||||
|
import de.dhbwstuttgart.bytecode.CodeGenException;
|
||||||
import de.dhbwstuttgart.core.ConsoleInterface;
|
import de.dhbwstuttgart.core.ConsoleInterface;
|
||||||
import de.dhbwstuttgart.util.Logger;
|
import de.dhbwstuttgart.util.Logger;
|
||||||
import de.dhbwstuttgart.util.Logger.LogLevel;
|
import de.dhbwstuttgart.util.Logger.LogLevel;
|
||||||
@@ -674,6 +675,15 @@ public class TestComplete {
|
|||||||
System.out.println(clazz.getDeclaredMethod("toString").invoke(instance));
|
System.out.println(clazz.getDeclaredMethod("toString").invoke(instance));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void recordTestInstanceVariables() throws Exception {
|
||||||
|
var classFiles = generateClassFiles(createClassLoader(), "RecordTestInstanceVariables.jav");
|
||||||
|
var clazz = classFiles.get("RecordTestInstanceVariables");
|
||||||
|
var instance = clazz.getDeclaredConstructor(Integer.class, Integer.class).newInstance(10, 20);
|
||||||
|
assertEquals(10, clazz.getDeclaredMethod("getA").invoke(instance));
|
||||||
|
assertEquals(20, clazz.getDeclaredMethod("getB").invoke(instance));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void genericRecordTest() throws Exception {
|
public void genericRecordTest() throws Exception {
|
||||||
var classFiles = generateClassFiles(createClassLoader(), "GenericRecord.jav");
|
var classFiles = generateClassFiles(createClassLoader(), "GenericRecord.jav");
|
||||||
@@ -1084,6 +1094,18 @@ public class TestComplete {
|
|||||||
assertEquals(50, m.invoke(null));
|
assertEquals(50, m.invoke(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStaticFail() throws Exception {
|
||||||
|
try {
|
||||||
|
generateClassFiles(createClassLoader(), "StaticFail.jav");
|
||||||
|
fail("No exception thrown!");
|
||||||
|
} catch (CodeGenException e) {
|
||||||
|
assertEquals("Attempted to call instance method nonStaticM with descriptor ()V from a static context", e.getMessage());
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
fail("Wrong exception thrown!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFor() throws Exception {
|
public void testFor() throws Exception {
|
||||||
var classFiles = generateClassFiles(createClassLoader(), "For.jav");
|
var classFiles = generateClassFiles(createClassLoader(), "For.jav");
|
||||||
|
|||||||
Reference in New Issue
Block a user