private static boolean isSyntacticallyAMethodName(Node name) { // A name is syntactically classified as a MethodName in this context: // // 1. Before the "(" in a method invocation expression (ยง15.12) if (whenParentIs(MethodCallExpr.class, name, (p, c) -> p.getName() == c)) { return true; } return false; }
private Optional<Node> usePath(final MethodCallExpr it) { MethodCallExpr expr = AST.scopeOf(it); String name = expr.getName(); List<Expression> args = expr.getArgs(); if (name.equals("use") && args.size() == 1 && args.get(0) instanceof StringLiteralExpr) { return Optional.of(expr); } return Optional.empty(); }
private static class MyVisitor extends GenericVisitorAdapter { public Object visit(MethodCallExpr n, Object arg) { if ("setOneMoreWorkingLineWithServiceID".equals(n.getName())){ for (Expression expression : n.getArgs()) { System.out.println(expression.toString()); } } return super.visit(n, arg); } }
@Override public void visit(final MethodCallExpr n, final Context ctx) { if (args.size() > 1) { // req, rsp String var = scope(n); if (args.get(1).equals(var) && n.getName().equals("send")) { Type type = type(n.getArgs().get(0), ctx); this.type = type; } } }
private Type useMvc(final MethodCallExpr n, final Context ctx) { if ("use".equals(n.getName())) { List<Expression> args = n.getArgs(); if (args.size() == 1) { Expression arg = args.get(0); if (arg instanceof ClassExpr) { return arg.accept(new TypeCollector(), ctx); } } } return null; }
private List<MethodCallExpr> routes(final MethodCallExpr expr, final Context ctx) { LinkedList<MethodCallExpr> expressions = new LinkedList<>(); Expression it = expr; while (it instanceof MethodCallExpr) { MethodCallExpr local = (MethodCallExpr) it; String name = local.getName(); int n = 0; if (Route.METHODS.contains(name.toUpperCase())) { n = route(local, ctx); } else if (name.equals("use")) { n = route(local, ctx); } else if (name.equals("all") && AST.scopeOf(local).getName().equals("use")) { n = route(local, ctx); } while (n > 0) { expressions.addFirst(local); n -= 1; } it = local.getScope(); } return expressions; }
@Override public void visit(final MethodCallExpr node, final Context ctx) { List<MethodCallExpr> nodes = dump(node); for (MethodCallExpr n : nodes) { List<MethodCallExpr> call = call(n); if (call.size() > 0) { MethodCallExpr cparam = call.get(0); String cname = cparam.getName(); String pname = cparam.getArgs().stream() .findFirst() .map(it -> ((StringLiteralExpr) it).getValue()) .orElse(BODY); Entry<Type, Object> typeDef = type(call.get(call.size() - 1), ctx); String doc = (String) this.doc.get(pname.equals(BODY) ? "body" : pname); params.add(new RouteParamImpl(pname, typeDef.getKey(), type(typeDef.getKey(), pname, cname), typeDef.getValue(), doc)); } } }
public MethodUsage solveMethodAsUsage(MethodCallExpr call) { List<ResolvedType> params = new ArrayList<>(); if (call.getArguments() != null) { for (Expression param : call.getArguments()) { //getTypeConcrete(Node node, boolean solveLambdas) try { params.add(getType(param, false)); } catch (Exception e) { throw new RuntimeException(String.format("Error calculating the type of parameter %s of method call %s", param, call), e); } //params.add(getTypeConcrete(param, false)); } } Context context = JavaParserFactory.getContext(call, typeSolver); Optional<MethodUsage> methodUsage = context.solveMethodAsUsage(call.getName().getId(), params); if (!methodUsage.isPresent()) { throw new RuntimeException("Method '" + call.getName() + "' cannot be resolved in context " + call + " (line: " + call.getRange().map(r -> "" + r.begin.line).orElse("??") + ") " + context + ". Parameter types: " + params); } return methodUsage.get(); }
public static MethodCallExpr findMethodCall(Node node, String methodName) { if (node instanceof MethodCallExpr) { MethodCallExpr methodCallExpr = (MethodCallExpr) node; if (methodCallExpr.getName().getId().equals(methodName)) { return methodCallExpr; } } for (Node child : node.getChildNodes()) { MethodCallExpr res = findMethodCall(child, methodName); if (res != null) { return res; } } return null; }
public MethodUsage solveMethodAsUsage(MethodCallExpr call) { List<ResolvedType> params = new ArrayList<>(); if (call.getArguments() != null) { for (Expression param : call.getArguments()) { //getTypeConcrete(Node node, boolean solveLambdas) try { params.add(getType(param, false)); } catch (Exception e) { throw new RuntimeException(String.format("Error calculating the type of parameter %s of method call %s", param, call), e); } //params.add(getTypeConcrete(param, false)); } } Context context = JavaParserFactory.getContext(call, typeSolver); Optional<MethodUsage> methodUsage = context.solveMethodAsUsage(call.getName().getId(), params, typeSolver); if (!methodUsage.isPresent()) { throw new RuntimeException("Method '" + call.getName() + "' cannot be resolved in context " + call + " (line: " + call.getRange().get().begin.line + ") " + context + ". Parameter types: " + params); } return methodUsage.get(); }
public MethodUsage solveMethodAsUsage(MethodCallExpr call) { List<ResolvedType> params = new ArrayList<>(); if (call.getArguments() != null) { for (Expression param : call.getArguments()) { //getTypeConcrete(Node node, boolean solveLambdas) try { params.add(getType(param, false)); } catch (Exception e) { throw new RuntimeException(String.format("Error calculating the type of parameter %s of method call %s", param, call), e); } //params.add(getTypeConcrete(param, false)); } } Context context = JavaParserFactory.getContext(call, typeSolver); Optional<MethodUsage> methodUsage = context.solveMethodAsUsage(call.getName().getId(), params, typeSolver); if (!methodUsage.isPresent()) { throw new RuntimeException("Method '" + call.getName() + "' cannot be resolved in context " + call + " (line: " + call.getRange().get().begin.line + ") " + context + ". Parameter types: " + params); } return methodUsage.get(); }
public static MethodCallExpr findMethodCall(Node node, String methodName) { if (node instanceof MethodCallExpr) { MethodCallExpr methodCallExpr = (MethodCallExpr) node; if (methodCallExpr.getName().getId().equals(methodName)) { return methodCallExpr; } } for (Node child : node.getChildNodes()) { MethodCallExpr res = findMethodCall(child, methodName); if (res != null) { return res; } } return null; }
private Type importApp(final MethodCallExpr n, final Context ctx) { Function<Expression, Type> type = expr -> { if (expr instanceof ObjectCreationExpr) { ClassOrInterfaceType t = ((ObjectCreationExpr) expr).getType(); Optional<java.lang.reflect.Type> resolved = ctx.resolveType(n, t.toStringWithoutComments()); if (resolved.isPresent()) { Type c = resolved.get(); if (isJooby(c)) { return c; } } } return null; }; if ("use".equals(n.getName())) { List<Expression> args = n.getArgs(); if (args.size() == 2) { return type.apply(args.get(1)); } if (args.size() == 1) { return type.apply(args.get(0)); } } return null; }
/** * Resolve static method calls using static imports * * @param expression The expression to resolve */ private void resolveStaticMethodsUsingImports(Expression expression) { if (expression instanceof MethodCallExpr) { MethodCallExpr methodCall = ((MethodCallExpr) expression); String methodName = methodCall.getName().getIdentifier(); if (!methodCall.getScope().isPresent() && context.hasStaticMethod(methodName)) { methodCall.setName(context.getFullyQualifiedNameForMethodName(methodName)); } } // Recurse downward in the expression expression .getChildNodes() .stream() .filter(Expression.class::isInstance) .map(Expression.class::cast) .forEach(this::resolveStaticMethodsUsingImports); }
/** * Given a method call find out to which method declaration it corresponds. */ public SymbolReference<ResolvedMethodDeclaration> solve(MethodCallExpr methodCallExpr, boolean solveLambdas) { List<ResolvedType> argumentTypes = new LinkedList<>(); List<LambdaArgumentTypePlaceholder> placeholders = new LinkedList<>(); solveArguments(methodCallExpr, methodCallExpr.getArguments(), solveLambdas, argumentTypes, placeholders); SymbolReference<ResolvedMethodDeclaration> res = JavaParserFactory.getContext(methodCallExpr, typeSolver).solveMethod(methodCallExpr.getName().getId(), argumentTypes, false, typeSolver); for (LambdaArgumentTypePlaceholder placeholder : placeholders) { placeholder.setMethod(res); } return res; }
/** * Given a method call find out to which method declaration it corresponds. */ public SymbolReference<ResolvedMethodDeclaration> solve(MethodCallExpr methodCallExpr, boolean solveLambdas) { List<ResolvedType> argumentTypes = new LinkedList<>(); List<LambdaArgumentTypePlaceholder> placeholders = new LinkedList<>(); solveArguments(methodCallExpr, methodCallExpr.getArguments(), solveLambdas, argumentTypes, placeholders); SymbolReference<ResolvedMethodDeclaration> res = JavaParserFactory.getContext(methodCallExpr, typeSolver).solveMethod(methodCallExpr.getName().getId(), argumentTypes, false, typeSolver); for (LambdaArgumentTypePlaceholder placeholder : placeholders) { placeholder.setMethod(res); } return res; }
/** * Given a method call find out to which method declaration it corresponds. */ public SymbolReference<ResolvedMethodDeclaration> solve(MethodCallExpr methodCallExpr, boolean solveLambdas) { List<ResolvedType> argumentTypes = new LinkedList<>(); List<LambdaArgumentTypePlaceholder> placeholders = new LinkedList<>(); solveArguments(methodCallExpr, methodCallExpr.getArguments(), solveLambdas, argumentTypes, placeholders); SymbolReference<ResolvedMethodDeclaration> res = JavaParserFactory.getContext(methodCallExpr, typeSolver).solveMethod(methodCallExpr.getName().getId(), argumentTypes, false); for (LambdaArgumentTypePlaceholder placeholder : placeholders) { placeholder.setMethod(res); } return res; }
public void visit(final MethodCallExpr n, final RoutingDefineContext arg) { if (n.getName().equals("define") && n.getScope().equals(ASTHelper.createNameExpr("Routes"))) { arg.setInRoutingDefine(true); super.visit(n, arg); arg.setInRoutingDefine(false); } else { super.visit(n, arg); } }
/** * Check the expression for component method calls. This will check that the methods used in the * template exist in the Component. It throws an exception if we use a method that is not declared * in our Component. This will not check for the type or number of parameters, we leave that to * the Java Compiler. * * @param expression The expression to check */ private void checkMethodNames(Expression expression) { if (expression instanceof MethodCallExpr) { MethodCallExpr methodCall = ((MethodCallExpr) expression); if (!methodCall.getScope().isPresent()) { String methodName = methodCall.getName().getIdentifier(); if (!context.hasMethod(methodName) && !context.hasStaticMethod(methodName)) { logger.error("Couldn't find the method \"" + methodName + "\". " + "Make sure it is not private."); } } } for (com.github.javaparser.ast.Node node : expression.getChildNodes()) { if (!(node instanceof Expression)) { continue; } Expression childExpr = (Expression) node; checkMethodNames(childExpr); } }
SymbolReference<ResolvedMethodDeclaration> refMethod = facade.solve(callExpr, false); if (!refMethod.isSolved()) { throw new com.github.javaparser.resolution.UnsolvedSymbolException(requireParentNode(node).toString(), callExpr.getName().getId());