Typdeklarationen wie List, werden jetzt automatisch TPHs eingesetzt. (Als hätte man den Diamond-Operator benutzt)
This commit is contained in:
parent
16e14f9363
commit
9a886ed223
@ -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<String> allNames;
|
||||
Map<String, Integer> allNames;
|
||||
CompilationUnitContext tree = JavaTXParser.parse(forSourceFile);
|
||||
allNames = GatherNames.getNames(tree, new PackageCrawler(librarys));
|
||||
return new JavaClassRegistry(allNames);
|
||||
|
@ -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<String> getClassNames(String packageName){
|
||||
List<String> nameList = new ArrayList();
|
||||
public Map<String, Integer> getClassNames(String packageName){
|
||||
Map<String, Integer> nameList = new HashMap<>();
|
||||
Set<Class<?>> 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;
|
||||
}
|
||||
|
@ -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<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
|
||||
for(int i = 0; i<reg.getNumberOfGenerics(name);i++){
|
||||
params.add(TypePlaceholder.fresh(offset));
|
||||
}
|
||||
return new RefType(reg.getName(name), params, offset);
|
||||
}else{
|
||||
return new RefType(reg.getName(name), convert(typeArguments, reg, generics), offset);
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
package de.dhbwstuttgart.parser.scope;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import de.dhbwstuttgart.parser.antlr.Java8BaseListener;
|
||||
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
|
||||
@ -13,8 +15,8 @@ import de.dhbwstuttgart.parser.antlr.Java8Parser;
|
||||
|
||||
public class GatherNames {
|
||||
|
||||
public static List<String> getNames(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException{
|
||||
List<String> ret = new ArrayList<>();
|
||||
public static Map<String, Integer> getNames(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException{
|
||||
Map<String, Integer> 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<String> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException {
|
||||
List<String> ret = new ArrayList();
|
||||
ret.addAll(packages.getClassNames("java.lang"));
|
||||
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
|
||||
private static Map<String, Integer> getImports(Java8Parser.CompilationUnitContext ctx, PackageCrawler packages) throws ClassNotFoundException {
|
||||
Map<String, Integer> 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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -8,20 +8,16 @@ import java.util.*;
|
||||
* Speichert die Klassen f<EFBFBD>r einen bestimmten Projektscope
|
||||
*/
|
||||
public class JavaClassRegistry {
|
||||
final List<JavaClassName> existingClasses = new ArrayList<>();
|
||||
final Map<JavaClassName, Integer> existingClasses = new HashMap<>();
|
||||
|
||||
public JavaClassRegistry(List<String> initialNames){
|
||||
for(String name : initialNames){
|
||||
existingClasses.add(new JavaClassName(name));
|
||||
public JavaClassRegistry(Map<String, Integer> 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<JavaClassName> getAllFromPackage(String packageName) {
|
||||
List<JavaClassName> 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));
|
||||
}
|
||||
}
|
||||
|
11
test/javFiles/LambdaRunnable.jav
Normal file
11
test/javFiles/LambdaRunnable.jav
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
public class LamRunnable{
|
||||
|
||||
public LamRunnable(){
|
||||
|
||||
|
||||
Runnable lam = () -> {System.out.println("lambda");};
|
||||
lam.run();
|
||||
}
|
||||
}
|
||||
|
9
test/typeinference/RunnableTest.java
Normal file
9
test/typeinference/RunnableTest.java
Normal file
@ -0,0 +1,9 @@
|
||||
package typeinference;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class RunnableTest extends JavaTXCompilerTest{
|
||||
public RunnableTest() {
|
||||
this.fileToTest = new File(rootDirectory+"LambdaRunnable.jav");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user