Fehler in TypeInsert beheben

This commit is contained in:
JanUlrich 2017-08-22 16:45:28 +02:00
parent 52f562b570
commit 3274f12922
9 changed files with 245 additions and 44 deletions

View File

@ -18,6 +18,7 @@ import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import org.antlr.v4.runtime.Token;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TypeGenerator {
@ -92,7 +93,7 @@ public class TypeGenerator {
public static RefTypeOrTPHOrWildcardOrGeneric convertTypeName(
String name, Java8Parser.TypeArgumentsContext typeArguments, Token offset, JavaClassRegistry reg, GenericsRegistry generics){
if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein:
if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein
if(generics.keySet().contains(name)){
return new GenericRefType(new GenericTypeName(generics.get(name),name), offset);
}else{

View File

@ -24,6 +24,11 @@ public abstract class AbstractASTWalker implements ASTVisitor{
}
}
@Override
public void visit(GenericTypeVar genericTypeVar) {
}
@Override
public void visit(FormalParameter formalParameter) {
formalParameter.getType().accept(this);
@ -87,11 +92,21 @@ public abstract class AbstractASTWalker implements ASTVisitor{
superWildcardType.getInnerType().accept(this);
}
@Override
public void visit(TypePlaceholder typePlaceholder) {
}
@Override
public void visit(ExtendsWildcardType extendsWildcardType) {
extendsWildcardType.getInnerType().accept(this);
}
@Override
public void visit(GenericRefType genericRefType) {
}
@Override
public void visit(LambdaExpression lambdaExpression) {
lambdaExpression.params.accept(this);
@ -104,6 +119,11 @@ public abstract class AbstractASTWalker implements ASTVisitor{
assign.rightSide.accept(this);
}
@Override
public void visit(Binary binary) {
}
@Override
public void visit(Block block) {
for(Statement stmt : block.getStatements()){
@ -111,11 +131,41 @@ public abstract class AbstractASTWalker implements ASTVisitor{
}
}
@Override
public void visit(CastExpr castExpr) {
}
@Override
public void visit(EmptyStmt emptyStmt) {
}
@Override
public void visit(FieldVar fieldVar) {
fieldVar.receiver.accept(this);
}
@Override
public void visit(ForStmt forStmt) {
}
@Override
public void visit(IfStmt ifStmt) {
}
@Override
public void visit(InstanceOf instanceOf) {
}
@Override
public void visit(LocalVar localVar) {
}
@Override
public void visit(LocalVarDecl localVarDecl) {
@ -133,6 +183,11 @@ public abstract class AbstractASTWalker implements ASTVisitor{
visit((MethodCall) methodCall);
}
@Override
public void visit(NewArray newArray) {
}
@Override
public void visit(Receiver receiver) {
receiver.expr.accept(this);
@ -142,4 +197,49 @@ public abstract class AbstractASTWalker implements ASTVisitor{
public void visit(Return aReturn) {
aReturn.retexpr.accept(this);
}
@Override
public void visit(ReturnVoid aReturn) {
}
@Override
public void visit(StaticClassName staticClassName) {
}
@Override
public void visit(Super aSuper) {
}
@Override
public void visit(This aThis) {
}
@Override
public void visit(UnaryPlus unaryPlus) {
}
@Override
public void visit(WhileStmt whileStmt) {
}
@Override
public void visit(DoStmt whileStmt) {
}
@Override
public void visit(Null aNull) {
}
@Override
public void visit(Literal literal) {
}
}

View File

@ -33,19 +33,12 @@ public class JavaClassRegistry {
if(name.equals(new JavaClassName(className)))return name;
}
//Jetzt noch alle importierten Packages durchsuchen:
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!
}
JavaClassName ret = getClassFromImportedPackages(className);
if(ret == null){
throw new TypeNotPresentException(className, new Throwable());
}else{
return ret;
}
throw new TypeNotPresentException(className, new Throwable());
}
@Override
@ -55,7 +48,33 @@ public class JavaClassRegistry {
public boolean contains(String whole) {
boolean ret = existingClasses.contains(new JavaClassName(whole));
if(ret == false)existingClasses.add(getName(whole));
return true;
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;
}
}

View File

@ -20,8 +20,9 @@ public class TypeInsert {
}
public String insert(String intoSource){
String ret = intoSource;
List<TypeInsertPoint> offsets = new ArrayList<>();
String ret = point.insert(intoSource, offsets);
offsets.add(point);
for(TypeInsertPoint insertPoint : inserts){
ret = insertPoint.insert(ret, offsets);
offsets.add(insertPoint);

View File

@ -25,36 +25,14 @@ import java.util.*;
* inferieren, wenn A bereits eingesetzt wurde. Es werden dann eben zusätzliche Generics entstehen
*/
public class TypeInsertFactory {
public static List<TypeInsert> createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults){
List<TypeInsert> ret = new ArrayList<>();
for(ClassOrInterface cl : forSourcefile.getClasses()){
//Felder:
for(Field field : cl.getFieldDecl()){
if(field.getType() instanceof TypePlaceholder){
for(Set<Pair> pairs : withResults.results)
ret.add(createInsertPoints(
field.getType(), field.getType().getOffset(), cl, null, pairs));
}
}
for(Method m : cl.getMethods()){
if(m.getReturnType() instanceof TypePlaceholder)for(Set<Pair> pairs : withResults.results) {
ret.add(createInsertPoints(
m.getReturnType(), m.getReturnType().getOffset(), cl, m, pairs));
}
for(FormalParameter param : m.getParameterList().getFormalparalist()){
if(param.getType() instanceof TypePlaceholder)for(Set<Pair> pairs : withResults.results)
ret.add(createInsertPoints(
param.getType(), param.getType().getOffset(), cl, m, pairs));
}
}
}
return ret;
public static Set<TypeInsert> createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults){
return new TypeInsertPlacer().getTypeInserts(forSourcefile, withResults);
}
private static TypeInsert createInsertPoints(RefTypeOrTPHOrWildcardOrGeneric type, Token offset, ClassOrInterface cl, Method m,
Set<Pair> pairs) {
public static TypeInsert createInsertPoints(RefTypeOrTPHOrWildcardOrGeneric type, Token offset, ClassOrInterface cl, Method m,
Set<Pair> pairs) {
Set<TypeInsertPoint> ret = new HashSet<>();
TypeInsertPoint insertPoint = null;
Set<TypePlaceholder> additionalInserts = new HashSet<>();

View File

@ -0,0 +1,99 @@
package de.dhbwstuttgart.typedeployment;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.constraints.Pair;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TypeInsertPlacer extends AbstractASTWalker{
Set<TypeInsert> inserts = new HashSet<>();
private ResultSet withResults;
public Set<TypeInsert> getTypeInserts(SourceFile forSourceFile, ResultSet withResults){
this.withResults = withResults;
forSourceFile.accept(this);
return inserts;
}
@Override
public void visit(ClassOrInterface classOrInterface) {
TypeInsertPlacerClass cl = new TypeInsertPlacerClass(classOrInterface, withResults);
this.inserts.addAll(cl.inserts);
}
/*
List<TypeInsert> ret = new ArrayList<>();
for(ClassOrInterface cl : forSourcefile.getClasses()){
//Felder:
for(Field field : cl.getFieldDecl()){
}
for(Method m : cl.getMethods()){
if(m.getReturnType() instanceof TypePlaceholder)for(Set<Pair> pairs : withResults.results) {
ret.add(createInsertPoints(
m.getReturnType(), m.getReturnType().getOffset(), cl, m, pairs));
}
for(FormalParameter param : m.getParameterList().getFormalparalist()){
if(param.getType() instanceof TypePlaceholder)for(Set<Pair> pairs : withResults.results)
ret.add(createInsertPoints(
param.getType(), param.getType().getOffset(), cl, m, pairs));
}
}
}
return ret;
*/
}
class TypeInsertPlacerClass extends AbstractASTWalker{
private final ResultSet results;
private final ClassOrInterface cl;
public final Set<TypeInsert> inserts = new HashSet<>();
TypeInsertPlacerClass(ClassOrInterface forClass, ResultSet withResults){
this.cl = forClass;
this.results = withResults;
forClass.accept(this);
}
@Override
public void visit(Method method) {
TypeInsertPlacerMethod mWalker = new TypeInsertPlacerMethod(method);
super.visit(method);
}
@Override
public void visit(Field field) {
if(field.getType() instanceof TypePlaceholder){
for(Set<Pair> pairs : results.results)
inserts.add(TypeInsertFactory.createInsertPoints(
field.getType(), field.getType().getOffset(), cl, null, pairs));
}
super.visit(field);
}
}
class TypeInsertPlacerMethod extends AbstractASTWalker{
TypeInsertPlacerMethod(Method forMethod){
forMethod.accept(this);
}
@Override
public void visit(ParameterList params) {
super.visit(params);
}
@Override
public void visit(LambdaExpression lambdaExpression) {
//Lambda-Ausdrücke brauchen keine Typeinsetzungen
}
}

View File

@ -11,7 +11,7 @@ public class TypeInsertPoint {
public TypeInsertPoint(Token point, String toInsert){
this.point = point;
this.insertString = (toInsert.length()>1) ? toInsert + " " : toInsert;
this.insertString = (toInsert.endsWith(" ")) ? toInsert : toInsert + " " ;
}
public String insert(String intoSource, List<TypeInsertPoint> additionalOffset){

View File

@ -1,5 +1,7 @@
class mathStruc<A> {
a;
mathStruc(A a) { }
A model(){ A a; return a; }

View File

@ -22,6 +22,7 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.*;
@ -44,7 +45,7 @@ public class JavaTXCompilerTest extends JavaTXCompiler {
for(File f : filesToTest){
SourceFile sf = this.parse(f);
System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1)));
List<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, this.typeInference());
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, this.typeInference());
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
for(TypeInsert tip : result){
System.out.println(tip.insert(content));