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.parser.NullToken;
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.GenericsRegistry;
import de.dhbwstuttgart.parser.scope.JavaClassName;
@ -27,66 +31,91 @@ import java.util.stream.Collectors;
//import jdk.internal.dynalink.support.TypeConverterFactory;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
public class SyntaxTreeGenerator{
public class SyntaxTreeGenerator extends Java17ParserBaseVisitor<SourceFile> {
private JavaClassRegistry reg;
private final GenericsRegistry globalGenerics;
private String pkgName = "";
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
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
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) {
// Die Generics müssen während des Bauens des AST erstellt 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.reg = reg;
}
public JavaClassRegistry getReg() {
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.
public String convertPackageOrTypeName(Java17Parser.PackageOrTypeNameContext ctx){
String ret;
if(ctx.packageOrTypeName() == null){
ret = ctx.Identifier().toString();
/*
* Package und Type Namen sind qualified Names und werden durch nachfolgende
* Methode behandelt. Diese Methode sollte damit überflüssig sein.
*
* 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;
}
public SourceFile convert(Java17Parser.CompilationUnitContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader) throws ClassNotFoundException{
if(ctx.packageDeclaration()!=null)this.pkgName = convert(ctx.packageDeclaration());
public SourceFile convert(Java17Parser.SourceFileContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader)
throws ClassNotFoundException {
SrcfileContext srcfile = new SrcfileContext(ctx);
if (srcfile.packageDeclaration() != null)
this.pkgName = convert(srcfile.packageDeclaration());
List<ClassOrInterface> classes = new ArrayList<>();
Map<String, Integer> imports = GatherNames.getImports(ctx, packageCrawler, classLoader);
this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet());
for(Java17Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
ClassOrInterface newClass;
if(typeDecl.classDeclaration() != null){
newClass = convertClass(typeDecl.classDeclaration());
for (Java17Parser.ClassOrInterfaceContext member : srcfile.classOrInterface()) {
ClassorinterfacedeclContext clsoif;
if (member instanceof NoclassorinterfaceContext) {
continue;
} else {
clsoif = new ClassorinterfacedeclContext(member);
}
else{
newClass = convertInterface(typeDecl.interfaceDeclaration());
ClassOrInterface newClass;
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);
}
@ -94,15 +123,11 @@ public class SyntaxTreeGenerator{
}
private String convert(Java17Parser.PackageDeclarationContext packageDeclarationContext) {
String ret = "";
for(TerminalNode identifier : packageDeclarationContext.Identifier()){
ret += identifier.getText()+".";
}
ret = ret.substring(0, ret.length()-1);
return ret;
return convertQualifiedName(packageDeclarationContext.qualifiedName());
}
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();
int modifiers = SyntaxTreeGenerator.convert(methodDeclarationContext.methodModifier());
GenericsRegistry localGenerics = createGenerics(methodDeclarationContext.methodHeader().typeParameters(),
@ -111,11 +136,13 @@ public class SyntaxTreeGenerator{
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();
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);
return convert(modifiers, header, ctx.methodBody(), parentClass, superClass, localGenerics);
@ -137,8 +164,8 @@ public class SyntaxTreeGenerator{
if (header.result() != null) {
if (header.result().unannType() != null) {
retType = TypeGenerator.convert(header.result().unannType(), reg, localGenerics);
}
else retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart());
} else
retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart());
} else {
retType = TypePlaceholder.fresh(header.getStart());
}
@ -152,54 +179,44 @@ public class SyntaxTreeGenerator{
block = stmtGen.convert(body.block(), true);
}
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() /*
* ,
* fieldInitializations
* geloescht
* PL
* 2018-
* 11-24
*/);
} else {
return new Method(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart());
}
}
private ClassOrInterface convertClass(Java17Parser.ClassDeclarationContext ctx) {
ClassOrInterface newClass;
fieldInitializations = new ArrayList<>(); //PL 2019-10-22: muss für jede Klasse neu initilisiert werden
if(ctx.normalClassDeclaration() != null){
newClass = convertNormal(ctx.normalClassDeclaration());
}
else{
newClass = convertEnum(ctx.enumDeclaration());
}
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();
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());
throw new TypeinferenceException(
"Name " + className + " bereits vorhanden in " + reg.getName(className).toString(), ctx.getStart());
}
GenericsRegistry generics = createGenerics(ctx.typeParameters(), name, "", reg, new GenericsRegistry(globalGenerics));
GenericsRegistry generics = createGenerics(ctx.genericDeclarationList(), name, "", reg,
new GenericsRegistry(globalGenerics));
Token offset = ctx.getStart();
GenericDeclarationList genericClassParameters;
if(ctx.typeParameters() == null){
genericClassParameters = createEmptyGenericDeclarationList(ctx.Identifier());
if (ctx.genericDeclarationList() == null) {
genericClassParameters = createEmptyGenericDeclarationList(ctx.identifier().getStop());
} else {
genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics);
genericClassParameters = TypeGenerator.convert(ctx.genericDeclarationList(), name, "", reg, generics);
}
RefType superClass;
if(ctx.superclass() != null){
superClass = convert(ctx.superclass());
if (ctx.EXTENDS() != null) {
superClass = convert(ctx.typeType());
} else {
superClass = new RefType(ASTFactory.createObjectClass().getClassName(), ctx.getStart());
}
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> methods = new ArrayList<>();
List<Constructor> konstruktoren = new ArrayList<>();
@ -207,8 +224,7 @@ public class SyntaxTreeGenerator{
for (Method m : methodsAndConstructors) {
if (m instanceof Constructor) {
konstruktoren.add((Constructor) m);
}
else {
} else {
methods.add(m);
}
}
@ -216,47 +232,57 @@ public class SyntaxTreeGenerator{
konstruktoren.add(
generateStandardConstructor(
ctx.Identifier().getText(), name, superClass,
genericClassParameters, offset)
);
genericClassParameters, offset));
}
Boolean isInterface = false;
List<RefType> implementedInterfaces = convert(ctx.superinterfaces(), generics);
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,
isInterface, implementedInterfaces, offset);
}
/*
private List<Statement> generateFieldInitializations(Java17Parser.ClassBodyContext classBodyContext, GenericsRegistry generics) {
List<Statement> ret = new ArrayList<>();
for(Java17Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){
if(classMember.classMemberDeclaration() != null){
Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration();
if(classMemberDeclarationContext.fieldDeclaration() != null
&& classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList() != null){
for(Java17Parser.VariableDeclaratorContext ctx : classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList().variableDeclarator()) {
String fieldName = ctx.variableDeclaratorId().Identifier().getText();
if(ctx.variableDeclaratorId().dims() != null)throw new NotImplementedException();
Token offset = ctx.getStart();
RefTypeOrTPHOrWildcardOrGeneric fieldType;
for(Field f : fields)
AssignToField leftSide = new AssignToField(new FieldVar(new This(offset), fieldName, ));
ret.addAll();
}
}else if(classMemberDeclarationContext.methodDeclaration()!= null){
//Do nothing!
}
}
}
return ret;
}
* private List<Statement>
* generateFieldInitializations(Java17Parser.ClassBodyContext classBodyContext,
* GenericsRegistry generics) {
* List<Statement> ret = new ArrayList<>();
* for(Java17Parser.ClassBodyDeclarationContext classMember :
* classBodyContext.classBodyDeclaration()){
* if(classMember.classMemberDeclaration() != null){
* Java17Parser.ClassMemberDeclarationContext classMemberDeclarationContext =
* classMember.classMemberDeclaration();
* if(classMemberDeclarationContext.fieldDeclaration() != null
* && classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList()
* != null){
* for(Java17Parser.VariableDeclaratorContext ctx :
* classMemberDeclarationContext.fieldDeclaration().variableDeclaratorList().
* variableDeclarator()) {
* String fieldName = ctx.variableDeclaratorId().Identifier().getText();
* if(ctx.variableDeclaratorId().dims() != null)throw new
* NotImplementedException();
* Token offset = ctx.getStart();
* RefTypeOrTPHOrWildcardOrGeneric fieldType;
* for(Field f : fields)
* 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) {
if(ctx == null)return new ArrayList<>();
if (ctx == null)
return new ArrayList<>();
return convert(ctx.interfaceTypeList(), generics);
}
@ -271,26 +297,42 @@ public class SyntaxTreeGenerator{
/**
* 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);
ParameterList params = new ParameterList(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);
ParameterList params = new ParameterList(new ArrayList<>(), 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) {
if (superclass.classType().classOrInterfaceType() != null) {
throw new NotImplementedException();
} 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);
if (ret instanceof RefType) {
return (RefType) ret;
@ -335,7 +377,8 @@ public class SyntaxTreeGenerator{
public static int convert(List<Java17Parser.MethodModifierContext> methodModifierContexts) {
int ret = 0;
for (Java17Parser.MethodModifierContext mod : methodModifierContexts) {
if(mod.annotation() == null)convertModifier(mod.getText());
if (mod.annotation() == null)
convertModifier(mod.getText());
}
return ret;
}
@ -343,12 +386,14 @@ public class SyntaxTreeGenerator{
public static int convertInterfaceModifier(List<Java17Parser.InterfaceMethodModifierContext> methodModifierContexts) {
int ret = 0;
for (Java17Parser.InterfaceMethodModifierContext mod : methodModifierContexts) {
if(mod.annotation() == null)convertModifier(mod.getText());
if (mod.annotation() == null)
convertModifier(mod.getText());
}
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<>();
int modifiers = 0;
for (Java17Parser.FieldModifierContext fieldModifierContext : fieldDeclarationContext.fieldModifier()) {
@ -359,9 +404,15 @@ public class SyntaxTreeGenerator{
&& fieldDeclarationContext.unannTypeOrAuto().unannType() != null) {
fieldType = TypeGenerator.convert(fieldDeclarationContext.unannTypeOrAuto().unannType(), reg, generics);
} 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());
fields.put(fieldName, fieldType);
if (varCtx.variableInitializer() != null) {
@ -377,7 +428,8 @@ public class SyntaxTreeGenerator{
}
// 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<>());
fieldInitializations.add(statementGenerator.generateFieldAssignment(ctx, typeOfField));
}
@ -398,23 +450,21 @@ public class SyntaxTreeGenerator{
modifiers.put(Modifier.toString(Modifier.INTERFACE), Modifier.INTERFACE);
int ret = 0;
for (String m : modifiers.keySet()) {
if(modifier.contains(m))ret+=modifiers.get(m);
if (modifier.contains(m))
ret += modifiers.get(m);
}
return ret;
}
private int convert(Java17Parser.ClassModifierContext ctx){
if(ctx.annotation() != null)return 0;
private int convert(Java17Parser.ClassOrInterfaceModifierContext ctx) {
if (ctx.annotation() != null)
return 0;
return convertModifier(ctx.getText());
}
private int convert(Java17Parser.FieldModifierContext ctx) {
if(ctx.annotation() != null)return 0;
return convertModifier(ctx.getText());
}
private int convert(Java17Parser.InterfaceModifierContext ctx) {
if(ctx.annotation() != null)return 0;
if (ctx.annotation() != null)
return 0;
return convertModifier(ctx.getText());
}
@ -438,11 +488,13 @@ public class SyntaxTreeGenerator{
modifiers += newModifier;
}
}
if(!Modifier.isInterface(modifiers))modifiers += Modifier.INTERFACE;
if (!Modifier.isInterface(modifiers))
modifiers += Modifier.INTERFACE;
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;
if (ctx.typeParameters() != null) {
@ -461,28 +513,30 @@ public class SyntaxTreeGenerator{
genericParams, superClass, true, extendedInterfaces, ctx.getStart());
}
private GenericDeclarationList createEmptyGenericDeclarationList(TerminalNode classNameIdentifier) {
CommonToken gtvOffset = new CommonToken(classNameIdentifier.getSymbol());
private GenericDeclarationList createEmptyGenericDeclarationList(Token classNameIdentifier) {
CommonToken gtvOffset = new CommonToken(classNameIdentifier);
gtvOffset.setCharPositionInLine(gtvOffset.getCharPositionInLine() + classNameIdentifier.getText().length());
gtvOffset.setStartIndex(gtvOffset.getStopIndex() + 1);
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);
ret.putAll(generics);
if(ctx == null || ctx.typeParameterList() == null)return ret;
for(Java17Parser.TypeParameterContext tp : ctx.typeParameterList().typeParameter()){
ret.put(tp.Identifier().getText(), new GenericContext(parentClass, parentMethod));
}
for(Java17Parser.TypeParameterContext tp : ctx.typeParameterList().typeParameter()){
if (ctx == null)
return ret;
for (Java17Parser.GenericTypeVarContext tp : ctx.genericTypeVar()) {
ret.put(tp.identifier().getText(), new GenericContext(parentClass, parentMethod));
TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret);
}
return ret;
}
private List<RefType> convert(Java17Parser.ExtendsInterfacesContext extendsInterfacesContext, GenericsRegistry generics) {
if(extendsInterfacesContext == null)return new ArrayList<>();
private List<RefType> convert(Java17Parser.ExtendsInterfacesContext extendsInterfacesContext,
GenericsRegistry generics) {
if (extendsInterfacesContext == null)
return new ArrayList<>();
return convert(extendsInterfacesContext.interfaceTypeList(), generics);
}
@ -492,7 +546,8 @@ public class SyntaxTreeGenerator{
for (Java17Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()) {
if (member.interfaceMethodDeclaration() != null) {
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,
// member.interfaceMethodDeclaration().getStart());
} else {
throw new NotImplementedException();
}
@ -511,5 +566,4 @@ public class SyntaxTreeGenerator{
return ret;
}
}

View File

@ -4,7 +4,7 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import de.dhbwstuttgart.parser.antlr.Java17BaseListener;
import de.dhbwstuttgart.parser.antlr.Java17ParserBaseVisitor;
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
@ -15,7 +15,8 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser;
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<>();
String pkgName = getPackageName(ctx);
String nameString = "";
@ -23,42 +24,56 @@ public class GatherNames {
if (typeDecl.interfaceDeclaration() != null) {
if (typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null) {
if (pkgName != "") {
nameString = pkgName + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
}
else{
nameString = pkgName + "."
+ typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
} else {
nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
}
int numGenerics = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters()!=null?
typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters().typeParameterList().typeParameter().size():0;
//Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen:
/* //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());
}
}
int numGenerics = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters() != null
? typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters().typeParameterList()
.typeParameter().size()
: 0;
// Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry
// anfügen:
/*
* //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);
}
}
else{
} else {
if (typeDecl.classDeclaration().normalClassDeclaration() != null) {
if (!pkgName.isEmpty()) {
nameString = pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
}
else{
} else {
nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
}
//Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen:
/* //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());
}
}
// Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry
// anfügen:
/*
* //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());
* }
* }
*/
int numGenerics = typeDecl.classDeclaration().normalClassDeclaration().typeParameters()!=null?
typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter().size():0;
int numGenerics = typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null
? typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList()
.typeParameter().size()
: 0;
ret.put(nameString, numGenerics);
}
@ -68,22 +83,21 @@ public class GatherNames {
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<>();
// ret.putAll(packages.getClassNames("java.lang"));
for (Java17Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()) {
if (importDeclCtx.singleTypeImportDeclaration() != null) {
Class cl = classLoader.loadClass(importDeclCtx.singleTypeImportDeclaration().typeName().getText());
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()));
}
else if(importDeclCtx.singleStaticImportDeclaration() != null){
Class cl = classLoader.loadClass(importDeclCtx.singleStaticImportDeclaration().typeName().getText()+"."+importDeclCtx.singleStaticImportDeclaration().Identifier().getText());
} else if (importDeclCtx.singleStaticImportDeclaration() != null) {
Class cl = classLoader.loadClass(importDeclCtx.singleStaticImportDeclaration().typeName().getText() + "."
+ importDeclCtx.singleStaticImportDeclaration().Identifier().getText());
ret.put(cl.getName(), cl.getTypeParameters().length);
}
else{
} else {
ret.putAll(packages.getClassNames(importDeclCtx.staticImportOnDemandDeclaration().typeName().getText()));
}
}