/** * Resolve all the types in the expression. This will replace the Class with the full qualified * name using the template imports. * * @param expression A Java expression from the Template */ private void resolveTypesUsingImports(Expression expression) { if (expression instanceof NodeWithType) { NodeWithType<?, ?> nodeWithType = ((NodeWithType<?, ?>) expression); nodeWithType.setType(getQualifiedName(nodeWithType.getType())); } // Recurse downward in the expression expression .getChildNodes() .stream() .filter(Expression.class::isInstance) .map(Expression.class::cast) .forEach(this::resolveTypesUsingImports); }
/** * Find all the parameters this expression depends on. This is either the local variables (from a * v-for loop) or the $event variable. * * @param expression An expression from the Template * @param parameters The parameters this expression depends on */ private void findExpressionParameters(Expression expression, List<VariableInfo> parameters) { if (expression instanceof NameExpr) { NameExpr nameExpr = ((NameExpr) expression); if ("$event".equals(nameExpr.getNameAsString())) { processEventParameter(expression, nameExpr, parameters); } else { processNameExpression(nameExpr, parameters); } } expression .getChildNodes() .stream() .filter(Expression.class::isInstance) .map(Expression.class::cast) .forEach(exp -> findExpressionParameters(exp, parameters)); }
/** * 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); }
/** * 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); } }