Beginn anpassung SyntaxTreeGenerator an neue Grammatik

This commit is contained in:
luca9913 2023-01-17 06:37:07 +01:00
parent 6d52949215
commit cb564dc436
2 changed files with 377 additions and 309 deletions

View File

@ -7,6 +7,10 @@ import java.lang.ClassNotFoundException;
import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java17Parser; import de.dhbwstuttgart.parser.antlr.Java17Parser;
import de.dhbwstuttgart.parser.antlr.Java17ParserBaseVisitor;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacedeclContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SrcfileContext;
import de.dhbwstuttgart.parser.scope.GatherNames; import de.dhbwstuttgart.parser.scope.GatherNames;
import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
@ -27,66 +31,91 @@ import java.util.stream.Collectors;
//import jdk.internal.dynalink.support.TypeConverterFactory; //import jdk.internal.dynalink.support.TypeConverterFactory;
import org.antlr.v4.runtime.CommonToken; import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode; import org.antlr.v4.runtime.tree.TerminalNode;
public class SyntaxTreeGenerator{ public class SyntaxTreeGenerator extends Java17ParserBaseVisitor<SourceFile> {
private JavaClassRegistry reg; private JavaClassRegistry reg;
private final GenericsRegistry globalGenerics; private final GenericsRegistry globalGenerics;
private String pkgName = ""; private String pkgName = "";
Set<JavaClassName> imports = new HashSet(); Set<JavaClassName> imports = new HashSet<>();
private Map<String, RefTypeOrTPHOrWildcardOrGeneric> fields = new HashMap<>(); //PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH bekommen private Map<String, RefTypeOrTPHOrWildcardOrGeneric> fields = new HashMap<>(); // PL 2018-11-01 fields eingefuegt,
// damit die fields immer die gleiche
// TPH bekommen
List<Statement> fieldInitializations = new ArrayList<>(); //PL 2019-10-23: Muss für jede Klasse neu initilisiert werden List<Statement> fieldInitializations = new ArrayList<>(); // PL 2019-10-23: Muss für jede Klasse neu initilisiert
// werden
public SyntaxTreeGenerator(JavaClassRegistry reg, GenericsRegistry globalGenerics){ public SyntaxTreeGenerator(JavaClassRegistry reg, GenericsRegistry globalGenerics) {
//Die Generics müssen während des Bauens des AST erstellt werden, // Die Generics müssen während des Bauens des AST erstellt werden,
// da diese mit der Methode oder Klasse, in welcher sie deklariert werden // da diese mit der Methode oder Klasse, in welcher sie deklariert werden
// verknüpft sein müssen. Dennoch werden die Namen aller Generics in einer globalen Datenbank benötigt. // verknüpft sein müssen. Dennoch werden die Namen aller Generics in einer
// globalen Datenbank benötigt.
this.globalGenerics = globalGenerics; this.globalGenerics = globalGenerics;
this.reg = reg; this.reg = reg;
} }
public JavaClassRegistry getReg() {
public JavaClassRegistry getReg(){
return this.reg; return this.reg;
} }
// Converts type name to String.
public String convertTypeName(Java17Parser.TypeNameContext ctx){
String ret;
if(ctx.packageOrTypeName() == null){
ret = ctx.Identifier().toString();
}
else{
ret = convertPackageOrTypeName(ctx.packageOrTypeName()) + "." + ctx.Identifier().toString();
}
return ret;
}
// Converts PackageOrTypeName to String. // Converts PackageOrTypeName to String.
public String convertPackageOrTypeName(Java17Parser.PackageOrTypeNameContext ctx){ /*
String ret; * Package und Type Namen sind qualified Names und werden durch nachfolgende
if(ctx.packageOrTypeName() == null){ * Methode behandelt. Diese Methode sollte damit überflüssig sein.
ret = ctx.Identifier().toString(); *
* public String convertPackageOrTypeName(Java17Parser.PackageOrTypeNameContext
* ctx) {
* String ret;
* if (ctx.packageOrTypeName() == null) {
* ret = ctx.Identifier().toString();
* } else {
* ret = convertPackageOrTypeName(ctx.packageOrTypeName()) + "." +
* ctx.Identifier().toString();
* }
* return ret;
* }
*/
public String convertQualifiedName(Java17Parser.QualifiedNameContext ctx) {
String ret = "";
for (Java17Parser.IdentifierContext ident : ctx.identifier()) {
ret += ident.getText();
if (ctx.identifier().iterator().hasNext()) {
ret += '.';
} }
else{
ret = convertPackageOrTypeName(ctx.packageOrTypeName()) + "." + ctx.Identifier().toString();
} }
return ret; return ret;
} }
public SourceFile convert(Java17Parser.CompilationUnitContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader) throws ClassNotFoundException{ public SourceFile convert(Java17Parser.SourceFileContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader)
if(ctx.packageDeclaration()!=null)this.pkgName = convert(ctx.packageDeclaration()); throws ClassNotFoundException {
SrcfileContext srcfile = new SrcfileContext(ctx);
if (srcfile.packageDeclaration() != null)
this.pkgName = convert(srcfile.packageDeclaration());
List<ClassOrInterface> classes = new ArrayList<>(); List<ClassOrInterface> classes = new ArrayList<>();
Map<String, Integer> imports = GatherNames.getImports(ctx, packageCrawler, classLoader); Map<String, Integer> imports = GatherNames.getImports(ctx, packageCrawler, classLoader);
this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet()); this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet());
for(Java17Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ for (Java17Parser.ClassOrInterfaceContext member : srcfile.classOrInterface()) {
ClassOrInterface newClass; ClassorinterfacedeclContext clsoif;
if(typeDecl.classDeclaration() != null){ if (member instanceof NoclassorinterfaceContext) {
newClass = convertClass(typeDecl.classDeclaration()); continue;
} else {
clsoif = new ClassorinterfacedeclContext(member);
} }
else{ ClassOrInterface newClass;
newClass = convertInterface(typeDecl.interfaceDeclaration()); int modifiers = 0;
if (!clsoif.classOrInterfaceModifier().isEmpty()) {
for (Java17Parser.ClassOrInterfaceModifierContext mod : clsoif.classOrInterfaceModifier()) {
int newModifier = convert(mod);
modifiers += newModifier;
}
}
fieldInitializations = new ArrayList<>(); // PL 2019-10-22: muss für jede Klasse neu initilisiert werden
if (clsoif.classDeclaration() != null) {
newClass = convertClass(clsoif.classDeclaration());
} else {
newClass = convertInterface(clsoif.interfaceDeclaration());
} }
classes.add(newClass); classes.add(newClass);
} }
@ -94,31 +123,29 @@ public class SyntaxTreeGenerator{
} }
private String convert(Java17Parser.PackageDeclarationContext packageDeclarationContext) { private String convert(Java17Parser.PackageDeclarationContext packageDeclarationContext) {
String ret = ""; return convertQualifiedName(packageDeclarationContext.qualifiedName());
for(TerminalNode identifier : packageDeclarationContext.Identifier()){
ret += identifier.getText()+".";
}
ret = ret.substring(0, ret.length()-1);
return ret;
} }
public Method convert(Java17Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { public Method convert(Java17Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass,
RefType superClass, GenericsRegistry generics) {
Java17Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader(); Java17Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader();
int modifiers = SyntaxTreeGenerator.convert(methodDeclarationContext.methodModifier()); int modifiers = SyntaxTreeGenerator.convert(methodDeclarationContext.methodModifier());
GenericsRegistry localGenerics = createGenerics(methodDeclarationContext.methodHeader().typeParameters(), GenericsRegistry localGenerics = createGenerics(methodDeclarationContext.methodHeader().typeParameters(),
parentClass, header.methodDeclarator().Identifier().getText(), reg, generics); parentClass, header.methodDeclarator().Identifier().getText(), reg, generics);
localGenerics.putAll(generics); localGenerics.putAll(generics);
return convert(modifiers, header, methodDeclarationContext.methodBody(),parentClass, superClass, localGenerics); return convert(modifiers, header, methodDeclarationContext.methodBody(), parentClass, superClass, localGenerics);
} }
public Method convert(Java17Parser.InterfaceMethodDeclarationContext ctx, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { public Method convert(Java17Parser.InterfaceMethodDeclarationContext ctx, JavaClassName parentClass,
RefType superClass, GenericsRegistry generics) {
Java17Parser.MethodHeaderContext header = ctx.methodHeader(); Java17Parser.MethodHeaderContext header = ctx.methodHeader();
int modifiers = SyntaxTreeGenerator.convertInterfaceModifier(ctx.interfaceMethodModifier()); int modifiers = SyntaxTreeGenerator.convertInterfaceModifier(ctx.interfaceMethodModifier());
GenericsRegistry localGenerics = createGenerics(header.typeParameters(), parentClass, header.methodDeclarator().Identifier().getText(), reg, generics); GenericsRegistry localGenerics = createGenerics(header.typeParameters(), parentClass,
header.methodDeclarator().Identifier().getText(), reg, generics);
localGenerics.putAll(generics); localGenerics.putAll(generics);
return convert(modifiers, header, ctx.methodBody(),parentClass, superClass, localGenerics); return convert(modifiers, header, ctx.methodBody(), parentClass, superClass, localGenerics);
} }
private Method convert(int modifiers, Java17Parser.MethodHeaderContext header, Java17Parser.MethodBodyContext body, private Method convert(int modifiers, Java17Parser.MethodHeaderContext header, Java17Parser.MethodBodyContext body,
@ -128,141 +155,140 @@ public class SyntaxTreeGenerator{
String name = header.methodDeclarator().Identifier().getText(); String name = header.methodDeclarator().Identifier().getText();
GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), header.getStart()); GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), header.getStart());
if(header.typeParameters() != null){ if (header.typeParameters() != null) {
gtvDeclarations = TypeGenerator.convert(header.typeParameters(), parentClass, name, reg, localGenerics); gtvDeclarations = TypeGenerator.convert(header.typeParameters(), parentClass, name, reg, localGenerics);
}else{ } else {
gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), header.getStart()); gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), header.getStart());
} }
RefTypeOrTPHOrWildcardOrGeneric retType; RefTypeOrTPHOrWildcardOrGeneric retType;
if(header.result() != null){ if (header.result() != null) {
if(header.result().unannType() != null){ if (header.result().unannType() != null) {
retType = TypeGenerator.convert(header.result().unannType(), reg, localGenerics); retType = TypeGenerator.convert(header.result().unannType(), reg, localGenerics);
} } else
else retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart()); retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart());
}else{ } else {
retType = TypePlaceholder.fresh(header.getStart()); retType = TypePlaceholder.fresh(header.getStart());
} }
ParameterList parameterList = stmtGen.convert(header.methodDeclarator().formalParameterList()); ParameterList parameterList = stmtGen.convert(header.methodDeclarator().formalParameterList());
Block block = null; Block block = null;
if(body.block() == null){ if (body.block() == null) {
if(! Modifier.isAbstract(modifiers)){ if (!Modifier.isAbstract(modifiers)) {
//TODO: Error! Abstrakte Methode ohne abstrakt Keyword // TODO: Error! Abstrakte Methode ohne abstrakt Keyword
} }
}else{ } else {
block = stmtGen.convert(body.block(),true); block = stmtGen.convert(body.block(), true);
} }
if(parentClass.equals(new JavaClassName(name))){ if (parentClass.equals(new JavaClassName(name))) {
return new Constructor(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart() /*, fieldInitializations geloescht PL 2018-11-24 */); return new Constructor(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart() /*
}else{ * ,
return new Method(modifiers, name, retType, parameterList,block, gtvDeclarations, header.getStart()); * fieldInitializations
* geloescht
* PL
* 2018-
* 11-24
*/);
} else {
return new Method(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart());
} }
} }
private ClassOrInterface convertClass(Java17Parser.ClassDeclarationContext ctx) { private ClassOrInterface convertClass(Java17Parser.ClassDeclarationContext ctx) {
ClassOrInterface newClass; String className = this.pkgName + (this.pkgName.length() > 0 ? "." : "") + ctx.identifier().getText();
fieldInitializations = new ArrayList<>(); //PL 2019-10-22: muss für jede Klasse neu initilisiert werden JavaClassName name = reg.getName(className); // Holt den Package Namen mit dazu
if(ctx.normalClassDeclaration() != null){ if (!name.toString().equals(className)) { // Kommt die Klasse schon in einem anderen Package vor?
newClass = convertNormal(ctx.normalClassDeclaration()); throw new TypeinferenceException(
"Name " + className + " bereits vorhanden in " + reg.getName(className).toString(), ctx.getStart());
} }
else{ GenericsRegistry generics = createGenerics(ctx.genericDeclarationList(), name, "", reg,
newClass = convertEnum(ctx.enumDeclaration()); new GenericsRegistry(globalGenerics));
}
return newClass;
}
private ClassOrInterface convertNormal(Java17Parser.NormalClassDeclarationContext ctx) {
int modifiers = 0;
if(ctx.classModifier() != null){
for(Java17Parser.ClassModifierContext mod : ctx.classModifier()){
int newModifier = convert(mod);
modifiers += newModifier;
}
}
String className = this.pkgName + (this.pkgName.length()>0?".":"") + ctx.Identifier().getText();
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?
throw new TypeinferenceException("Name " + className + " bereits vorhanden in " + reg.getName(className).toString()
,ctx.getStart());
}
GenericsRegistry generics = createGenerics(ctx.typeParameters(), name, "", reg, new GenericsRegistry(globalGenerics));
Token offset = ctx.getStart(); Token offset = ctx.getStart();
GenericDeclarationList genericClassParameters; GenericDeclarationList genericClassParameters;
if(ctx.typeParameters() == null){ if (ctx.genericDeclarationList() == null) {
genericClassParameters = createEmptyGenericDeclarationList(ctx.Identifier()); genericClassParameters = createEmptyGenericDeclarationList(ctx.identifier().getStop());
}else{ } else {
genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics); genericClassParameters = TypeGenerator.convert(ctx.genericDeclarationList(), name, "", reg, generics);
} }
RefType superClass ; RefType superClass;
if(ctx.superclass() != null){ if (ctx.EXTENDS() != null) {
superClass = convert(ctx.superclass()); superClass = convert(ctx.typeType());
}else{ } else {
superClass = new RefType(ASTFactory.createObjectClass().getClassName(), ctx.getStart()); superClass = new RefType(ASTFactory.createObjectClass().getClassName(), ctx.getStart());
} }
List<Field> fielddecl = convertFields(ctx.classBody(), generics); List<Field> fielddecl = convertFields(ctx.classBody(), generics);
//fieldInitializations = generateFieldInitializations(ctx.classBody(), generics); // fieldInitializations = generateFieldInitializations(ctx.classBody(),
// generics);
List<Method> methodsAndConstructors = convertMethods(ctx.classBody(), name, superClass, generics); List<Method> methodsAndConstructors = convertMethods(ctx.classBody(), name, superClass, generics);
List<Method> methods = new ArrayList<>(); List<Method> methods = new ArrayList<>();
List<Constructor> konstruktoren = new ArrayList<>(); List<Constructor> konstruktoren = new ArrayList<>();
//int noOfMethods = methods.size(); // int noOfMethods = methods.size();
for(Method m : methodsAndConstructors){ for (Method m : methodsAndConstructors) {
if(m instanceof Constructor){ if (m instanceof Constructor) {
konstruktoren.add((Constructor) m); konstruktoren.add((Constructor) m);
} } else {
else {
methods.add(m); methods.add(m);
} }
} }
if(konstruktoren.size()<1){//Standardkonstruktor anfügen: if (konstruktoren.size() < 1) {// Standardkonstruktor anfügen:
konstruktoren.add( konstruktoren.add(
generateStandardConstructor( generateStandardConstructor(
ctx.Identifier().getText(), name, superClass, ctx.Identifier().getText(), name, superClass,
genericClassParameters, offset) genericClassParameters, offset));
);
} }
Boolean isInterface = false; Boolean isInterface = false;
List<RefType> implementedInterfaces = convert(ctx.superinterfaces(), generics); List<RefType> implementedInterfaces = convert(ctx.superinterfaces(), generics);
return new ClassOrInterface(modifiers, name, fielddecl, return new ClassOrInterface(modifiers, name, fielddecl,
Optional.of(this.generatePseudoConstructor(ctx.Identifier().getText(), name, superClass, genericClassParameters, offset)), Optional.of(this.generatePseudoConstructor(ctx.Identifier().getText(), name, superClass, genericClassParameters,
offset)),
methods, konstruktoren, genericClassParameters, superClass, methods, konstruktoren, genericClassParameters, superClass,
isInterface, implementedInterfaces, offset); isInterface, implementedInterfaces, offset);
} }
/* /*
private List<Statement> generateFieldInitializations(Java17Parser.ClassBodyContext classBodyContext, GenericsRegistry generics) { * private List<Statement>
List<Statement> ret = new ArrayList<>(); * generateFieldInitializations(Java17Parser.ClassBodyContext classBodyContext,
for(Java17Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ * GenericsRegistry generics) {
if(classMember.classMemberDeclaration() != null){ * List<Statement> ret = new ArrayList<>();
Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration(); * for(Java17Parser.ClassBodyDeclarationContext classMember :
if(classMemberDeclarationContext.fieldDeclaration() != null * classBodyContext.classBodyDeclaration()){
&& classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList() != null){ * if(classMember.classMemberDeclaration() != null){
for(Java17Parser.VariableDeclaratorContext ctx : classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList().variableDeclarator()) { * Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext =
String fieldName = ctx.variableDeclaratorId().Identifier().getText(); * classMember.classMemberDeclaration();
if(ctx.variableDeclaratorId().dims() != null)throw new NotImplementedException(); * if(classMemberDeclarationContext.fieldDeclaration() != null
Token offset = ctx.getStart(); * && classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList()
RefTypeOrTPHOrWildcardOrGeneric fieldType; * != null){
for(Field f : fields) * for(Java17Parser.VariableDeclaratorContext ctx :
AssignToField leftSide = new AssignToField(new FieldVar(new This(offset), fieldName, )); * classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList().
ret.addAll(); * variableDeclarator()) {
} * String fieldName = ctx.variableDeclaratorId().Identifier().getText();
}else if(classMemberDeclarationContext.methodDeclaration()!= null){ * if(ctx.variableDeclaratorId().dims() != null)throw new
//Do nothing! * NotImplementedException();
} * Token offset = ctx.getStart();
} * RefTypeOrTPHOrWildcardOrGeneric fieldType;
} * for(Field f : fields)
return ret; * AssignToField leftSide = new AssignToField(new FieldVar(new This(offset),
} * fieldName, ));
*/ * ret.addAll();
* }
* }else if(classMemberDeclarationContext.methodDeclaration()!= null){
* //Do nothing!
* }
* }
* }
* return ret;
* }
*/
private List<RefType> convert(Java17Parser.SuperinterfacesContext ctx, GenericsRegistry generics) { private List<RefType> convert(Java17Parser.SuperinterfacesContext ctx, GenericsRegistry generics) {
if(ctx == null)return new ArrayList<>(); if (ctx == null)
return new ArrayList<>();
return convert(ctx.interfaceTypeList(), generics); return convert(ctx.interfaceTypeList(), generics);
} }
private List<RefType> convert(Java17Parser.InterfaceTypeListContext ctx, GenericsRegistry generics) { private List<RefType> convert(Java17Parser.InterfaceTypeListContext ctx, GenericsRegistry generics) {
List<RefType> ret = new ArrayList<>(); List<RefType> ret = new ArrayList<>();
for(Java17Parser.InterfaceTypeContext interfaceType : ctx.interfaceType()){ for (Java17Parser.InterfaceTypeContext interfaceType : ctx.interfaceType()) {
ret.add((RefType) TypeGenerator.convert(interfaceType.classType(), reg, generics)); ret.add((RefType) TypeGenerator.convert(interfaceType.classType(), reg, generics));
} }
return ret; return ret;
@ -271,30 +297,46 @@ public class SyntaxTreeGenerator{
/** /**
* http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9 * http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9
*/ */
private Constructor generateStandardConstructor(String className, JavaClassName parentClass, RefType superClass, GenericDeclarationList classGenerics, Token offset){ private Constructor generateStandardConstructor(String className, JavaClassName parentClass, RefType superClass,
GenericDeclarationList classGenerics, Token offset) {
RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset);
ParameterList params = new ParameterList(new ArrayList<>(), offset); ParameterList params = new ParameterList(new ArrayList<>(), offset);
Block block = new Block(new ArrayList<>(), offset); Block block = new Block(new ArrayList<>(), offset);
return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset /*, fieldInitializations geloescht PL 2018-11-24 */); return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset /*
* ,
* fieldInitializations
* geloescht PL
* 2018-11-24
*/);
} }
/* fieldInitializations werden in einem Psedokonstruktor in der abstrakten Syntax gespeichert */ /*
private Constructor generatePseudoConstructor(String className, JavaClassName parentClass, RefType superClass, GenericDeclarationList classGenerics, Token offset){ * fieldInitializations werden in einem Psedokonstruktor in der abstrakten
* Syntax gespeichert
*/
private Constructor generatePseudoConstructor(String className, JavaClassName parentClass, RefType superClass,
GenericDeclarationList classGenerics, Token offset) {
RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset);
ParameterList params = new ParameterList(new ArrayList<>(), offset); ParameterList params = new ParameterList(new ArrayList<>(), offset);
Block block = new Block(new ArrayList<>(fieldInitializations), offset); Block block = new Block(new ArrayList<>(fieldInitializations), offset);
return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset /*, fieldInitializations geloescht PL 2018-11-24 */); return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset /*
* ,
* fieldInitializations
* geloescht PL
* 2018-11-24
*/);
} }
private RefType convert(Java17Parser.SuperclassContext superclass) { private RefType convert(Java17Parser.SuperclassContext superclass) {
if(superclass.classType().classOrInterfaceType() != null){ if (superclass.classType().classOrInterfaceType() != null) {
throw new NotImplementedException(); throw new NotImplementedException();
}else{ } else {
RefTypeOrTPHOrWildcardOrGeneric ret = TypeGenerator.convertTypeName(superclass.classType().Identifier().getText(), superclass.classType().typeArguments(), RefTypeOrTPHOrWildcardOrGeneric ret = TypeGenerator.convertTypeName(superclass.classType().Identifier().getText(),
superclass.classType().typeArguments(),
superclass.getStart(), reg, globalGenerics); superclass.getStart(), reg, globalGenerics);
if(ret instanceof RefType){ if (ret instanceof RefType) {
return (RefType) ret; return (RefType) ret;
}else{ } else {
throw new TypeinferenceException(superclass.getText() + " ist kein gültiger Supertyp", superclass.getStart()); throw new TypeinferenceException(superclass.getText() + " ist kein gültiger Supertyp", superclass.getStart());
} }
} }
@ -303,12 +345,12 @@ public class SyntaxTreeGenerator{
private List<Method> convertMethods(Java17Parser.ClassBodyContext classBodyContext, private List<Method> convertMethods(Java17Parser.ClassBodyContext classBodyContext,
JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { JavaClassName parentClass, RefType superClass, GenericsRegistry generics) {
List<Method> ret = new ArrayList<>(); List<Method> ret = new ArrayList<>();
for(Java17Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ for (Java17Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()) {
if(classMember.classMemberDeclaration() != null){ if (classMember.classMemberDeclaration() != null) {
Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration(); Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration();
if(classMemberDeclarationContext.fieldDeclaration() != null){ if (classMemberDeclarationContext.fieldDeclaration() != null) {
//Do nothing! // Do nothing!
}else if(classMemberDeclarationContext.methodDeclaration()!= null){ } else if (classMemberDeclarationContext.methodDeclaration() != null) {
ret.add(this.convert(classMemberDeclarationContext.methodDeclaration(), parentClass, superClass, generics)); ret.add(this.convert(classMemberDeclarationContext.methodDeclaration(), parentClass, superClass, generics));
} }
@ -319,13 +361,13 @@ public class SyntaxTreeGenerator{
private List<Field> convertFields(Java17Parser.ClassBodyContext classBodyContext, GenericsRegistry generics) { private List<Field> convertFields(Java17Parser.ClassBodyContext classBodyContext, GenericsRegistry generics) {
List<Field> ret = new ArrayList<>(); List<Field> ret = new ArrayList<>();
for(Java17Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ for (Java17Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()) {
if(classMember.classMemberDeclaration() != null){ if (classMember.classMemberDeclaration() != null) {
Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration(); Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration();
if(classMemberDeclarationContext.fieldDeclaration() != null){ if (classMemberDeclarationContext.fieldDeclaration() != null) {
ret.addAll(convert(classMember.classMemberDeclaration().fieldDeclaration(), generics)); ret.addAll(convert(classMember.classMemberDeclaration().fieldDeclaration(), generics));
}else if(classMemberDeclarationContext.methodDeclaration()!= null){ } else if (classMemberDeclarationContext.methodDeclaration() != null) {
//Do nothing! // Do nothing!
} }
} }
} }
@ -334,40 +376,49 @@ public class SyntaxTreeGenerator{
public static int convert(List<Java17Parser.MethodModifierContext> methodModifierContexts) { public static int convert(List<Java17Parser.MethodModifierContext> methodModifierContexts) {
int ret = 0; int ret = 0;
for(Java17Parser.MethodModifierContext mod : methodModifierContexts){ for (Java17Parser.MethodModifierContext mod : methodModifierContexts) {
if(mod.annotation() == null)convertModifier(mod.getText()); if (mod.annotation() == null)
convertModifier(mod.getText());
} }
return ret; return ret;
} }
public static int convertInterfaceModifier(List<Java17Parser.InterfaceMethodModifierContext> methodModifierContexts) { public static int convertInterfaceModifier(List<Java17Parser.InterfaceMethodModifierContext> methodModifierContexts) {
int ret = 0; int ret = 0;
for(Java17Parser.InterfaceMethodModifierContext mod : methodModifierContexts){ for (Java17Parser.InterfaceMethodModifierContext mod : methodModifierContexts) {
if(mod.annotation() == null)convertModifier(mod.getText()); if (mod.annotation() == null)
convertModifier(mod.getText());
} }
return ret; return ret;
} }
private List<? extends Field> convert(Java17Parser.FieldDeclarationContext fieldDeclarationContext, GenericsRegistry generics) { private List<? extends Field> convert(Java17Parser.FieldDeclarationContext fieldDeclarationContext,
GenericsRegistry generics) {
List<Field> ret = new ArrayList<>(); List<Field> ret = new ArrayList<>();
int modifiers = 0; int modifiers = 0;
for(Java17Parser.FieldModifierContext fieldModifierContext : fieldDeclarationContext.fieldModifier()){ for (Java17Parser.FieldModifierContext fieldModifierContext : fieldDeclarationContext.fieldModifier()) {
modifiers+=(convert(fieldModifierContext)); modifiers += (convert(fieldModifierContext));
} }
RefTypeOrTPHOrWildcardOrGeneric fieldType; RefTypeOrTPHOrWildcardOrGeneric fieldType;
if(fieldDeclarationContext.unannTypeOrAuto() != null if (fieldDeclarationContext.unannTypeOrAuto() != null
&& fieldDeclarationContext.unannTypeOrAuto().unannType() != null){ && fieldDeclarationContext.unannTypeOrAuto().unannType() != null) {
fieldType = TypeGenerator.convert(fieldDeclarationContext.unannTypeOrAuto().unannType(), reg, generics); fieldType = TypeGenerator.convert(fieldDeclarationContext.unannTypeOrAuto().unannType(), reg, generics);
}else{ } else {
fieldType = TypePlaceholder.fresh(fieldDeclarationContext.variableDeclaratorList().getStart()); //PL 2019-12-06: variableDeclaratorList() eingefuegt, um als Token nicht die Modifier zu bekommen fieldType = TypePlaceholder.fresh(fieldDeclarationContext.variableDeclaratorList().getStart()); // PL 2019-12-06:
// variableDeclaratorList()
// eingefuegt, um
// als Token nicht
// die Modifier zu
// bekommen
} }
for(Java17Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList().variableDeclarator()){ for (Java17Parser.VariableDeclaratorContext varCtx : fieldDeclarationContext.variableDeclaratorList()
.variableDeclarator()) {
String fieldName = convert(varCtx.variableDeclaratorId()); String fieldName = convert(varCtx.variableDeclaratorId());
fields.put(fieldName, fieldType); fields.put(fieldName, fieldType);
if(varCtx.variableInitializer() != null){ if (varCtx.variableInitializer() != null) {
initializeField(varCtx, fieldType, generics); initializeField(varCtx, fieldType, generics);
} }
ret.add(new Field(fieldName,fieldType,modifiers,varCtx.getStart())); ret.add(new Field(fieldName, fieldType, modifiers, varCtx.getStart()));
} }
return ret; return ret;
} }
@ -377,12 +428,13 @@ public class SyntaxTreeGenerator{
} }
// Initialize a field by creating implicit constructor. // Initialize a field by creating implicit constructor.
private void initializeField(Java17Parser.VariableDeclaratorContext ctx, RefTypeOrTPHOrWildcardOrGeneric typeOfField, GenericsRegistry generics){ private void initializeField(Java17Parser.VariableDeclaratorContext ctx, RefTypeOrTPHOrWildcardOrGeneric typeOfField,
GenericsRegistry generics) {
StatementGenerator statementGenerator = new StatementGenerator(reg, generics, fields, new HashMap<>()); StatementGenerator statementGenerator = new StatementGenerator(reg, generics, fields, new HashMap<>());
fieldInitializations.add(statementGenerator.generateFieldAssignment(ctx, typeOfField)); fieldInitializations.add(statementGenerator.generateFieldAssignment(ctx, typeOfField));
} }
public static int convertModifier(String modifier){ public static int convertModifier(String modifier) {
HashMap<String, Integer> modifiers = new HashMap<>(); HashMap<String, Integer> modifiers = new HashMap<>();
modifiers.put(Modifier.toString(Modifier.PUBLIC), Modifier.PUBLIC); modifiers.put(Modifier.toString(Modifier.PUBLIC), Modifier.PUBLIC);
modifiers.put(Modifier.toString(Modifier.PRIVATE), Modifier.PRIVATE); modifiers.put(Modifier.toString(Modifier.PRIVATE), Modifier.PRIVATE);
@ -397,57 +449,57 @@ public class SyntaxTreeGenerator{
modifiers.put(Modifier.toString(Modifier.NATIVE), Modifier.NATIVE); modifiers.put(Modifier.toString(Modifier.NATIVE), Modifier.NATIVE);
modifiers.put(Modifier.toString(Modifier.INTERFACE), Modifier.INTERFACE); modifiers.put(Modifier.toString(Modifier.INTERFACE), Modifier.INTERFACE);
int ret = 0; int ret = 0;
for(String m : modifiers.keySet()){ for (String m : modifiers.keySet()) {
if(modifier.contains(m))ret+=modifiers.get(m); if (modifier.contains(m))
ret += modifiers.get(m);
} }
return ret; return ret;
} }
private int convert(Java17Parser.ClassModifierContext ctx){ private int convert(Java17Parser.ClassOrInterfaceModifierContext ctx) {
if(ctx.annotation() != null)return 0; if (ctx.annotation() != null)
return 0;
return convertModifier(ctx.getText()); return convertModifier(ctx.getText());
} }
private int convert(Java17Parser.FieldModifierContext ctx){ private int convert(Java17Parser.FieldModifierContext ctx) {
if(ctx.annotation() != null)return 0; if (ctx.annotation() != null)
return 0;
return convertModifier(ctx.getText()); return convertModifier(ctx.getText());
} }
private int convert(Java17Parser.InterfaceModifierContext ctx) { private ClassOrInterface convertEnum(Java17Parser.EnumDeclarationContext ctx) {
if(ctx.annotation() != null)return 0;
return convertModifier(ctx.getText());
}
private ClassOrInterface convertEnum(Java17Parser.EnumDeclarationContext ctx){
return null; return null;
} }
private ClassOrInterface convertInterface(Java17Parser.InterfaceDeclarationContext ctx){ private ClassOrInterface convertInterface(Java17Parser.InterfaceDeclarationContext ctx) {
if(ctx.normalInterfaceDeclaration() != null){ if (ctx.normalInterfaceDeclaration() != null) {
return convertNormal(ctx.normalInterfaceDeclaration()); return convertNormal(ctx.normalInterfaceDeclaration());
}else{ } else {
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
private ClassOrInterface convertNormal(Java17Parser.NormalInterfaceDeclarationContext ctx) { private ClassOrInterface convertNormal(Java17Parser.NormalInterfaceDeclarationContext ctx) {
int modifiers = 0; int modifiers = 0;
if(ctx.interfaceModifier() != null){ if (ctx.interfaceModifier() != null) {
for( Java17Parser.InterfaceModifierContext mod : ctx.interfaceModifier()){ for (Java17Parser.InterfaceModifierContext mod : ctx.interfaceModifier()) {
int newModifier = convert(mod); int newModifier = convert(mod);
modifiers += newModifier; modifiers += newModifier;
} }
} }
if(!Modifier.isInterface(modifiers))modifiers += Modifier.INTERFACE; if (!Modifier.isInterface(modifiers))
modifiers += Modifier.INTERFACE;
JavaClassName name = reg.getName(ctx.Identifier().getText()); JavaClassName name = reg.getName(ctx.Identifier().getText());
GenericsRegistry generics = createGenerics(ctx.typeParameters(), name, "", reg, new GenericsRegistry(globalGenerics)); GenericsRegistry generics = createGenerics(ctx.typeParameters(), name, "", reg,
new GenericsRegistry(globalGenerics));
GenericDeclarationList genericParams; GenericDeclarationList genericParams;
if(ctx.typeParameters() != null){ if (ctx.typeParameters() != null) {
genericParams = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics); genericParams = TypeGenerator.convert(ctx.typeParameters(), name, "", reg, generics);
}else{ } else {
genericParams = createEmptyGenericDeclarationList(ctx.Identifier()); genericParams = createEmptyGenericDeclarationList(ctx.Identifier());
} }
RefType superClass = ASTFactory.createObjectType(); RefType superClass = ASTFactory.createObjectType();
@ -461,39 +513,42 @@ public class SyntaxTreeGenerator{
genericParams, superClass, true, extendedInterfaces, ctx.getStart()); genericParams, superClass, true, extendedInterfaces, ctx.getStart());
} }
private GenericDeclarationList createEmptyGenericDeclarationList(TerminalNode classNameIdentifier) { private GenericDeclarationList createEmptyGenericDeclarationList(Token classNameIdentifier) {
CommonToken gtvOffset = new CommonToken(classNameIdentifier.getSymbol()); CommonToken gtvOffset = new CommonToken(classNameIdentifier);
gtvOffset.setCharPositionInLine(gtvOffset.getCharPositionInLine()+classNameIdentifier.getText().length()); gtvOffset.setCharPositionInLine(gtvOffset.getCharPositionInLine() + classNameIdentifier.getText().length());
gtvOffset.setStartIndex(gtvOffset.getStopIndex()+1); gtvOffset.setStartIndex(gtvOffset.getStopIndex() + 1);
return new GenericDeclarationList(new ArrayList<>(), gtvOffset); return new GenericDeclarationList(new ArrayList<>(), gtvOffset);
} }
private GenericsRegistry createGenerics(Java17Parser.TypeParametersContext ctx, JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) { private GenericsRegistry createGenerics(Java17Parser.GenericDeclarationListContext ctx, JavaClassName parentClass,
String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
GenericsRegistry ret = new GenericsRegistry(this.globalGenerics); GenericsRegistry ret = new GenericsRegistry(this.globalGenerics);
ret.putAll(generics); ret.putAll(generics);
if(ctx == null || ctx.typeParameterList() == null)return ret; if (ctx == null)
for(Java17Parser.TypeParameterContext tp : ctx.typeParameterList().typeParameter()){ return ret;
ret.put(tp.Identifier().getText(), new GenericContext(parentClass, parentMethod)); for (Java17Parser.GenericTypeVarContext tp : ctx.genericTypeVar()) {
} ret.put(tp.identifier().getText(), new GenericContext(parentClass, parentMethod));
for(Java17Parser.TypeParameterContext tp : ctx.typeParameterList().typeParameter()){
TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret); TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret);
} }
return ret; return ret;
} }
private List<RefType> convert(Java17Parser.ExtendsInterfacesContext extendsInterfacesContext, GenericsRegistry generics) { private List<RefType> convert(Java17Parser.ExtendsInterfacesContext extendsInterfacesContext,
if(extendsInterfacesContext == null)return new ArrayList<>(); GenericsRegistry generics) {
if (extendsInterfacesContext == null)
return new ArrayList<>();
return convert(extendsInterfacesContext.interfaceTypeList(), generics); return convert(extendsInterfacesContext.interfaceTypeList(), generics);
} }
private List<Method> convertMethods(Java17Parser.InterfaceBodyContext interfaceBodyContext, private List<Method> convertMethods(Java17Parser.InterfaceBodyContext interfaceBodyContext,
JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { JavaClassName parentClass, RefType superClass, GenericsRegistry generics) {
List<Method> ret = new ArrayList<>(); List<Method> ret = new ArrayList<>();
for(Java17Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()){ for (Java17Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()) {
if(member.interfaceMethodDeclaration() != null){ if (member.interfaceMethodDeclaration() != null) {
ret.add(this.convert(member.interfaceMethodDeclaration(), parentClass, superClass, generics)); ret.add(this.convert(member.interfaceMethodDeclaration(), parentClass, superClass, generics));
//new Method(name, type, modifier, params, null, genericDecls, member.interfaceMethodDeclaration().getStart()); // new Method(name, type, modifier, params, null, genericDecls,
}else{ // member.interfaceMethodDeclaration().getStart());
} else {
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
@ -502,14 +557,13 @@ public class SyntaxTreeGenerator{
private List<Field> convertFields(Java17Parser.InterfaceBodyContext interfaceBodyContext) { private List<Field> convertFields(Java17Parser.InterfaceBodyContext interfaceBodyContext) {
List<Field> ret = new ArrayList<>(); List<Field> ret = new ArrayList<>();
for(Java17Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()){ for (Java17Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()) {
if(member.constantDeclaration() != null){ if (member.constantDeclaration() != null) {
//TODO: Erstelle hier ein Feld! // TODO: Erstelle hier ein Feld!
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
return ret; return ret;
} }
} }

View File

@ -4,7 +4,7 @@ import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.*; import java.util.*;
import de.dhbwstuttgart.parser.antlr.Java17BaseListener; import de.dhbwstuttgart.parser.antlr.Java17ParserBaseVisitor;
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
import org.antlr.v4.runtime.tree.ParseTreeWalker; import org.antlr.v4.runtime.tree.ParseTreeWalker;
@ -15,50 +15,65 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser;
public class GatherNames { public class GatherNames {
public static Map<String, Integer> getNames(Java17Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException{ public static Map<String, Integer> getNames(Java17Parser.SourceFileContext ctx, PackageCrawler packages,
ClassLoader classLoader) throws ClassNotFoundException {
Map<String, Integer> ret = new HashMap<>(); Map<String, Integer> ret = new HashMap<>();
String pkgName = getPackageName(ctx); String pkgName = getPackageName(ctx);
String nameString = ""; String nameString = "";
for (Java17Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ for (Java17Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()) {
if(typeDecl.interfaceDeclaration() != null){ if (typeDecl.interfaceDeclaration() != null) {
if(typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null){ if (typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null) {
if(pkgName != ""){ if (pkgName != "") {
nameString = pkgName + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); nameString = pkgName + "."
} + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
else{ } else {
nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
} }
int numGenerics = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters()!=null? int numGenerics = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters() != null
typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters().typeParameterList().typeParameter().size():0; ? typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters().typeParameterList()
//Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen: .typeParameter().size()
/* //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht sind : 0;
if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){ // Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry
for(Java17Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){ // anfügen:
//this.reg.add(tp.Identifier().toString()); /*
} * //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht
} * sind
* if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() !=
* null){
* for(Java17Parser.TypeParameterContext tp :
* typeDecl.classDeclaration().normalClassDeclaration().typeParameters().
* typeParameterList().typeParameter()){
* //this.reg.add(tp.Identifier().toString());
* }
* }
*/ */
ret.put(nameString, numGenerics); ret.put(nameString, numGenerics);
} }
} } else {
else{ if (typeDecl.classDeclaration().normalClassDeclaration() != null) {
if(typeDecl.classDeclaration().normalClassDeclaration() != null){ if (!pkgName.isEmpty()) {
if(!pkgName.isEmpty()){
nameString = pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); nameString = pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
} } else {
else{
nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
} }
//Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen: // Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry
/* //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht sind // anfügen:
if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){ /*
for(Java17Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){ * //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht
this.reg.add(tp.Identifier().toString()); * sind
} * if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() !=
} * null){
* for(Java17Parser.TypeParameterContext tp :
* typeDecl.classDeclaration().normalClassDeclaration().typeParameters().
* typeParameterList().typeParameter()){
* this.reg.add(tp.Identifier().toString());
* }
* }
*/ */
int numGenerics = typeDecl.classDeclaration().normalClassDeclaration().typeParameters()!=null? int numGenerics = typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null
typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter().size():0; ? typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList()
.typeParameter().size()
: 0;
ret.put(nameString, numGenerics); ret.put(nameString, numGenerics);
} }
@ -68,32 +83,31 @@ public class GatherNames {
return ret; return ret;
} }
public static Map<String, Integer> getImports(Java17Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException { public static Map<String, Integer> getImports(Java17Parser.SourceFileContext ctx, PackageCrawler packages,
ClassLoader classLoader) throws ClassNotFoundException {
Map<String, Integer> ret = new HashMap<>(); Map<String, Integer> ret = new HashMap<>();
//ret.putAll(packages.getClassNames("java.lang")); // ret.putAll(packages.getClassNames("java.lang"));
for(Java17Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ for (Java17Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()) {
if(importDeclCtx.singleTypeImportDeclaration() != null){ if (importDeclCtx.singleTypeImportDeclaration() != null) {
Class cl = classLoader.loadClass(importDeclCtx.singleTypeImportDeclaration().typeName().getText()); Class cl = classLoader.loadClass(importDeclCtx.singleTypeImportDeclaration().typeName().getText());
ret.put(cl.getName(), cl.getTypeParameters().length); ret.put(cl.getName(), cl.getTypeParameters().length);
} } else if (importDeclCtx.typeImportOnDemandDeclaration() != null) {
else if(importDeclCtx.typeImportOnDemandDeclaration() != null){
ret.putAll(packages.getClassNames(importDeclCtx.typeImportOnDemandDeclaration().packageOrTypeName().getText())); ret.putAll(packages.getClassNames(importDeclCtx.typeImportOnDemandDeclaration().packageOrTypeName().getText()));
} } else if (importDeclCtx.singleStaticImportDeclaration() != null) {
else if(importDeclCtx.singleStaticImportDeclaration() != null){ Class cl = classLoader.loadClass(importDeclCtx.singleStaticImportDeclaration().typeName().getText() + "."
Class cl = classLoader.loadClass(importDeclCtx.singleStaticImportDeclaration().typeName().getText()+"."+importDeclCtx.singleStaticImportDeclaration().Identifier().getText()); + importDeclCtx.singleStaticImportDeclaration().Identifier().getText());
ret.put(cl.getName(), cl.getTypeParameters().length); ret.put(cl.getName(), cl.getTypeParameters().length);
} } else {
else{
ret.putAll(packages.getClassNames(importDeclCtx.staticImportOnDemandDeclaration().typeName().getText())); ret.putAll(packages.getClassNames(importDeclCtx.staticImportOnDemandDeclaration().typeName().getText()));
} }
} }
return ret; return ret;
} }
private static String getPackageName(Java17Parser.CompilationUnitContext ctx){ private static String getPackageName(Java17Parser.CompilationUnitContext ctx) {
String pkgName = ""; String pkgName = "";
if(ctx.packageDeclaration() != null){ if (ctx.packageDeclaration() != null) {
for(TerminalNode t : ctx.packageDeclaration().Identifier()){ for (TerminalNode t : ctx.packageDeclaration().Identifier()) {
pkgName = pkgName + "." + t.toString(); pkgName = pkgName + "." + t.toString();
} }
pkgName = pkgName.substring(1); pkgName = pkgName.substring(1);