Skip to content

Commit

Permalink
make TypeParametersMap immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Oct 24, 2016
1 parent 1355de0 commit 71a7ebd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
Expand Up @@ -66,5 +66,5 @@ public void setMethod(SymbolReference<MethodDeclaration> method) {
public boolean isAssignableBy(Type other) { public boolean isAssignableBy(Type other) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

} }
Expand Up @@ -31,6 +31,7 @@
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference; import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.usages.MethodUsage; import com.github.javaparser.symbolsolver.model.usages.MethodUsage;
import com.github.javaparser.symbolsolver.model.usages.TypeParametersMap;
import com.github.javaparser.symbolsolver.model.usages.typesystem.ArrayType; import com.github.javaparser.symbolsolver.model.usages.typesystem.ArrayType;
import com.github.javaparser.symbolsolver.model.usages.typesystem.ReferenceType; import com.github.javaparser.symbolsolver.model.usages.typesystem.ReferenceType;
import com.github.javaparser.symbolsolver.model.usages.typesystem.ReferenceTypeImpl; import com.github.javaparser.symbolsolver.model.usages.typesystem.ReferenceTypeImpl;
Expand Down Expand Up @@ -301,7 +302,7 @@ public List<ReferenceType> getAncestors() {
List<ReferenceType> ancestors = new ArrayList<>(); List<ReferenceType> ancestors = new ArrayList<>();
ReferenceType enumClass = ReflectionFactory.typeUsageFor(Enum.class, typeSolver).asReferenceType(); ReferenceType enumClass = ReflectionFactory.typeUsageFor(Enum.class, typeSolver).asReferenceType();
TypeParameterDeclaration eTypeParameter = enumClass.getTypeDeclaration().getTypeParameters().get(0); TypeParameterDeclaration eTypeParameter = enumClass.getTypeDeclaration().getTypeParameters().get(0);
enumClass.typeParametersMap().setValue(eTypeParameter, new ReferenceTypeImpl(this, typeSolver)); enumClass = enumClass.deriveTypeParameters(new TypeParametersMap.Builder().setValue(eTypeParameter, new ReferenceTypeImpl(this, typeSolver)).build());
ancestors.add(enumClass); ancestors.add(enumClass);
if (wrappedNode.getImplements() != null) { if (wrappedNode.getImplements() != null) {
for (ClassOrInterfaceType implementedType : wrappedNode.getImplements()) { for (ClassOrInterfaceType implementedType : wrappedNode.getImplements()) {
Expand Down
Expand Up @@ -552,7 +552,7 @@ public void testReplaceTypeVariables() {
com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration streamMap = streamInterface.getDeclaredMethods().stream().filter(m -> m.getName().equals("map")).findFirst().get(); com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration streamMap = streamInterface.getDeclaredMethods().stream().filter(m -> m.getName().equals("map")).findFirst().get();
TypeParameterDeclaration streamMapR = streamMap.findTypeParameter("T").get(); TypeParameterDeclaration streamMapR = streamMap.findTypeParameter("T").get();
TypeVariable typeVariable = new TypeVariable(streamMapR); TypeVariable typeVariable = new TypeVariable(streamMapR);
stream.typeParametersMap().setValue(stream.typeDeclaration.getTypeParameters().get(0), typeVariable); stream = stream.deriveTypeParameters(stream.typeParametersMap().toBuilder().setValue(stream.typeDeclaration.getTypeParameters().get(0), typeVariable).build());


TypeParameterDeclaration tpToReplace = streamInterface.getTypeParameters().get(0); TypeParameterDeclaration tpToReplace = streamInterface.getTypeParameters().get(0);
Type replaced = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeResolver), typeResolver); Type replaced = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeResolver), typeResolver);
Expand All @@ -570,7 +570,7 @@ public void testReplaceTypeVariablesWithLambdaInBetween() {
com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration streamMap = streamInterface.getDeclaredMethods().stream().filter(m -> m.getName().equals("map")).findFirst().get(); com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration streamMap = streamInterface.getDeclaredMethods().stream().filter(m -> m.getName().equals("map")).findFirst().get();
TypeParameterDeclaration streamMapR = streamMap.findTypeParameter("R").get(); TypeParameterDeclaration streamMapR = streamMap.findTypeParameter("R").get();
TypeVariable typeVariable = new TypeVariable(streamMapR); TypeVariable typeVariable = new TypeVariable(streamMapR);
stream.typeParametersMap().setValue(stream.typeDeclaration.getTypeParameters().get(0), typeVariable); stream = stream.deriveTypeParameters(stream.typeParametersMap().toBuilder().setValue(stream.typeDeclaration.getTypeParameters().get(0), typeVariable).build());


TypeParameterDeclaration tpToReplace = streamInterface.getTypeParameters().get(0); TypeParameterDeclaration tpToReplace = streamInterface.getTypeParameters().get(0);
Type replaced = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeResolver), typeResolver); Type replaced = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeResolver), typeResolver);
Expand Down
Expand Up @@ -36,7 +36,7 @@ public class MethodUsage implements TypeParametrized {
private TypeParametersMap typeParametersMap; private TypeParametersMap typeParametersMap;


public MethodUsage(MethodDeclaration declaration) { public MethodUsage(MethodDeclaration declaration) {
this.typeParametersMap = new TypeParametersMap(); this.typeParametersMap = TypeParametersMap.empty();
this.declaration = declaration; this.declaration = declaration;
for (int i = 0; i < declaration.getNumberOfParams(); i++) { for (int i = 0; i < declaration.getNumberOfParams(); i++) {
paramTypes.add(declaration.getParam(i).getType()); paramTypes.add(declaration.getParam(i).getType());
Expand All @@ -45,7 +45,7 @@ public MethodUsage(MethodDeclaration declaration) {
} }


public MethodUsage(MethodDeclaration declaration, List<Type> paramTypes, Type returnType) { public MethodUsage(MethodDeclaration declaration, List<Type> paramTypes, Type returnType) {
this.typeParametersMap = new TypeParametersMap(); this.typeParametersMap = TypeParametersMap.empty();
this.declaration = declaration; this.declaration = declaration;
this.paramTypes = paramTypes; this.paramTypes = paramTypes;
this.returnType = returnType; this.returnType = returnType;
Expand Down
Expand Up @@ -30,6 +30,35 @@
* @author Federico Tomassetti * @author Federico Tomassetti
*/ */
public class TypeParametersMap { public class TypeParametersMap {

public static class Builder {
private Map<String, Type> nameToValue;
private Map<String, TypeParameterDeclaration> nameToDeclaration;

public Builder() {
nameToValue = new HashMap<>();
nameToDeclaration = new HashMap<>();
}

private Builder(Map<String, Type> nameToValue, Map<String, TypeParameterDeclaration> nameToDeclaration) {
this.nameToValue = new HashMap<>();
this.nameToValue.putAll(nameToValue);
this.nameToDeclaration = new HashMap<>();
this.nameToDeclaration.putAll(nameToDeclaration);
}

public TypeParametersMap build() {
return new TypeParametersMap(nameToValue, nameToDeclaration);
}

public Builder setValue(TypeParameterDeclaration typeParameter, Type value) {
String qualifiedName = typeParameter.getQualifiedName();
nameToValue.put(qualifiedName, value);
nameToDeclaration.put(qualifiedName, typeParameter);
return this;
}
}

@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
Expand All @@ -56,15 +85,15 @@ public String toString() {
private Map<String, Type> nameToValue; private Map<String, Type> nameToValue;
private Map<String, TypeParameterDeclaration> nameToDeclaration; private Map<String, TypeParameterDeclaration> nameToDeclaration;


public TypeParametersMap() { public static TypeParametersMap empty() {
nameToValue = new HashMap<>(); return new Builder().build();
nameToDeclaration = new HashMap<>();
} }


public void setValue(TypeParameterDeclaration typeParameter, Type value) { private TypeParametersMap(Map<String, Type> nameToValue, Map<String, TypeParameterDeclaration> nameToDeclaration) {
String qualifiedName = typeParameter.getQualifiedName(); this.nameToValue = new HashMap<>();
nameToValue.put(qualifiedName, value); this.nameToValue.putAll(nameToValue);
nameToDeclaration.put(qualifiedName, typeParameter); this.nameToDeclaration = new HashMap<>();
this.nameToDeclaration.putAll(nameToDeclaration);
} }


public Type getValue(TypeParameterDeclaration typeParameter) { public Type getValue(TypeParameterDeclaration typeParameter) {
Expand All @@ -84,6 +113,10 @@ public Optional<Type> getValueBySignature(String signature) {
} }
} }


public Builder toBuilder() {
return new Builder(nameToValue, nameToDeclaration);
}

public boolean isEmpty() { public boolean isEmpty() {
return nameToValue.isEmpty(); return nameToValue.isEmpty();
} }
Expand Down
Expand Up @@ -60,10 +60,11 @@ public ReferenceType(TypeDeclaration typeDeclaration, List<Type> typeParameters,
throw new IllegalArgumentException(String.format("expected either zero type parameters or has many as defined in the declaration (%d). Found %d", throw new IllegalArgumentException(String.format("expected either zero type parameters or has many as defined in the declaration (%d). Found %d",
typeDeclaration.getTypeParameters().size(), typeParameters.size())); typeDeclaration.getTypeParameters().size(), typeParameters.size()));
} }
this.typeParametersMap = new TypeParametersMap(); TypeParametersMap.Builder typeParametersMapBuilder = new TypeParametersMap.Builder();
for (int i = 0; i < typeParameters.size(); i++) { for (int i = 0; i < typeParameters.size(); i++) {
this.typeParametersMap.setValue(typeDeclaration.getTypeParameters().get(i), typeParameters.get(i)); typeParametersMapBuilder.setValue(typeDeclaration.getTypeParameters().get(i), typeParameters.get(i));
} }
this.typeParametersMap = typeParametersMapBuilder.build();
this.typeDeclaration = typeDeclaration; this.typeDeclaration = typeDeclaration;
this.typeSolver = typeSolver; this.typeSolver = typeSolver;
} }
Expand Down Expand Up @@ -434,4 +435,7 @@ private static List<Type> deriveParams(TypeDeclaration typeDeclaration) {
return typeDeclaration.getTypeParameters().stream().map((tp) -> new TypeVariable(tp)).collect(Collectors.toList()); return typeDeclaration.getTypeParameters().stream().map((tp) -> new TypeVariable(tp)).collect(Collectors.toList());
} }


public ReferenceType deriveTypeParameters(TypeParametersMap typeParametersMap) {
return create(typeDeclaration, typeParametersMap, typeSolver);
}
} }

0 comments on commit 71a7ebd

Please sign in to comment.