use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class MethodCallExprContext method solveMethodAsUsage.
@Override
public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver) {
if (wrappedNode.getScope().isPresent()) {
Expression scope = wrappedNode.getScope().get();
// Consider static method calls
if (scope instanceof NameExpr) {
String className = ((NameExpr) scope).getName().getId();
SymbolReference<ResolvedTypeDeclaration> ref = solveType(className, typeSolver);
if (ref.isSolved()) {
SymbolReference<ResolvedMethodDeclaration> m = MethodResolutionLogic.solveMethodInType(ref.getCorrespondingDeclaration(), name, argumentsTypes, typeSolver);
if (m.isSolved()) {
MethodUsage methodUsage = new MethodUsage(m.getCorrespondingDeclaration());
methodUsage = resolveMethodTypeParametersFromExplicitList(typeSolver, methodUsage);
methodUsage = resolveMethodTypeParameters(methodUsage, argumentsTypes);
return Optional.of(methodUsage);
} else {
throw new UnsolvedSymbolException(ref.getCorrespondingDeclaration().toString(), "Method '" + name + "' with parameterTypes " + argumentsTypes);
}
}
}
ResolvedType typeOfScope = JavaParserFacade.get(typeSolver).getType(scope);
// we can replace the parameter types from the scope into the typeParametersValues
Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>();
for (int i = 0; i < argumentsTypes.size(); i++) {
// by replacing types I can also find new equivalences
// for example if I replace T=U with String because I know that T=String I can derive that also U equal String
ResolvedType originalArgumentType = argumentsTypes.get(i);
ResolvedType updatedArgumentType = usingParameterTypesFromScope(typeOfScope, originalArgumentType, inferredTypes);
argumentsTypes.set(i, updatedArgumentType);
}
for (int i = 0; i < argumentsTypes.size(); i++) {
ResolvedType updatedArgumentType = applyInferredTypes(argumentsTypes.get(i), inferredTypes);
argumentsTypes.set(i, updatedArgumentType);
}
return solveMethodAsUsage(typeOfScope, name, argumentsTypes, typeSolver, this);
} else {
Context parentContext = getParent();
while (parentContext instanceof MethodCallExprContext) {
parentContext = parentContext.getParent();
}
return parentContext.solveMethodAsUsage(name, argumentsTypes, typeSolver);
}
}
use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class JsonPrinterTest method testWithoutType.
@Test
public void testWithoutType() {
JsonPrinter jsonPrinter = new JsonPrinter(false);
Expression expression = parseExpression("1+1");
String output = jsonPrinter.output(expression);
assertEquals("{\"operator\":\"PLUS\",\"left\":{\"value\":\"1\"},\"right\":{\"value\":\"1\"}}", output);
}
use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class BlockStmtTest method issue748AddingIdenticalStatementsDoesParentingRight.
@Test
public void issue748AddingIdenticalStatementsDoesParentingRight() {
BlockStmt blockStmt = new BlockStmt();
Expression exp = new NameExpr("x");
MethodCallExpr expression = new MethodCallExpr(exp, "y");
blockStmt.addStatement(expression);
blockStmt.addStatement(expression.clone());
// This fails when the issue exists:
String s = blockStmt.toString();
}
use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class ModifierVisitorTest method binaryExprReturnsRightExpressionWhenLeftSideIsRemoved.
@Test
public void binaryExprReturnsRightExpressionWhenLeftSideIsRemoved() {
final Expression expression = parseExpression("1+2");
final Visitable result = expression.accept(new ModifierVisitor<Void>() {
public Visitable visit(IntegerLiteralExpr integerLiteralExpr, Void arg) {
if (integerLiteralExpr.getValue().equals("2")) {
return null;
}
return integerLiteralExpr;
}
}, null);
assertEquals("1", result.toString());
}
use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class AnnotationMemberDeclarationTest method removeDefaultValueWhenDefaultValueIsPresent.
@Test
public void removeDefaultValueWhenDefaultValueIsPresent() {
AnnotationMemberDeclaration decl = new AnnotationMemberDeclaration();
SimpleName name = new SimpleName("foo");
decl.setName(name);
Expression defaultValue = new IntegerLiteralExpr("2");
decl.setDefaultValue(defaultValue);
decl.removeDefaultValue();
assertFalse(defaultValue.getParentNode().isPresent());
}
Aggregations