Visitor Ansatz verworfen, STG in ASTGen neu aufsetzen

This commit is contained in:
luca9913 2023-01-20 06:13:38 +01:00
parent a85b60b95f
commit a13294edf4
6 changed files with 188 additions and 183 deletions

View File

@ -62,10 +62,6 @@ http://maven.apache.org/maven-v4_0_0.xsd">
<groupId>org.antlr</groupId> <groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId> <artifactId>antlr4-maven-plugin</artifactId>
<version>4.8-1</version> <version>4.8-1</version>
<configuration>
<visitor>true</visitor>
<listener>false</listener>
</configuration>
<executions> <executions>
<execution> <execution>
<id>antlr</id> <id>antlr</id>

View File

@ -5,33 +5,43 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.Token;
import java.util.List; import java.util.List;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import de.dhbwstuttgart.environment.PackageCrawler; import de.dhbwstuttgart.environment.PackageCrawler;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.antlr.Java17Parser; import de.dhbwstuttgart.parser.antlr.Java17Parser;
import de.dhbwstuttgart.parser.antlr.Java17ParserBaseVisitor;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassOrInterfaceModifierContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassOrInterfaceModifierContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacedeclContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacedeclContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ModifierContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ModifierContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext; 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;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.statement.Statement; import de.dhbwstuttgart.syntaxtree.statement.Statement;
public class ASTGen extends Java17ParserBaseVisitor<Object> { public class ASTGen {
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<>();
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 Map<String, RefTypeOrTPHOrWildcardOrGeneric> fields = new HashMap<>(); private Map<String, RefTypeOrTPHOrWildcardOrGeneric> fields = new HashMap<>();
@ -45,6 +55,18 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
// globalen Datenbank benötigt. // globalen Datenbank benötigt.
this.globalGenerics = globalGenerics; this.globalGenerics = globalGenerics;
this.reg = reg; this.reg = reg;
this.allmodifiers.put(Modifier.toString(Modifier.PUBLIC), Modifier.PUBLIC);
this.allmodifiers.put(Modifier.toString(Modifier.PRIVATE), Modifier.PRIVATE);
this.allmodifiers.put(Modifier.toString(Modifier.PROTECTED), Modifier.PROTECTED);
this.allmodifiers.put(Modifier.toString(Modifier.ABSTRACT), Modifier.ABSTRACT);
this.allmodifiers.put(Modifier.toString(Modifier.STATIC), Modifier.STATIC);
this.allmodifiers.put(Modifier.toString(Modifier.STRICT), Modifier.STRICT);
this.allmodifiers.put(Modifier.toString(Modifier.FINAL), Modifier.FINAL);
this.allmodifiers.put(Modifier.toString(Modifier.TRANSIENT), Modifier.TRANSIENT);
this.allmodifiers.put(Modifier.toString(Modifier.VOLATILE), Modifier.VOLATILE);
this.allmodifiers.put(Modifier.toString(Modifier.SYNCHRONIZED), Modifier.SYNCHRONIZED);
this.allmodifiers.put(Modifier.toString(Modifier.NATIVE), Modifier.NATIVE);
this.allmodifiers.put(Modifier.toString(Modifier.INTERFACE), Modifier.INTERFACE);
} }
public JavaClassRegistry getReg() { public JavaClassRegistry getReg() {
@ -62,46 +84,55 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
return ret; return ret;
} }
public SourceFile generate(Java17Parser.SourceFileContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader) public SourceFile convert(Java17Parser.SourceFileContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader)
throws ClassNotFoundException { throws Exception {
Map<String, Integer> imports = GatherNames.getImports(ctx, packageCrawler, classLoader); SrcfileContext srcfile;
this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet());
return (SourceFile) this.visit(ctx);
}
@Override
public SourceFile visitSrcfile(Java17Parser.SrcfileContext ctx) {
List<ClassOrInterface> classes = new ArrayList<>(); List<ClassOrInterface> classes = new ArrayList<>();
for (Java17Parser.ClassOrInterfaceContext member : ctx.classOrInterface()) { if (ctx instanceof Java17Parser.SrcfileContext) {
srcfile = new SrcfileContext(ctx);
} else {
return new SourceFile(this.pkgName, classes, this.imports);
}
if (srcfile.packageDeclaration() != null)
this.pkgName = convert(srcfile.packageDeclaration());
Map<String, Integer> imports = GatherNames.getImports(srcfile, packageCrawler, classLoader);
this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet());
for (Java17Parser.ClassOrInterfaceContext type : srcfile.classOrInterface()) {
ClassorinterfacedeclContext clsoif; ClassorinterfacedeclContext clsoif;
if (member instanceof NoclassorinterfaceContext) { if (type instanceof NoclassorinterfaceContext) {
continue; continue;
} else { } else {
clsoif = new ClassorinterfacedeclContext(member); clsoif = new ClassorinterfacedeclContext(type);
} }
ClassOrInterface newClass; ClassOrInterface newClass;
int modifiers = 0; int modifiers = 0;
if (!clsoif.classOrInterfaceModifier().isEmpty()) { if (!clsoif.classOrInterfaceModifier().isEmpty()) {
for (Java17Parser.ClassOrInterfaceModifierContext mod : clsoif.classOrInterfaceModifier()) { for (Java17Parser.ClassOrInterfaceModifierContext mod : clsoif.classOrInterfaceModifier()) {
int newModifier = convertModifier(mod.getText()); int newMod = convertModifier(mod);
modifiers += newModifier; modifiers += newMod;
} }
} }
fieldInitializations = new ArrayList<>(); // PL 2019-10-22: muss für jede Klasse neu initilisiert werden fieldInitializations = new ArrayList<>(); // PL 2019-10-22: muss für jede Klasse neu initilisiert werden
if (clsoif.classDeclaration() != null) { if (clsoif.classDeclaration() != null) {
newClass = visitClassDeclaration(clsoif.classDeclaration()); newClass = convertClass(clsoif.classDeclaration(), modifiers);
} else { } else {
newClass = convertInterface(clsoif.interfaceDeclaration()); newClass = convertInterface(clsoif.interfaceDeclaration(), modifiers);
} }
classes.add(newClass); classes.add(newClass);
} }
return new SourceFile(this.pkgName, classes, this.imports); if (classes.size() > 0) {
return new SourceFile(this.pkgName, classes, this.imports);
} else {
throw new Exception("SourceFile enthält keine Klassen");
}
} }
@Override private String convert(Java17Parser.PackageDeclarationContext ctx) {
public ClassOrInterface visitClassDeclaration(Java17Parser.ClassDeclarationContext ctx) { return convertQualifiedName(ctx.qualifiedName());
String className = this.pkgName + (this.pkgName.length() > 0 ? "." : "") + ctx.identifier().getText(); }
private ClassOrInterface convertClass(Java17Parser.ClassDeclarationContext ctx, int modifiers) {
String className = this.pkgName.length() > 0 ? this.pkgName + "." : "" + 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?
throw new TypeinferenceException( throw new TypeinferenceException(
@ -122,35 +153,43 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
} 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);
List<Method> methodsAndConstructors = convertMethods(ctx.classBody(), name, superClass, generics); private RefType convert(Java17Parser.TypeTypeContext typeType) {
List<Method> methods = new ArrayList<>(); if (typeType.classOrInterfaceType() != null) {
List<Constructor> konstruktoren = new ArrayList<>(); throw new NotImplementedException();
// int noOfMethods = methods.size(); } else {
for (Method m : methodsAndConstructors) { RefTypeOrTPHOrWildcardOrGeneric ret = TypeGenerator.convertTypeName(
if (m instanceof Constructor) { typeType.classOrInterfaceType().typeIdentifier().getText(),
konstruktoren.add((Constructor) m); typeType.classOrInterfaceType().typeArguments()
.get(typeType.classOrInterfaceType().typeArguments().size() - 1),
typeType.getStart(), reg, globalGenerics);
if (ret instanceof RefType) {
return (RefType) ret;
} else { } else {
methods.add(m); throw new TypeinferenceException(typeType.getText() + " ist kein gültiger Supertyp", typeType.getStart());
} }
} }
if (konstruktoren.size() < 1) {// Standardkonstruktor anfügen: }
konstruktoren.add(
generateStandardConstructor( public Method convert(Java17Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass,
ctx.Identifier().getText(), name, superClass, RefType superClass, GenericsRegistry generics) {
genericClassParameters, offset));
return null;
}
public int convertModifier(Java17Parser.ClassOrInterfaceModifierContext ctx) {
int ret = 0;
if (ctx.annotation() != null) {
return ret;
} else {
for (String m : this.allmodifiers.keySet()) {
if (ctx.getText().contains(m))
ret += allmodifiers.get(m);
}
} }
return ret;
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)),
methods, konstruktoren, genericClassParameters, superClass,
isInterface, implementedInterfaces, offset);
} }
private GenericsRegistry createGenerics(Java17Parser.GenericDeclarationListContext ctx, JavaClassName parentClass, private GenericsRegistry createGenerics(Java17Parser.GenericDeclarationListContext ctx, JavaClassName parentClass,
@ -159,7 +198,6 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
ret.putAll(generics); ret.putAll(generics);
if (ctx == null) if (ctx == null)
return ret; return ret;
ret.putAll(visitGenericDeclarationList(ctx));
for (Java17Parser.GenericTypeVarContext tp : ctx.genericTypeVar()) { for (Java17Parser.GenericTypeVarContext tp : ctx.genericTypeVar()) {
ret.put(tp.identifier().getText(), new GenericContext(parentClass, parentMethod)); ret.put(tp.identifier().getText(), new GenericContext(parentClass, parentMethod));
TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret); TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret);
@ -167,26 +205,10 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
return ret; return ret;
} }
public static int convertModifier(String modifier) { private GenericDeclarationList createEmptyGenericDeclarationList(Token classNameIdentifier) {
HashMap<String, Integer> modifiers = new HashMap<>(); CommonToken gtvOffset = new CommonToken(classNameIdentifier);
modifiers.put(Modifier.toString(Modifier.PUBLIC), Modifier.PUBLIC); gtvOffset.setCharPositionInLine(gtvOffset.getCharPositionInLine() + classNameIdentifier.getText().length());
modifiers.put(Modifier.toString(Modifier.PRIVATE), Modifier.PRIVATE); gtvOffset.setStartIndex(gtvOffset.getStopIndex() + 1);
modifiers.put(Modifier.toString(Modifier.PROTECTED), Modifier.PROTECTED); return new GenericDeclarationList(new ArrayList<>(), gtvOffset);
modifiers.put(Modifier.toString(Modifier.ABSTRACT), Modifier.ABSTRACT);
modifiers.put(Modifier.toString(Modifier.STATIC), Modifier.STATIC);
modifiers.put(Modifier.toString(Modifier.STRICT), Modifier.STRICT);
modifiers.put(Modifier.toString(Modifier.FINAL), Modifier.FINAL);
modifiers.put(Modifier.toString(Modifier.TRANSIENT), Modifier.TRANSIENT);
modifiers.put(Modifier.toString(Modifier.VOLATILE), Modifier.VOLATILE);
modifiers.put(Modifier.toString(Modifier.SYNCHRONIZED), Modifier.SYNCHRONIZED);
modifiers.put(Modifier.toString(Modifier.NATIVE), Modifier.NATIVE);
modifiers.put(Modifier.toString(Modifier.INTERFACE), Modifier.INTERFACE);
int ret = 0;
for (String m : modifiers.keySet()) {
if (modifier.contains(m))
ret += modifiers.get(m);
}
return ret;
} }
} }

