Changed GatherNames to add types declared inside of other types to the JavaClassRegistry

This commit is contained in:
luca9913 2023-07-07 09:24:45 +02:00
parent 492cbe48e9
commit 939d402b1e
3 changed files with 118 additions and 77 deletions

View File

@ -1,4 +1,5 @@
import java.lang.Integer; import java.lang.Integer;
import java.lang.Boolean;
import java.lang.String; import java.lang.String;
import java.lang.Object; import java.lang.Object;

View File

@ -199,11 +199,7 @@ interfaceMemberDeclaration
: constDeclaration # interfaceconst : constDeclaration # interfaceconst
| interfaceMethodDeclaration # interfacemethod | interfaceMethodDeclaration # interfacemethod
| genericInterfaceMethodDeclaration # genericinterfacemethod | genericInterfaceMethodDeclaration # genericinterfacemethod
| interfaceDeclaration # subinterface | classOrInterface # subclassorinterface
| annotationTypeDeclaration # interfaceannotationtype
| classDeclaration # interfaceclass
| enumDeclaration # interfaceenum
| recordDeclaration # interfacerecord // Java17
; ;
constDeclaration constDeclaration
@ -391,11 +387,7 @@ annotationTypeElementDeclaration
annotationTypeElementRest annotationTypeElementRest
: typeType annotationMethodOrConstantRest ';' : typeType annotationMethodOrConstantRest ';'
| classDeclaration ';'? | classOrInterface ';'?
| interfaceDeclaration ';'?
| enumDeclaration ';'?
| annotationTypeDeclaration ';'?
| recordDeclaration ';'? // Java17
; ;
annotationMethodOrConstantRest annotationMethodOrConstantRest

View File

