diff --git a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java index 9a632674..2f04423f 100644 --- a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -5,10 +5,7 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; +import java.util.*; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; @@ -55,7 +52,7 @@ public class CompilationEnvironment { } public JavaClassRegistry getRegistry(File forSourceFile) throws ClassNotFoundException, IOException { - List allNames; + Map allNames; CompilationUnitContext tree = JavaTXParser.parse(forSourceFile); allNames = GatherNames.getNames(tree, new PackageCrawler(librarys)); return new JavaClassRegistry(allNames); diff --git a/src/de/dhbwstuttgart/environment/PackageCrawler.java b/src/de/dhbwstuttgart/environment/PackageCrawler.java index cec63562..7c63a0b6 100644 --- a/src/de/dhbwstuttgart/environment/PackageCrawler.java +++ b/src/de/dhbwstuttgart/environment/PackageCrawler.java @@ -1,10 +1,7 @@ package de.dhbwstuttgart.environment; import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Set; +import java.util.*; import org.reflections.Reflections; import org.reflections.scanners.ResourcesScanner; @@ -66,14 +63,14 @@ public class PackageCrawler { return classes; } - public List getClassNames(String packageName){ - List nameList = new ArrayList(); + public Map getClassNames(String packageName){ + Map nameList = new HashMap<>(); Set> classes = getClassesInPackage(packageName); if(packageName.equals("java.lang") && ! classes.contains(Object.class)) { classes.add(Object.class); } for(Class c : classes){ - nameList.add(c.getName()); + nameList.put(c.getName(), c.getTypeParameters().length); } return nameList; } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index b5042fba..2cf41b43 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -14,6 +14,7 @@ import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import org.antlr.v4.runtime.Token; import java.util.ArrayList; @@ -131,7 +132,11 @@ public class TypeGenerator { } } if(typeArguments == null){ - return new RefType(reg.getName(name), offset); + List params = new ArrayList<>(); + for(int i = 0; i getNames(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException{ - List ret = new ArrayList<>(); + public static Map getNames(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException{ + Map ret = new HashMap<>(); String pkgName = getPackageName(ctx); String nameString = ""; for (Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ @@ -26,6 +28,8 @@ public class GatherNames { 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){ @@ -34,7 +38,7 @@ public class GatherNames { } } */ - ret.add(nameString); + ret.put(nameString, numGenerics); } } else{ @@ -53,30 +57,36 @@ public class GatherNames { } } */ - ret.add(nameString); + int numGenerics = typeDecl.classDeclaration().normalClassDeclaration().typeParameters()!=null? + typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter().size():0; + + ret.put(nameString, numGenerics); } } } - ret.addAll(getImports(ctx, packages)); + ret.putAll(getImports(ctx, packages)); return ret; } - private static List getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException { - List ret = new ArrayList(); - ret.addAll(packages.getClassNames("java.lang")); - for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ + private static Map getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException { + Map ret = new HashMap<>(); + ret.putAll(packages.getClassNames("java.lang")); + ClassLoader classLoader = ClassLoader.getSystemClassLoader(); + for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){ if(importDeclCtx.singleTypeImportDeclaration() != null){ - ret.add(importDeclCtx.singleTypeImportDeclaration().typeName().getText()); + Class cl = classLoader.loadClass(importDeclCtx.singleTypeImportDeclaration().typeName().getText()); + ret.put(cl.getName(), cl.getTypeParameters().length); } else if(importDeclCtx.typeImportOnDemandDeclaration() != null){ - ret.addAll(packages.getClassNames(importDeclCtx.typeImportOnDemandDeclaration().packageOrTypeName().getText())); + ret.putAll(packages.getClassNames(importDeclCtx.typeImportOnDemandDeclaration().packageOrTypeName().getText())); } else if(importDeclCtx.singleStaticImportDeclaration() != null){ - ret.add(importDeclCtx.singleStaticImportDeclaration().typeName().getText()+"."+importDeclCtx.singleStaticImportDeclaration().Identifier().getText()); - } + Class cl = classLoader.loadClass(importDeclCtx.singleStaticImportDeclaration().typeName().getText()+"."+importDeclCtx.singleStaticImportDeclaration().Identifier().getText()); + ret.put(cl.getName(), cl.getTypeParameters().length); + } else{ - ret.addAll(packages.getClassNames(importDeclCtx.staticImportOnDemandDeclaration().typeName().getText())); + ret.putAll(packages.getClassNames(importDeclCtx.staticImportOnDemandDeclaration().typeName().getText())); } } return ret; diff --git a/src/de/dhbwstuttgart/parser/scope/JavaClassName.java b/src/de/dhbwstuttgart/parser/scope/JavaClassName.java index a20ce2e7..b87ff719 100644 --- a/src/de/dhbwstuttgart/parser/scope/JavaClassName.java +++ b/src/de/dhbwstuttgart/parser/scope/JavaClassName.java @@ -49,8 +49,10 @@ public class JavaClassName { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); + /* result = prime * result + ((packageName == null) ? 0 : packageName.hashCode()); //PackageName does not infect hashCode + */ return result; } diff --git a/src/de/dhbwstuttgart/parser/scope/JavaClassRegistry.java b/src/de/dhbwstuttgart/parser/scope/JavaClassRegistry.java index a0cfacbf..d9ab804c 100644 --- a/src/de/dhbwstuttgart/parser/scope/JavaClassRegistry.java +++ b/src/de/dhbwstuttgart/parser/scope/JavaClassRegistry.java @@ -8,20 +8,16 @@ import java.util.*; * Speichert die Klassen f�r einen bestimmten Projektscope */ public class JavaClassRegistry { - final List existingClasses = new ArrayList<>(); + final Map existingClasses = new HashMap<>(); - public JavaClassRegistry(List initialNames){ - for(String name : initialNames){ - existingClasses.add(new JavaClassName(name)); + public JavaClassRegistry(Map initialNames){ + for(String name : initialNames.keySet()){ + existingClasses.put(new JavaClassName(name), initialNames.get(name)); } } - - public void add(String className){ - existingClasses.add(new JavaClassName(className)); - } public JavaClassName getName(String className) { - for(JavaClassName name : existingClasses){ + for(JavaClassName name : existingClasses.keySet()){ if(name.equals(new JavaClassName(className)))return name; } throw new NotImplementedException(); @@ -34,7 +30,7 @@ public class JavaClassRegistry { public List getAllFromPackage(String packageName) { List ret = new ArrayList<>(); - for(JavaClassName className : this.existingClasses){ + for(JavaClassName className : this.existingClasses.keySet()){ JavaClassName toCompare = new JavaClassName(packageName + "." + JavaClassName.stripClassName(className.toString())); if(toCompare.toString().equals(className.toString())){ ret.add(className); @@ -44,6 +40,10 @@ public class JavaClassRegistry { } public boolean contains(String whole) { - return existingClasses.contains(new JavaClassName(whole)); + return existingClasses.containsKey(new JavaClassName(whole)); + } + + public int getNumberOfGenerics(String name) { + return existingClasses.get(new JavaClassName(name)); } } diff --git a/test/javFiles/LambdaRunnable.jav b/test/javFiles/LambdaRunnable.jav new file mode 100644 index 00000000..982680c3 --- /dev/null +++ b/test/javFiles/LambdaRunnable.jav @@ -0,0 +1,11 @@ + +public class LamRunnable{ + + public LamRunnable(){ + + + Runnable lam = () -> {System.out.println("lambda");}; + lam.run(); + } +} + diff --git a/test/typeinference/RunnableTest.java b/test/typeinference/RunnableTest.java new file mode 100644 index 00000000..bc0c52ad --- /dev/null +++ b/test/typeinference/RunnableTest.java @@ -0,0 +1,9 @@ +package typeinference; + +import java.io.File; + +public class RunnableTest extends JavaTXCompilerTest{ + public RunnableTest() { + this.fileToTest = new File(rootDirectory+"LambdaRunnable.jav"); + } +} \ No newline at end of file