diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 98626fde..498ea41b 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -14,6 +14,7 @@ import de.dhbwstuttgart.parser.antlr.Java8Parser.CompilationUnitContext; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.TypeScope; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; diff --git a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java index 43196b6f..05a8055c 100644 --- a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -1,15 +1,18 @@ package de.dhbwstuttgart.environment; import java.io.File; +import java.io.FilenameFilter; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.*; +import com.google.common.collect.Lists; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import org.antlr.v4.runtime.tree.TerminalNode; import org.reflections.Reflections; import org.reflections.scanners.ResourcesScanner; import org.reflections.scanners.SubTypesScanner; @@ -70,9 +73,37 @@ public class CompilationEnvironment { Map allNames; CompilationUnitContext tree = JavaTXParser.parse(forSourceFile); allNames = GatherNames.getNames(tree, packageCrawler, classLoader); + for(Class c : loadDefaultPackageClasses(forSourceFile, classLoader)){ + allNames.put(c.getName(), c.getTypeParameters().length); + } return new JavaClassRegistry(allNames); } + public List loadDefaultPackageClasses(File forSourceFile, ClassLoader classLoader) throws IOException, ClassNotFoundException { + List ret = new ArrayList<>(); + String packageName = getPackageName(JavaTXParser.parse(forSourceFile)); + //Set classLoader to include default package for this specific source file + File dir = new File(forSourceFile.getParent()); + String path = "file://" + dir.toString().substring(0, dir.toString().length() - packageName.length() + 1); + ArrayList defaultPath = Lists.newArrayList(new URL(path)); + classLoader = new URLClassLoader(defaultPath.toArray(new URL[0]), classLoader); + //Gather all names in the default package for this source file (classes that are imported by default) + File [] files = dir.listFiles((dir1, name) -> name.endsWith(".class")); + if(files != null)for (File classFile : files) { + String className = classFile.getName().substring(0,classFile.getName().length()-6); + ret.add(classLoader.loadClass(packageName + className)); + } + return ret; + } + + private String getPackageName(CompilationUnitContext forTree){ + String packageName = ""; + for(TerminalNode subPackage : forTree.packageDeclaration().Identifier()){ + packageName += subPackage.toString() + "."; + } + return packageName; + } + public List getAllAvailableClasses() { List ret = new ArrayList<>(); for(Class c : new PackageCrawler(librarys).getAllAvailableClasses()){ diff --git a/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java b/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java index 757c323b..8e93355b 100644 --- a/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java +++ b/src/main/java/de/dhbwstuttgart/parser/scope/GatherNames.java @@ -65,54 +65,9 @@ public class GatherNames { } } ret.putAll(getImports(ctx, packages, classLoader)); - ret.putAll(getUsedTypes(ctx, classLoader, ret.keySet())); return ret; } - private static Map getUsedTypes(Java8Parser.CompilationUnitContext ctx, ClassLoader classLoader, Collection alreadyUsedNames) { - Map ret = new HashMap<>(); - new ParseTreeWalker().walk(new Java8BaseListener(){ - @Override - public void enterUnannClassType(Java8Parser.UnannClassTypeContext ctx) { - addClass(ctx.Identifier().getText()); - } - - @Override - public void enterUnannClassType_lf_unannClassOrInterfaceType(Java8Parser.UnannClassType_lf_unannClassOrInterfaceTypeContext ctx) { - addClass(ctx.Identifier().getText()); - } - - @Override - public void enterUnannClassType_lfno_unannClassOrInterfaceType(Java8Parser.UnannClassType_lfno_unannClassOrInterfaceTypeContext ctx) { - addClass(ctx.Identifier().getText()); - } - - @Override - public void enterClassInstanceCreationExpression(Java8Parser.ClassInstanceCreationExpressionContext ctx) { - addClass(ctx.Identifier(0).getText()); - } - - @Override - public void enterClassInstanceCreationExpression_lf_primary(Java8Parser.ClassInstanceCreationExpression_lf_primaryContext ctx) { - addClass(ctx.Identifier().getText()); - } - - @Override - public void enterClassInstanceCreationExpression_lfno_primary(Java8Parser.ClassInstanceCreationExpression_lfno_primaryContext ctx) { - addClass(ctx.Identifier(0).getText()); - } - - private void addClass(String name) { - try { - Class cl = classLoader.loadClass(name); - ret.put(cl.getName(), cl.getTypeParameters().length); - }catch (ClassNotFoundException e){ - } - } - }, ctx); - return ret; - } - public static Map getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException { Map ret = new HashMap<>(); //ret.putAll(packages.getClassNames("java.lang")); diff --git a/src/test/java/packages/ImportTest.java b/src/test/java/packages/ImportTest.java index b13918c6..4ae04779 100644 --- a/src/test/java/packages/ImportTest.java +++ b/src/test/java/packages/ImportTest.java @@ -25,6 +25,12 @@ public class ImportTest extends TestCase { File f = new File(rootDirectory + "output/de/test/ToImport.class"); assertTrue(f.exists()); + compiler = new JavaTXCompiler(new File(rootDirectory+"ToImport.jav")); + compiler.typeInference(); + compiler.generateBytecode(null); + f = new File(rootDirectory + "ToImport.class"); + assertTrue(f.exists()); + compiler = new JavaTXCompiler(new File(rootDirectory+"subpackage1/ToImport2.jav")); compiler.typeInference(); compiler.generateBytecode(rootDirectory + "output/"); @@ -83,4 +89,20 @@ public class ImportTest extends TestCase { f = new File(rootDirectory + "ImportTest2.class"); assertTrue(f.exists()); } + + @Test + public void testImportDefaultPackage() throws IOException, ClassNotFoundException { + JavaTXCompiler compiler = new JavaTXCompiler( + Lists.newArrayList(new File(rootDirectory+"ImportTestDefault.jav"))); + compiler.typeInference(); + File f = new File(rootDirectory + "ImportTestDefault.class"); + if(f.exists() && !f.isDirectory()) { + f.delete(); + } + compiler.generateBytecode(null); + f = new File(rootDirectory + "ImportTestDefault.class"); + assertTrue(f.exists()); + } + + } diff --git a/src/test/resources/javFiles/packageTest/de/test/ImportTestDefault.jav b/src/test/resources/javFiles/packageTest/de/test/ImportTestDefault.jav new file mode 100644 index 00000000..ecd8d7c5 --- /dev/null +++ b/src/test/resources/javFiles/packageTest/de/test/ImportTestDefault.jav @@ -0,0 +1,7 @@ +package de.test; + +class ImportTestDefault{ + void methode(){ + new ToImport(); + } +} \ No newline at end of file