Imports in parser implementieren

This commit is contained in:
JanUlrich 2017-10-10 15:34:52 +02:00
parent cecec91f74
commit 9607fcf980
3 changed files with 26 additions and 46 deletions

View File

@ -76,13 +76,13 @@ public class SyntaxTreeGenerator{
newImports.add(convertSingleTypeImportDeclaration(importDeclCtx.singleTypeImportDeclaration())); newImports.add(convertSingleTypeImportDeclaration(importDeclCtx.singleTypeImportDeclaration()));
} }
else if(importDeclCtx.typeImportOnDemandDeclaration() != null){ else if(importDeclCtx.typeImportOnDemandDeclaration() != null){
newImports.add(convertTypeImportOnDemandDeclaration(importDeclCtx.typeImportOnDemandDeclaration())); newImports.addAll(convertTypeImportOnDemandDeclaration(importDeclCtx.typeImportOnDemandDeclaration()));
} }
else if(importDeclCtx.singleStaticImportDeclaration() != null){ else if(importDeclCtx.singleStaticImportDeclaration() != null){
newImports.add(convertSingleStaticImportDeclaration(importDeclCtx.singleStaticImportDeclaration())); newImports.add(convertSingleStaticImportDeclaration(importDeclCtx.singleStaticImportDeclaration()));
} }
else{ else{
newImports.add(convertStaticImportOnDemandDeclaration(importDeclCtx.staticImportOnDemandDeclaration())); newImports.addAll(convertStaticImportOnDemandDeclaration(importDeclCtx.staticImportOnDemandDeclaration()));
} }
} }
this.imports.addAll(newImports); this.imports.addAll(newImports);
@ -94,16 +94,16 @@ public class SyntaxTreeGenerator{
return ret; return ret;
} }
private JavaClassName convertTypeImportOnDemandDeclaration(Java8Parser.TypeImportOnDemandDeclarationContext ctx){ private List<JavaClassName> convertTypeImportOnDemandDeclaration(Java8Parser.TypeImportOnDemandDeclarationContext ctx){
return null; return reg.getAllFromPackage(ctx.packageOrTypeName().getText());
} }
private JavaClassName convertSingleStaticImportDeclaration(Java8Parser.SingleStaticImportDeclarationContext ctx){ private JavaClassName convertSingleStaticImportDeclaration(Java8Parser.SingleStaticImportDeclarationContext ctx){
return null; throw new NotImplementedException();
} }
private JavaClassName convertStaticImportOnDemandDeclaration(Java8Parser.StaticImportOnDemandDeclarationContext ctx){ private List<JavaClassName> convertStaticImportOnDemandDeclaration(Java8Parser.StaticImportOnDemandDeclarationContext ctx){
return null; return reg.getAllFromPackage(ctx.typeName().getText());
} }
private String getPackageFromClass(String cls){ private String getPackageFromClass(String cls){

View File

@ -1,5 +1,7 @@
package de.dhbwstuttgart.parser.scope; package de.dhbwstuttgart.parser.scope;
import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -35,7 +37,7 @@ public class JavaClassName {
} }
/** /**
* Gibt von einem Klassennamen nur den Namen der Klasse zurück * Gibt von einem Klassennamen nur den Namen der Klasse zur<EFBFBD>ck
* Beispiel: * Beispiel:
* java.lang.Object wird zu: Object * java.lang.Object wird zu: Object
*/ */

View File

@ -1,13 +1,14 @@
package de.dhbwstuttgart.parser.scope; package de.dhbwstuttgart.parser.scope;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import java.util.*; import java.util.*;
/** /**
* Speichert die Klassen für einen bestimmten Projektscope * Speichert die Klassen f<EFBFBD>r einen bestimmten Projektscope
*/ */
public class JavaClassRegistry { public class JavaClassRegistry {
final List<JavaClassName> existingClasses = new ArrayList<>(); final List<JavaClassName> existingClasses = new ArrayList<>();
final List<String> importedPackages = new ArrayList<>();
public JavaClassRegistry(List<String> initialNames){ public JavaClassRegistry(List<String> initialNames){
for(String name : initialNames){ for(String name : initialNames){
@ -23,13 +24,7 @@ public class JavaClassRegistry {
for(JavaClassName name : existingClasses){ for(JavaClassName name : existingClasses){
if(name.equals(new JavaClassName(className)))return name; if(name.equals(new JavaClassName(className)))return name;
} }
//Jetzt noch alle importierten Packages durchsuchen: throw new NotImplementedException();
JavaClassName ret = getClassFromImportedPackages(className);
if(ret == null){
throw new TypeNotPresentException(className, new Throwable());
}else{
return ret;
}
} }
@Override @Override
@ -37,35 +32,18 @@ public class JavaClassRegistry {
return existingClasses.toString(); return existingClasses.toString();
} }
public List<JavaClassName> getAllFromPackage(String packageName) {
List<JavaClassName> ret = new ArrayList<>();
for(JavaClassName className : this.existingClasses){
JavaClassName toCompare = new JavaClassName(packageName + "." + JavaClassName.stripClassName(className.toString()));
if(toCompare.toString().equals(className.toString())){
ret.add(className);
}
}
return ret;
}
public boolean contains(String whole) { public boolean contains(String whole) {
boolean ret = existingClasses.contains(new JavaClassName(whole)); return existingClasses.contains(new JavaClassName(whole));
if(ret == false){
JavaClassName imported = getClassFromImportedPackages(whole);
if(imported != null){
existingClasses.add(imported);
return true;
}else {
return false;
}
}else {
return true;
}
} }
private JavaClassName getClassFromImportedPackages(String className){
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String shortName = JavaClassName.stripClassName(className);
for(String packageName : importedPackages) {
try {
loader.loadClass(packageName+"."+shortName);
//Keine Exception! Die Klasse existiert:
JavaClassName ret = new JavaClassName(packageName+"."+shortName);
if(ret.equals(new JavaClassName(className)))return ret;
} catch (ClassNotFoundException e) {
//Die Klasse wurde nicht gefunden!
}
}
return null;
}
} }