Skip to content

Commit

Permalink
Fix issue 2823 solve symbol as value in the case where the scope is a…
Browse files Browse the repository at this point in the history
… constraint
  • Loading branch information
jlerbsc committed Nov 7, 2020
1 parent f12876c commit 5000030
Showing 1 changed file with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@

package com.github.javaparser.symbolsolver.javaparsermodel.contexts;

import static com.github.javaparser.symbolsolver.javaparser.Navigator.demandParentNode;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.ast.expr.ThisExpr;
Expand All @@ -32,6 +38,7 @@
import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.resolution.types.ResolvedPrimitiveType;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory;
Expand All @@ -40,12 +47,6 @@
import com.github.javaparser.symbolsolver.model.resolution.Value;
import com.github.javaparser.symbolsolver.resolution.SymbolSolver;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import static com.github.javaparser.symbolsolver.javaparser.Navigator.demandParentNode;

/**
* @author Federico Tomassetti
*/
Expand Down Expand Up @@ -92,18 +93,9 @@ public Optional<Value> solveSymbolAsValue(String name) {
return Optional.of(new Value(ResolvedPrimitiveType.INT, ARRAY_LENGTH_FIELD_NAME));
}
if (typeOfScope.isReferenceType()) {
Optional<ResolvedReferenceTypeDeclaration> optionalTypeDeclaration = typeOfScope.asReferenceType().getTypeDeclaration();
if(optionalTypeDeclaration.isPresent()) {
ResolvedReferenceTypeDeclaration typeDeclaration = optionalTypeDeclaration.get();
if (typeDeclaration.isEnum()) {
ResolvedEnumDeclaration enumDeclaration = (ResolvedEnumDeclaration) typeDeclaration;
if (enumDeclaration.hasEnumConstant(name)) {
return Optional.of(new Value(enumDeclaration.getEnumConstant(name).getType(), name));
}
}
}
Optional<ResolvedType> typeUsage = typeOfScope.asReferenceType().getFieldType(name);
return typeUsage.map(resolvedType -> new Value(resolvedType, name));
return solveSymbolAsValue(name, typeOfScope.asReferenceType());
} else if (typeOfScope.isConstraint()) {
return solveSymbolAsValue(name, typeOfScope.asConstraintType().getBound().asReferenceType());
} else {
return Optional.empty();
}
Expand All @@ -113,6 +105,24 @@ public Optional<Value> solveSymbolAsValue(String name) {
.solveSymbolAsValue(name);
}
}

/*
* Try to resolve the name parameter as a field of the reference type
*/
private Optional<Value> solveSymbolAsValue(String name, ResolvedReferenceType type) {
Optional<ResolvedReferenceTypeDeclaration> optionalTypeDeclaration = type.getTypeDeclaration();
if (optionalTypeDeclaration.isPresent()) {
ResolvedReferenceTypeDeclaration typeDeclaration = optionalTypeDeclaration.get();
if (typeDeclaration.isEnum()) {
ResolvedEnumDeclaration enumDeclaration = (ResolvedEnumDeclaration) typeDeclaration;
if (enumDeclaration.hasEnumConstant(name)) {
return Optional.of(new Value(enumDeclaration.getEnumConstant(name).getType(), name));
}
}
}
Optional<ResolvedType> typeUsage = type.getFieldType(name);
return typeUsage.map(resolvedType -> new Value(resolvedType, name));
}

public SymbolReference<ResolvedValueDeclaration> solveField(String name) {
Collection<ResolvedReferenceTypeDeclaration> rrtds = findTypeDeclarations(Optional.of(wrappedNode.getScope()));
Expand Down

0 comments on commit 5000030

Please sign in to comment.