Only replace refType occurs in MethodReturn, Parameter, Field and Local

Var
This commit is contained in:
Till Schnell 2021-04-14 10:18:56 +02:00
parent e139f8c867
commit 5cf41101bf

View File

@ -6,6 +6,10 @@ import java.util.Map;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; 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.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
@ -34,53 +38,41 @@ public class ReplaceTypeparamVisitor
} }
@Override @Override
public void visit (RefType refType) { public void visit (Field field) {
System.out.println("Field: " + field.getName());
// check if RefType has Parameter Types field.accept(new ReplaceRefTypeVisitor());
if (!refType.getParaList().isEmpty()) {
System.out.print("Visit Type: " + refType + " -> ");
// Iterate over all Parameter Types super.visit(field);
for (ListIterator<RefTypeOrTPHOrWildcardOrGeneric> 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);
} }
/** @Override
* Generate the TPH for a {@link RefType} and saves the mapping. public void visit (FormalParameter formalParameter) {
* System.out.println("FormalParameter: " + formalParameter.getName());
* @param t {@link RefType}
* @return {@link TypePlaceholder} generated formalParameter.accept(new ReplaceRefTypeVisitor());
*/
private TypePlaceholder generateTypePlaceholder (RefType t) { super.visit(formalParameter);
TypePlaceholder tph = TypePlaceholder.fresh(new NullToken()); }
tphMap.put(tph, t);
return tph; @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<? extends TypePlaceholder, ? extends RefType> getTphMap () { public Map<? extends TypePlaceholder, ? extends RefType> getTphMap () {
return tphMap; 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<RefTypeOrTPHOrWildcardOrGeneric> 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;
}
}
} }