View File

@ -7,7 +7,6 @@ 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.ClassorinterfacedeclContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SrcfileContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SrcfileContext;
@ -34,7 +33,7 @@ import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode; import org.antlr.v4.runtime.tree.TerminalNode;
public class SyntaxTreeGenerator extends Java17ParserBaseVisitor<SourceFile> { public class SyntaxTreeGenerator {
private JavaClassRegistry reg; private JavaClassRegistry reg;
private final GenericsRegistry globalGenerics; private final GenericsRegistry globalGenerics;
private String pkgName = ""; private String pkgName = "";
@ -528,7 +527,7 @@ public class SyntaxTreeGenerator extends Java17ParserBaseVisitor<SourceFile> {
return ret; return ret;
for (Java17Parser.GenericTypeVarContext tp : ctx.genericTypeVar()) { for (Java17Parser.GenericTypeVarContext tp : ctx.genericTypeVar()) {
ret.put(tp.identifier().getText(), new GenericContext(parentClass, parentMethod)); ret.put(tp.identifier().getText(), new GenericContext(parentClass, parentMethod));
TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret); TypeGenerator.convert(tp.typeBound(), reg, ret);
} }
return ret; return ret;
} }

View File

@ -113,28 +113,27 @@ public class TypeGenerator {
genericsRegistry); genericsRegistry);
} }
public static GenericDeclarationList convert(Java17Parser.TypeParametersContext typeParametersContext, public static GenericDeclarationList convert(Java17Parser.GenericDeclarationListContext typeParametersContext,
JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) { JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
Token endOffset = typeParametersContext.getStop(); Token endOffset = typeParametersContext.getStop();
List<GenericTypeVar> typeVars = new ArrayList<>(); List<GenericTypeVar> typeVars = new ArrayList<>();
for (Java17Parser.TypeParameterContext typeParameter : typeParametersContext.typeParameterList() for (Java17Parser.GenericTypeVarContext typeParameter : typeParametersContext.genericTypeVar()) {
.typeParameter()) {
typeVars.add(convert(typeParameter, parentClass, parentMethod, reg, generics)); typeVars.add(convert(typeParameter, parentClass, parentMethod, reg, generics));
endOffset = typeParameter.getStop(); endOffset = typeParameter.getStop();
} }
return new GenericDeclarationList(typeVars, endOffset); return new GenericDeclarationList(typeVars, endOffset);
} }
public static GenericTypeVar convert(Java17Parser.TypeParameterContext typeParameter, JavaClassName parentClass, public static GenericTypeVar convert(Java17Parser.GenericTypeVarContext typeVar, JavaClassName parentClass,
String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) { String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
String name = typeParameter.Identifier().getText(); String name = typeVar.identifier().getText();
// TODO: Es müssen erst alle GenericTypeVars generiert werden, dann können die // TODO: Es müssen erst alle GenericTypeVars generiert werden, dann können die
// bounds dieser Generics ermittelt werden // bounds dieser Generics ermittelt werden
// Problem <A extends B, B> ist erlaubt, würde aber bei den Bounds von A den // Problem <A extends B, B> ist erlaubt, würde aber bei den Bounds von A den
// Generic B nicht als solchen erkennen // Generic B nicht als solchen erkennen
List<RefTypeOrTPHOrWildcardOrGeneric> bounds = TypeGenerator.convert(typeParameter.typeBound(), reg, generics); List<RefTypeOrTPHOrWildcardOrGeneric> bounds = TypeGenerator.convert(typeVar.typeBound(), reg, generics);
GenericTypeVar ret = new GenericTypeVar(name, bounds, typeParameter.getStart(), typeParameter.getStop()); GenericTypeVar ret = new GenericTypeVar(name, bounds, typeVar.getStart(), typeVar.getStop());
return ret; return ret;
} }