@ -1,28 +1,46 @@
package de.dhbwstuttgart.parser.scope; package de.dhbwstuttgart.parser.scope;
import java.util.*; import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.antlr.v4.runtime.ParserRuleContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassDeclarationContext;
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.environment.PackageCrawler;
import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.antlr.Java17Parser; import de.dhbwstuttgart.parser.antlr.Java17Parser;
import de.dhbwstuttgart.parser.antlr.Java17Parser.AnnotationTypeElementDeclarationContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassBodyContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassBodyDeclarationContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassOrInterfaceContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacedeclContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.EnumConstantContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.EnumConstantsContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.InterfaceBodyDeclarationContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.InterfacememberContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.MemberclassorinterfaceContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.MemberdeclContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SrcfileContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.SubclassorinterfaceContext;
public class GatherNames { public class GatherNames {
public static Map<String, Integer> getNames(SrcfileContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException { public static Map<String, Integer> getNames(SrcfileContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException {
Map<String, Integer> ret = new HashMap<>(); Map<String, Integer> ret = new HashMap<>();
String pkgName = getPackageName(ctx); for (Java17Parser.ClassOrInterfaceContext clsoifctx : ctx.classOrInterface()) {
String nameString = ""; if (clsoifctx instanceof NoclassorinterfaceContext) {
for (Java17Parser.ClassOrInterfaceContext member : ctx.classOrInterface()) {
if (member instanceof NoclassorinterfaceContext) {
continue; continue;
} }
ClassorinterfacedeclContext clsoif = (ClassorinterfacedeclContext) member; ret.putAll(getNames(clsoifctx, getPackageName(ctx), packages, classLoader));
}
ret.putAll(getImports(ctx, packages, classLoader));
return ret;
}
public static Map<String, Integer> getNames(ClassOrInterfaceContext clsoifctx, String pkgName, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException {
Map<String, Integer> ret = new HashMap<>();
ClassorinterfacedeclContext clsoif = (ClassorinterfacedeclContext) clsoifctx;
String nameString = "";
String fullname = clsoif.getChild(clsoif.getChildCount() - 1).getClass().getName(); String fullname = clsoif.getChild(clsoif.getChildCount() - 1).getClass().getName();
String classname = fullname.substring(fullname.indexOf("$") + 1); String classname = fullname.substring(fullname.indexOf("$") + 1);
int numGenerics = 0; int numGenerics = 0;
@ -38,6 +56,7 @@ public class GatherNames {
} }
numGenerics = clsoif.classDeclaration().genericDeclarationList() != null ? clsoif.classDeclaration().genericDeclarationList().genericTypeVar().size() : 0; numGenerics = clsoif.classDeclaration().genericDeclarationList() != null ? clsoif.classDeclaration().genericDeclarationList().genericTypeVar().size() : 0;
ret.put(nameString, numGenerics); ret.put(nameString, numGenerics);
ret.putAll(getNames(clsoif.classDeclaration().classBody().classBodyDeclaration(), pkgName, packages, classLoader));
break; break;
case "EnumDeclarationContext": case "EnumDeclarationContext":
if (!pkgName.isEmpty()) { if (!pkgName.isEmpty()) {
@ -47,6 +66,15 @@ public class GatherNames {
} }
numGenerics = 0; numGenerics = 0;
ret.put(nameString, numGenerics); ret.put(nameString, numGenerics);
EnumConstantsContext enumConstants = clsoif.enumDeclaration().enumConstants();
if (!Objects.isNull(enumConstants)) {
for (EnumConstantContext enumConstant : enumConstants.enumConstant()) {
ClassBodyContext enumConstClassBody = enumConstant.classBody();
if (!Objects.isNull(enumConstClassBody)) {
ret.putAll(getNames(enumConstClassBody.classBodyDeclaration(), pkgName, packages, classLoader));
}
}
}
break; break;
case "InterfaceDeclarationContext": case "InterfaceDeclarationContext":
if (pkgName != "") { if (pkgName != "") {
@ -56,6 +84,11 @@ public class GatherNames {
} }
numGenerics = clsoif.interfaceDeclaration().genericDeclarationList() != null ? clsoif.interfaceDeclaration().genericDeclarationList().genericTypeVar().size() : 0; numGenerics = clsoif.interfaceDeclaration().genericDeclarationList() != null ? clsoif.interfaceDeclaration().genericDeclarationList().genericTypeVar().size() : 0;
ret.put(nameString, numGenerics); ret.put(nameString, numGenerics);
for (InterfaceBodyDeclarationContext ifbody : clsoif.interfaceDeclaration().interfaceBody().interfaceBodyDeclaration()) {
if (ifbody instanceof InterfacememberContext member && member.interfaceMemberDeclaration() instanceof SubclassorinterfaceContext sub) {
ret.putAll(getNames(sub.classOrInterface(), pkgName, packages, classLoader));
}
}
break; break;
case "AnnotationTypeDeclarationContext": case "AnnotationTypeDeclarationContext":
if (pkgName != "") { if (pkgName != "") {
@ -65,6 +98,12 @@ public class GatherNames {
} }
numGenerics = 0; numGenerics = 0;
ret.put(nameString, numGenerics); ret.put(nameString, numGenerics);
for (AnnotationTypeElementDeclarationContext anTypeElem : clsoif.annotationTypeDeclaration().annotationTypeBody().annotationTypeElementDeclaration()) {
ClassOrInterfaceContext anClsoifctx = anTypeElem.annotationTypeElementRest().classOrInterface();
if (!Objects.isNull(anClsoifctx)) {
ret.putAll(getNames(anClsoifctx, pkgName, packages, classLoader));
}
}
break; break;
case "RecordDeclarationContext": case "RecordDeclarationContext":
if (pkgName != "") { if (pkgName != "") {
@ -74,12 +113,21 @@ public class GatherNames {
} }
numGenerics = clsoif.recordDeclaration().genericDeclarationList() != null ? clsoif.recordDeclaration().genericDeclarationList().genericTypeVar().size() : 0; numGenerics = clsoif.recordDeclaration().genericDeclarationList() != null ? clsoif.recordDeclaration().genericDeclarationList().genericTypeVar().size() : 0;
ret.put(nameString, numGenerics); ret.put(nameString, numGenerics);
ret.putAll(getNames(clsoif.recordDeclaration().recordBody().classBodyDeclaration(), pkgName, packages, classLoader));
break; break;
default: default:
throw new NotImplementedException(); throw new NotImplementedException();
} }
return ret;
}
public static Map<String, Integer> getNames(List<ClassBodyDeclarationContext> clsBodyDecl, String pkgName, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException {
Map<String, Integer> ret = new HashMap<>();
for (ClassBodyDeclarationContext clsbody : clsBodyDecl) {
if (clsbody instanceof MemberdeclContext member && member.memberDeclaration() instanceof MemberclassorinterfaceContext memberclsoifctx) {
ret.putAll(getNames(memberclsoifctx.classOrInterface(), pkgName, packages, classLoader));
}
} }
ret.putAll(getImports(ctx, packages, classLoader));
return ret; return ret;
} }