forked from JavaTX/JavaCompilerCore
Visitor Ansatz verworfen, STG in ASTGen neu aufsetzen
This commit is contained in:
parent
a85b60b95f
commit
a13294edf4
4
pom.xml
4
pom.xml
@ -62,10 +62,6 @@ http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-maven-plugin</artifactId>
|
||||
<version>4.8-1</version>
|
||||
<configuration>
|
||||
<visitor>true</visitor>
|
||||
<listener>false</listener>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>antlr</id>
|
||||
|
@ -5,33 +5,43 @@ import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.antlr.v4.runtime.CommonToken;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
import java.util.List;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import de.dhbwstuttgart.environment.PackageCrawler;
|
||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||
import de.dhbwstuttgart.exceptions.TypeinferenceException;
|
||||
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.ClassorinterfacedeclContext;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.ModifierContext;
|
||||
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;
|
||||
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
|
||||
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.SyntaxTreeNode;
|
||||
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefType;
|
||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||
import de.dhbwstuttgart.syntaxtree.statement.Statement;
|
||||
|
||||
public class ASTGen extends Java17ParserBaseVisitor<Object> {
|
||||
public class ASTGen {
|
||||
private JavaClassRegistry reg;
|
||||
private final GenericsRegistry globalGenerics;
|
||||
private String pkgName = "";
|
||||
Set<JavaClassName> imports = new HashSet<>();
|
||||
HashMap<String, Integer> allmodifiers = new HashMap<>();
|
||||
// PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH
|
||||
// bekommen
|
||||
private Map<String, RefTypeOrTPHOrWildcardOrGeneric> fields = new HashMap<>();
|
||||
@ -45,6 +55,18 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
|
||||
// globalen Datenbank benötigt.
|
||||
this.globalGenerics = globalGenerics;
|
||||
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() {
|
||||
@ -62,46 +84,55 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public SourceFile generate(Java17Parser.SourceFileContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader)
|
||||
throws ClassNotFoundException {
|
||||
Map<String, Integer> imports = GatherNames.getImports(ctx, packageCrawler, classLoader);
|
||||
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) {
|
||||
public SourceFile convert(Java17Parser.SourceFileContext ctx, PackageCrawler packageCrawler, ClassLoader classLoader)
|
||||
throws Exception {
|
||||
SrcfileContext srcfile;
|
||||
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;
|
||||
if (member instanceof NoclassorinterfaceContext) {
|
||||
if (type instanceof NoclassorinterfaceContext) {
|
||||
continue;
|
||||
} else {
|
||||
clsoif = new ClassorinterfacedeclContext(member);
|
||||
clsoif = new ClassorinterfacedeclContext(type);
|
||||
}
|
||||
ClassOrInterface newClass;
|
||||
int modifiers = 0;
|
||||
if (!clsoif.classOrInterfaceModifier().isEmpty()) {
|
||||
for (Java17Parser.ClassOrInterfaceModifierContext mod : clsoif.classOrInterfaceModifier()) {
|
||||
int newModifier = convertModifier(mod.getText());
|
||||
modifiers += newModifier;
|
||||
int newMod = convertModifier(mod);
|
||||
modifiers += newMod;
|
||||
}
|
||||
}
|
||||
fieldInitializations = new ArrayList<>(); // PL 2019-10-22: muss für jede Klasse neu initilisiert werden
|
||||
if (clsoif.classDeclaration() != null) {
|
||||
newClass = visitClassDeclaration(clsoif.classDeclaration());
|
||||
newClass = convertClass(clsoif.classDeclaration(), modifiers);
|
||||
} else {
|
||||
newClass = convertInterface(clsoif.interfaceDeclaration());
|
||||
newClass = convertInterface(clsoif.interfaceDeclaration(), modifiers);
|
||||
}
|
||||
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
|
||||
public ClassOrInterface visitClassDeclaration(Java17Parser.ClassDeclarationContext ctx) {
|
||||
String className = this.pkgName + (this.pkgName.length() > 0 ? "." : "") + ctx.identifier().getText();
|
||||
private String convert(Java17Parser.PackageDeclarationContext ctx) {
|
||||
return convertQualifiedName(ctx.qualifiedName());
|
||||
}
|
||||
|
||||
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
|
||||
if (!name.toString().equals(className)) { // Kommt die Klasse schon in einem anderen Package vor?
|
||||
throw new TypeinferenceException(
|
||||
@ -122,35 +153,43 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
|
||||
} else {
|
||||
superClass = new RefType(ASTFactory.createObjectClass().getClassName(), ctx.getStart());
|
||||
}
|
||||
List<Field> fielddecl = convertFields(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<>();
|
||||
// int noOfMethods = methods.size();
|
||||
for (Method m : methodsAndConstructors) {
|
||||
if (m instanceof Constructor) {
|
||||
konstruktoren.add((Constructor) m);
|
||||
// List<Field> fielddecl = convertFields(ctx.classBody(), generics);
|
||||
}
|
||||
|
||||
private RefType convert(Java17Parser.TypeTypeContext typeType) {
|
||||
if (typeType.classOrInterfaceType() != null) {
|
||||
throw new NotImplementedException();
|
||||
} else {
|
||||
RefTypeOrTPHOrWildcardOrGeneric ret = TypeGenerator.convertTypeName(
|
||||
typeType.classOrInterfaceType().typeIdentifier().getText(),
|
||||
typeType.classOrInterfaceType().typeArguments()
|
||||
.get(typeType.classOrInterfaceType().typeArguments().size() - 1),
|
||||
typeType.getStart(), reg, globalGenerics);
|
||||
if (ret instanceof RefType) {
|
||||
return (RefType) ret;
|
||||
} 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(
|
||||
ctx.Identifier().getText(), name, superClass,
|
||||
genericClassParameters, offset));
|
||||
}
|
||||
|
||||
public Method convert(Java17Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass,
|
||||
RefType superClass, GenericsRegistry generics) {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private GenericsRegistry createGenerics(Java17Parser.GenericDeclarationListContext ctx, JavaClassName parentClass,
|
||||
@ -159,7 +198,6 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
|
||||
ret.putAll(generics);
|
||||
if (ctx == null)
|
||||
return ret;
|
||||
ret.putAll(visitGenericDeclarationList(ctx));
|
||||
for (Java17Parser.GenericTypeVarContext tp : ctx.genericTypeVar()) {
|
||||
ret.put(tp.identifier().getText(), new GenericContext(parentClass, parentMethod));
|
||||
TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret);
|
||||
@ -167,26 +205,10 @@ public class ASTGen extends Java17ParserBaseVisitor<Object> {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static int convertModifier(String modifier) {
|
||||
HashMap<String, Integer> modifiers = new HashMap<>();
|
||||
modifiers.put(Modifier.toString(Modifier.PUBLIC), Modifier.PUBLIC);
|
||||
modifiers.put(Modifier.toString(Modifier.PRIVATE), Modifier.PRIVATE);
|
||||
modifiers.put(Modifier.toString(Modifier.PROTECTED), Modifier.PROTECTED);
|
||||
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;
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ 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;
|
||||
@ -34,7 +33,7 @@ import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
|
||||
public class SyntaxTreeGenerator extends Java17ParserBaseVisitor<SourceFile> {
|
||||
public class SyntaxTreeGenerator {
|
||||
private JavaClassRegistry reg;
|
||||
private final GenericsRegistry globalGenerics;
|
||||
private String pkgName = "";
|
||||
@ -528,7 +527,7 @@ public class SyntaxTreeGenerator extends Java17ParserBaseVisitor<SourceFile> {
|
||||
return ret;
|
||||
for (Java17Parser.GenericTypeVarContext tp : ctx.genericTypeVar()) {
|
||||
ret.put(tp.identifier().getText(), new GenericContext(parentClass, parentMethod));
|
||||
TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret);
|
||||
TypeGenerator.convert(tp.typeBound(), reg, ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -113,28 +113,27 @@ public class TypeGenerator {
|
||||
genericsRegistry);
|
||||
}
|
||||
|
||||
public static GenericDeclarationList convert(Java17Parser.TypeParametersContext typeParametersContext,
|
||||
public static GenericDeclarationList convert(Java17Parser.GenericDeclarationListContext typeParametersContext,
|
||||
JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) {
|
||||
Token endOffset = typeParametersContext.getStop();
|
||||
List<GenericTypeVar> typeVars = new ArrayList<>();
|
||||
for (Java17Parser.TypeParameterContext typeParameter : typeParametersContext.typeParameterList()
|
||||
.typeParameter()) {
|
||||
for (Java17Parser.GenericTypeVarContext typeParameter : typeParametersContext.genericTypeVar()) {
|
||||
typeVars.add(convert(typeParameter, parentClass, parentMethod, reg, generics));
|
||||
endOffset = typeParameter.getStop();
|
||||
}
|
||||
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 name = typeParameter.Identifier().getText();
|
||||
String name = typeVar.identifier().getText();
|
||||
// TODO: Es müssen erst alle GenericTypeVars generiert werden, dann können die
|
||||
// bounds dieser Generics ermittelt werden
|
||||
// Problem <A extends B, B> ist erlaubt, würde aber bei den Bounds von A den
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
@ -1,116 +1,102 @@
|
||||
package de.dhbwstuttgart.parser.scope;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.*;
|
||||
|
||||
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;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacedeclContext;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser.SrcfileContext;
|
||||
import de.dhbwstuttgart.environment.PackageCrawler;
|
||||
import de.dhbwstuttgart.parser.antlr.Java17Parser;
|
||||
|
||||
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 {
|
||||
Map<String, Integer> ret = new HashMap<>();
|
||||
String pkgName = getPackageName(ctx);
|
||||
String nameString = "";
|
||||
for (Java17Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()) {
|
||||
if (typeDecl.interfaceDeclaration() != null) {
|
||||
if (typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null) {
|
||||
if (pkgName != "") {
|
||||
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());
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
ret.put(nameString, numGenerics);
|
||||
for (Java17Parser.ClassOrInterfaceContext member : ctx.classOrInterface()) {
|
||||
if (member instanceof NoclassorinterfaceContext) {
|
||||
continue;
|
||||
}
|
||||
ClassorinterfacedeclContext clsoif = new ClassorinterfacedeclContext(member);
|
||||
if (clsoif.interfaceDeclaration() != null) {
|
||||
if (pkgName != "") {
|
||||
nameString = pkgName + "."
|
||||
+ clsoif.interfaceDeclaration().identifier().getText();
|
||||
} else {
|
||||
nameString = clsoif.interfaceDeclaration().identifier().getText();
|
||||
}
|
||||
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 {
|
||||
if (typeDecl.classDeclaration().normalClassDeclaration() != null) {
|
||||
if (!pkgName.isEmpty()) {
|
||||
nameString = pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
|
||||
} 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());
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
int numGenerics = typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null
|
||||
? typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList()
|
||||
.typeParameter().size()
|
||||
: 0;
|
||||
|
||||
ret.put(nameString, numGenerics);
|
||||
if (!pkgName.isEmpty()) {
|
||||
nameString = pkgName + "." + clsoif.classDeclaration().identifier().getText();
|
||||
} else {
|
||||
nameString = clsoif.classDeclaration().getText();
|
||||
}
|
||||
// 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));
|
||||
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 {
|
||||
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());
|
||||
if (importDeclCtx.MUL() == null) {
|
||||
Class<?> cl = classLoader.loadClass(importDeclCtx.qualifiedName().getText());
|
||||
ret.put(cl.getName(), cl.getTypeParameters().length);
|
||||
} 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());
|
||||
ret.put(cl.getName(), cl.getTypeParameters().length);
|
||||
} else {
|
||||
ret.putAll(packages.getClassNames(importDeclCtx.staticImportOnDemandDeclaration().typeName().getText()));
|
||||
} else if (importDeclCtx.MUL() != null) {
|
||||
ret.putAll(packages.getClassNames(importDeclCtx.qualifiedName().getText()));
|
||||
}
|
||||
// Die Unterscheidungen für 'static imports' wurden herausgenommen, da sie den
|
||||
// auszuführenden Code nicht beeinflussen
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static String getPackageName(Java17Parser.CompilationUnitContext ctx) {
|
||||
private static String getPackageName(Java17Parser.SrcfileContext ctx) {
|
||||
String pkgName = "";
|
||||
if (ctx.packageDeclaration() != null) {
|
||||
for (TerminalNode t : ctx.packageDeclaration().Identifier()) {
|
||||
pkgName = pkgName + "." + t.toString();
|
||||
}
|
||||
pkgName = pkgName.substring(1);
|
||||
pkgName = ctx.packageDeclaration().qualifiedName().getText();
|
||||
}
|
||||
return pkgName;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
package de.dhbwstuttgart.syntaxtree;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
@ -8,37 +9,39 @@ import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
|
||||
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
|
||||
//import sun.security.x509.X509CertInfo;
|
||||
|
||||
|
||||
public class SourceFile extends SyntaxTreeNode{
|
||||
public class SourceFile extends SyntaxTreeNode {
|
||||
private String pkgName;
|
||||
|
||||
public final List<ClassOrInterface> KlassenVektor;
|
||||
public final Set<JavaClassName> imports;
|
||||
|
||||
/**
|
||||
* Die SourceFile repräsntiert eine zu einem Syntaxbaum eingelesene Java-Datei.
|
||||
* SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar.
|
||||
*/
|
||||
public SourceFile(String pkgName, List<ClassOrInterface> classDefinitions, Set<JavaClassName> imports){
|
||||
* Die SourceFile repräsntiert eine zu einem Syntaxbaum eingelesene Java-Datei.
|
||||
* SourceFile stellt dabei den Wurzelknoten des Syntaxbaumes dar.
|
||||
*/
|
||||
public SourceFile(String pkgName, List<ClassOrInterface> classDefinitions, Set<JavaClassName> imports) {
|
||||
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.imports = imports;
|
||||
}
|
||||
|
||||
public SourceFile(SourceFile sf) {
|
||||
super(new NullToken());
|
||||
this.KlassenVektor = new ArrayList<>(sf.KlassenVektor);
|
||||
this.imports = new HashSet<>(sf.imports);
|
||||
super(new NullToken());
|
||||
this.KlassenVektor = new ArrayList<>(sf.KlassenVektor);
|
||||
this.imports = new HashSet<>(sf.imports);
|
||||
}
|
||||
|
||||
|
||||
public String getPkgName(){
|
||||
public String getPkgName() {
|
||||
return this.pkgName;
|
||||
}
|
||||
|
||||
// Get imports (to test implementation)
|
||||
public Set<JavaClassName> getImports(){
|
||||
public Set<JavaClassName> getImports() {
|
||||
return this.imports;
|
||||
}
|
||||
|
||||
@ -47,9 +50,9 @@ public class SourceFile extends SyntaxTreeNode{
|
||||
}
|
||||
|
||||
public List<Method> getAllMethods() {
|
||||
List<Method> ret = new ArrayList<>();
|
||||
getClasses().forEach(cl -> ret.addAll(cl.getMethods()));
|
||||
return ret;
|
||||
List<Method> ret = new ArrayList<>();
|
||||
getClasses().forEach(cl -> ret.addAll(cl.getMethods()));
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user