View File

@ -1,116 +1,102 @@
package de.dhbwstuttgart.parser.scope; package de.dhbwstuttgart.parser.scope;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*; import java.util.*;
import de.dhbwstuttgart.parser.antlr.Java17ParserBaseVisitor; import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacedeclContext;
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; import de.dhbwstuttgart.parser.antlr.Java17Parser.SrcfileContext;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.antlr.v4.runtime.tree.TerminalNode;
import de.dhbwstuttgart.environment.PackageCrawler; import de.dhbwstuttgart.environment.PackageCrawler;
import de.dhbwstuttgart.parser.antlr.Java17Parser; import de.dhbwstuttgart.parser.antlr.Java17Parser;
public class GatherNames { public class GatherNames {
public static Map<String, Integer> getNames(Java17Parser.SourceFileContext ctx, PackageCrawler packages, public static Map<String, Integer> getNames(SrcfileContext ctx, PackageCrawler packages,
ClassLoader classLoader) throws ClassNotFoundException { 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.ClassOrInterfaceContext member : ctx.classOrInterface()) {
if (typeDecl.interfaceDeclaration() != null) { if (member instanceof NoclassorinterfaceContext) {
if (typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null) { continue;
if (pkgName != "") { }
nameString = pkgName + "." ClassorinterfacedeclContext clsoif = new ClassorinterfacedeclContext(member);
+ typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); if (clsoif.interfaceDeclaration() != null) {
} else { if (pkgName != "") {
nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); nameString = pkgName + "."
} + clsoif.interfaceDeclaration().identifier().getText();
int numGenerics = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters() != null } else {
? typeDecl.interfaceDeclaration().normalInterfaceDeclaration().typeParameters().typeParameterList() nameString = clsoif.interfaceDeclaration().identifier().getText();
.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);
} }
int numGenerics = clsoif.interfaceDeclaration().genericDeclarationList() != null
? clsoif.interfaceDeclaration().genericDeclarationList().genericTypeVar().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(clsoif.classDeclaration().normalClassDeclaration().typeParameters() !=
* null){
* for(Java17Parser.TypeParameterContext tp :
* clsoif.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()) {
if (!pkgName.isEmpty()) { nameString = pkgName + "." + clsoif.classDeclaration().identifier().getText();
nameString = pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); } else {
} else { nameString = clsoif.classDeclaration().getText();
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());
* }
* }
*/
int numGenerics = typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null
? typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList()
.typeParameter().size()
: 0;
ret.put(nameString, numGenerics);
} }
// 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(clsoif.classDeclaration().normalClassDeclaration().typeParameters() !=
* null){
* for(Java17Parser.TypeParameterContext tp :
* clsoif.classDeclaration().normalClassDeclaration().typeParameters().
* typeParameterList().typeParameter()){
* this.reg.add(tp.Identifier().toString());
* }
* }
*/
int numGenerics = clsoif.classDeclaration().genericDeclarationList() != null
? clsoif.classDeclaration().genericDeclarationList().genericTypeVar().size()
: 0;
ret.put(nameString, numGenerics);
} }
} }
ret.putAll(getImports(ctx, packages, classLoader)); ret.putAll(getImports(ctx, packages, classLoader));
return ret; return ret;
} }
public static Map<String, Integer> getImports(Java17Parser.SourceFileContext ctx, PackageCrawler packages, public static Map<String, Integer> getImports(Java17Parser.SrcfileContext ctx, PackageCrawler packages,
ClassLoader classLoader) throws ClassNotFoundException { 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.MUL() == null) {
Class cl = classLoader.loadClass(importDeclCtx.singleTypeImportDeclaration().typeName().getText()); Class<?> cl = classLoader.loadClass(importDeclCtx.qualifiedName().getText());
ret.put(cl.getName(), cl.getTypeParameters().length); ret.put(cl.getName(), cl.getTypeParameters().length);
} else if (importDeclCtx.typeImportOnDemandDeclaration() != null) { } else if (importDeclCtx.MUL() != null) {
ret.putAll(packages.getClassNames(importDeclCtx.typeImportOnDemandDeclaration().packageOrTypeName().getText())); ret.putAll(packages.getClassNames(importDeclCtx.qualifiedName().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 {
ret.putAll(packages.getClassNames(importDeclCtx.staticImportOnDemandDeclaration().typeName().getText()));
} }
// Die Unterscheidungen für 'static imports' wurden herausgenommen, da sie den
// auszuführenden Code nicht beeinflussen
} }
return ret; return ret;
} }
private static String getPackageName(Java17Parser.CompilationUnitContext ctx) { private static String getPackageName(Java17Parser.SrcfileContext ctx) {
String pkgName = ""; String pkgName = "";
if (ctx.packageDeclaration() != null) { if (ctx.packageDeclaration() != null) {
for (TerminalNode t : ctx.packageDeclaration().Identifier()) { pkgName = ctx.packageDeclaration().qualifiedName().getText();
pkgName = pkgName + "." + t.toString();
}
pkgName = pkgName.substring(1);
} }
return pkgName; return pkgName;
} }

