Die getRegistry Methode der CompilationEnvironment Klasse beachtet jetzt auch alle .class Files, welche sich im gleichen Ordner befinden
This commit is contained in:
parent
2e421cccaa
commit
691d31df5e
@ -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;
|
||||
|
@ -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<String, Integer> 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<Class> loadDefaultPackageClasses(File forSourceFile, ClassLoader classLoader) throws IOException, ClassNotFoundException {
|
||||
List<Class> 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<URL> 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<ClassOrInterface> getAllAvailableClasses() {
|
||||
List<ClassOrInterface> ret = new ArrayList<>();
|
||||
for(Class c : new PackageCrawler(librarys).getAllAvailableClasses()){
|
||||
|
@ -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<String, Integer> getUsedTypes(Java8Parser.CompilationUnitContext ctx, ClassLoader classLoader, Collection<String> alreadyUsedNames) {
|
||||
Map<String, Integer> 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<String, Integer> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages, ClassLoader classLoader) throws ClassNotFoundException {
|
||||
Map<String, Integer> ret = new HashMap<>();
|
||||
//ret.putAll(packages.getClassNames("java.lang"));
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,7 @@
|
||||
package de.test;
|
||||
|
||||
class ImportTestDefault{
|
||||
void methode(){
|
||||
new ToImport();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user