From 5cf41101bfb42e09ecb809eb7eeb290389c61de9 Mon Sep 17 00:00:00 2001 From: Till Schnell Date: Wed, 14 Apr 2021 10:18:56 +0200 Subject: [PATCH] Only replace refType occurs in MethodReturn, Parameter, Field and Local Var --- .../ReplaceTypeparamVisitor.java | 136 ++++++++++++------ 1 file changed, 93 insertions(+), 43 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java b/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java index 10d64406..ef308f09 100644 --- a/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java +++ b/src/main/java/de/dhbwstuttgart/inferWildcards/ReplaceTypeparamVisitor.java @@ -6,6 +6,10 @@ import java.util.Map; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; +import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.FormalParameter; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -34,53 +38,41 @@ public class ReplaceTypeparamVisitor } @Override - public void visit (RefType refType) { + public void visit (Field field) { + System.out.println("Field: " + field.getName()); - // check if RefType has Parameter Types - if (!refType.getParaList().isEmpty()) { - System.out.print("Visit Type: " + refType + " -> "); + field.accept(new ReplaceRefTypeVisitor()); - // Iterate over all Parameter Types - for (ListIterator listIterator = refType.getParaList() - .listIterator(); listIterator - .hasNext();) { - RefTypeOrTPHOrWildcardOrGeneric next = listIterator.next(); - - // If Parameter type is RefType replace with TPH - if (next instanceof RefType) { - RefType nextRefType = (RefType) next; - - // Visit replaced RefType to get all nested type parameters - // Should be done before generating parents TPH to include child TPH in the - // parent TPH mapping - this.visit(nextRefType); - - // Generate TPH - TypePlaceholder tph = generateTypePlaceholder(nextRefType); - - // Replace in AST - listIterator.set(tph); - } - } - - System.out.println(refType); - } - - - // Let the parent care about all the other stuff - super.visit(refType); + super.visit(field); } - /** - * Generate the TPH for a {@link RefType} and saves the mapping. - * - * @param t {@link RefType} - * @return {@link TypePlaceholder} generated - */ - private TypePlaceholder generateTypePlaceholder (RefType t) { - TypePlaceholder tph = TypePlaceholder.fresh(new NullToken()); - tphMap.put(tph, t); - return tph; + @Override + public void visit (FormalParameter formalParameter) { + System.out.println("FormalParameter: " + formalParameter.getName()); + + formalParameter.accept(new ReplaceRefTypeVisitor()); + + super.visit(formalParameter); + } + + @Override + public void visit (LocalVarDecl localVarDecl) { + System.out.println("LocalVarDecl: " + localVarDecl.getName()); + + localVarDecl.accept(new ReplaceRefTypeVisitor()); + + super.visit(localVarDecl); + } + + @Override + public void visit (Method method) { + System.out.println("Method: " + method.getName()); + + RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType(); + if (returnType != null) + returnType.accept(new ReplaceRefTypeVisitor()); + + super.visit(method); } /** @@ -92,4 +84,62 @@ public class ReplaceTypeparamVisitor public Map getTphMap () { return tphMap; } + + /** + * Visitor replace each RefType occurs. + * + * @author Till Schnell + * @version 1.0 + */ + private class ReplaceRefTypeVisitor + extends AbstractASTWalker + { + @Override + public void visit (RefType refType) { + + // check if RefType has Parameter Types + if (!refType.getParaList().isEmpty()) { + System.out.print("Visit Type: " + refType + " -> "); + + // Iterate over all Parameter Types + for (ListIterator listIterator = refType.getParaList() + .listIterator(); listIterator.hasNext();) { + RefTypeOrTPHOrWildcardOrGeneric next = listIterator.next(); + + // If Parameter type is RefType replace with TPH + if (next instanceof RefType) { + RefType nextRefType = (RefType) next; + + // Visit replaced RefType to get all nested type parameters + // Should be done before generating parents TPH to include child TPH in the + // parent TPH mapping + this.visit(nextRefType); + + // Generate TPH + TypePlaceholder tph = generateTypePlaceholder(nextRefType); + + // Replace in AST + listIterator.set(tph); + } + } + + System.out.println(refType); + } + + // Let the parent care about all the other stuff + super.visit(refType); + } + + /** + * Generate the TPH for a {@link RefType} and saves the mapping. + * + * @param t {@link RefType} + * @return {@link TypePlaceholder} generated + */ + private TypePlaceholder generateTypePlaceholder (RefType t) { + TypePlaceholder tph = TypePlaceholder.fresh(new NullToken()); + tphMap.put(tph, t); + return tph; + } + } }