View File

@ -1,4 +1,5 @@
package de.dhbwstuttgart.syntaxtree; package de.dhbwstuttgart.syntaxtree;
import java.io.File; import java.io.File;
import java.util.*; import java.util.*;
@ -8,48 +9,50 @@ import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
//import sun.security.x509.X509CertInfo; //import sun.security.x509.X509CertInfo;
public class SourceFile extends SyntaxTreeNode {
public class SourceFile extends SyntaxTreeNode{
private String pkgName; private String pkgName;
public final List<ClassOrInterface> KlassenVektor; public final List<ClassOrInterface> KlassenVektor;
public final Set<JavaClassName> imports; public final Set<JavaClassName> imports;
/** /**
* Die SourceFile repräsntiert eine zu einem Syntaxbaum eingelesene Java-Datei. * Die SourceFile repräsntiert eine zu einem Syntaxbaum eingelesene Java-Datei.
* SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar. * SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar.
*/ */
public SourceFile(String pkgName, List<ClassOrInterface> classDefinitions, Set<JavaClassName> imports){ public SourceFile(String pkgName, List<ClassOrInterface> classDefinitions, Set<JavaClassName> imports) {
super(new NullToken()); super(new NullToken());
this.KlassenVektor = classDefinitions; if (classDefinitions.size() > 0) { // Enthält die Liste Klassen?
this.KlassenVektor = classDefinitions; // Klassen werden übernommen
} else {
this.KlassenVektor = null; // es handelt sich um ein "Java Module"
}
this.pkgName = pkgName; this.pkgName = pkgName;
this.imports = imports; this.imports = imports;
} }
public SourceFile(SourceFile sf) { public SourceFile(SourceFile sf) {
super(new NullToken()); super(new NullToken());
this.KlassenVektor = new ArrayList<>(sf.KlassenVektor); this.KlassenVektor = new ArrayList<>(sf.KlassenVektor);
this.imports = new HashSet<>(sf.imports); this.imports = new HashSet<>(sf.imports);
} }
public String getPkgName() {
public String getPkgName(){
return this.pkgName; return this.pkgName;
} }
// Get imports (to test implementation) // Get imports (to test implementation)
public Set<JavaClassName> getImports(){ public Set<JavaClassName> getImports() {
return this.imports; return this.imports;
} }
public List<ClassOrInterface> getClasses() { public List<ClassOrInterface> getClasses() {
return KlassenVektor; return KlassenVektor;
} }
public List<Method> getAllMethods() { public List<Method> getAllMethods() {
List<Method> ret = new ArrayList<>(); List<Method> ret = new ArrayList<>();
getClasses().forEach(cl -> ret.addAll(cl.getMethods())); getClasses().forEach(cl -> ret.addAll(cl.getMethods()));
return ret; return ret;
} }
@Override @Override