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.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;
@ -33,6 +37,63 @@ public class ReplaceTypeparamVisitor
this.tphMap = new HashMap<>();
}
@Override
public void visit (Field field) {
System.out.println("Field: " + field.getName());
field.accept(new ReplaceRefTypeVisitor());
super.visit(field);
}
@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);
}
/**
* Return the mapping of the replaced {@link RefType} and the inserted
* {@link TypePlaceholder}.
*
* @return {@link Map} of {@link TypePlaceholder} and {@link RefType}
*/
public Map<? extends TypePlaceholder, ? extends RefType> 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) {
@ -42,8 +103,7 @@ public class ReplaceTypeparamVisitor
// Iterate over all Parameter Types
for (ListIterator<RefTypeOrTPHOrWildcardOrGeneric> listIterator = refType.getParaList()
.listIterator(); listIterator
.hasNext();) {
.listIterator(); listIterator.hasNext();) {
RefTypeOrTPHOrWildcardOrGeneric next = listIterator.next();
// If Parameter type is RefType replace with TPH
@ -66,7 +126,6 @@ public class ReplaceTypeparamVisitor
System.out.println(refType);
}
// Let the parent care about all the other stuff
super.visit(refType);
}
@ -82,14 +141,5 @@ public class ReplaceTypeparamVisitor
tphMap.put(tph, t);
return tph;
}
/**
* Return the mapping of the replaced {@link RefType} and the inserted
* {@link TypePlaceholder}.
*
* @return {@link Map} of {@link TypePlaceholder} and {@link RefType}
*/
public Map<? extends TypePlaceholder, ? extends RefType> getTphMap () {
return tphMap;
}
}