use of org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation in project camel by apache.
the class CamelJavaParserHelper method parseExpression.
private static void parseExpression(String node, JavaClassSource clazz, Block block, Expression exp, List<ParserResult> expressions) {
if (exp == null) {
return;
}
if (exp instanceof MethodInvocation) {
MethodInvocation mi = (MethodInvocation) exp;
doParseCamelSimple(node, clazz, block, mi, expressions);
// if the method was called on another method, then recursive
exp = mi.getExpression();
parseExpression(node, clazz, block, exp, expressions);
}
}
use of org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation in project camel by apache.
the class CamelJavaParserHelper method getLiteralValue.
public static String getLiteralValue(JavaClassSource clazz, Block block, Expression expression) {
// unwrap parenthesis
if (expression instanceof ParenthesizedExpression) {
expression = ((ParenthesizedExpression) expression).getExpression();
}
if (expression instanceof StringLiteral) {
return ((StringLiteral) expression).getLiteralValue();
} else if (expression instanceof BooleanLiteral) {
return "" + ((BooleanLiteral) expression).booleanValue();
} else if (expression instanceof NumberLiteral) {
return ((NumberLiteral) expression).getToken();
}
// if it a method invocation then add a dummy value assuming the method invocation will return a valid response
if (expression instanceof MethodInvocation) {
String name = ((MethodInvocation) expression).getName().getIdentifier();
return "{{" + name + "}}";
}
// source code we have access to
if (expression instanceof QualifiedName) {
QualifiedName qn = (QualifiedName) expression;
String name = qn.getFullyQualifiedName();
return "{{" + name + "}}";
}
if (expression instanceof SimpleName) {
FieldSource<JavaClassSource> field = getField(clazz, block, (SimpleName) expression);
if (field != null) {
// is the field annotated with a Camel endpoint
if (field.getAnnotations() != null) {
for (Annotation ann : field.getAnnotations()) {
boolean valid = "org.apache.camel.EndpointInject".equals(ann.getQualifiedName()) || "org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
if (valid) {
Expression exp = (Expression) ann.getInternal();
if (exp instanceof SingleMemberAnnotation) {
exp = ((SingleMemberAnnotation) exp).getValue();
} else if (exp instanceof NormalAnnotation) {
List values = ((NormalAnnotation) exp).values();
for (Object value : values) {
MemberValuePair pair = (MemberValuePair) value;
if ("uri".equals(pair.getName().toString())) {
exp = pair.getValue();
break;
}
}
}
if (exp != null) {
return getLiteralValue(clazz, block, exp);
}
}
}
}
// is the field an org.apache.camel.Endpoint type?
if ("Endpoint".equals(field.getType().getSimpleName())) {
// then grab the uri from the first argument
VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
expression = vdf.getInitializer();
if (expression instanceof MethodInvocation) {
MethodInvocation mi = (MethodInvocation) expression;
List args = mi.arguments();
if (args != null && args.size() > 0) {
// the first argument has the endpoint uri
expression = (Expression) args.get(0);
return getLiteralValue(clazz, block, expression);
}
}
} else {
// no annotations so try its initializer
VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
expression = vdf.getInitializer();
if (expression == null) {
// its a field which has no initializer, then add a dummy value assuming the field will be initialized at runtime
return "{{" + field.getName() + "}}";
} else {
return getLiteralValue(clazz, block, expression);
}
}
} else {
// we could not find the field in this class/method, so its maybe from some other super class, so insert a dummy value
final String fieldName = ((SimpleName) expression).getIdentifier();
return "{{" + fieldName + "}}";
}
} else if (expression instanceof InfixExpression) {
String answer = null;
// is it a string that is concat together?
InfixExpression ie = (InfixExpression) expression;
if (InfixExpression.Operator.PLUS.equals(ie.getOperator())) {
String val1 = getLiteralValue(clazz, block, ie.getLeftOperand());
String val2 = getLiteralValue(clazz, block, ie.getRightOperand());
// if numeric then we plus the values, otherwise we string concat
boolean numeric = isNumericOperator(clazz, block, ie.getLeftOperand()) && isNumericOperator(clazz, block, ie.getRightOperand());
if (numeric) {
Long num1 = val1 != null ? Long.valueOf(val1) : 0;
Long num2 = val2 != null ? Long.valueOf(val2) : 0;
answer = "" + (num1 + num2);
} else {
answer = (val1 != null ? val1 : "") + (val2 != null ? val2 : "");
}
if (!answer.isEmpty()) {
// include extended when we concat on 2 or more lines
List extended = ie.extendedOperands();
if (extended != null) {
for (Object ext : extended) {
String val3 = getLiteralValue(clazz, block, (Expression) ext);
if (numeric) {
Long num3 = val3 != null ? Long.valueOf(val3) : 0;
Long num = Long.valueOf(answer);
answer = "" + (num + num3);
} else {
answer += val3 != null ? val3 : "";
}
}
}
}
}
return answer;
}
return null;
}
use of org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation in project camel by apache.
the class CamelJavaParserHelper method findConfigureMethodInCreateRouteBuilder.
private static MethodSource<JavaClassSource> findConfigureMethodInCreateRouteBuilder(JavaClassSource clazz, MethodSource<JavaClassSource> method) {
// find configure inside the code
MethodDeclaration md = (MethodDeclaration) method.getInternal();
Block block = md.getBody();
if (block != null) {
List statements = block.statements();
for (int i = 0; i < statements.size(); i++) {
Statement stmt = (Statement) statements.get(i);
Expression exp = null;
if (stmt instanceof ReturnStatement) {
ReturnStatement rs = (ReturnStatement) stmt;
exp = rs.getExpression();
} else if (stmt instanceof ExpressionStatement) {
ExpressionStatement es = (ExpressionStatement) stmt;
exp = es.getExpression();
if (exp instanceof MethodInvocation) {
MethodInvocation mi = (MethodInvocation) exp;
for (Object arg : mi.arguments()) {
if (arg instanceof ClassInstanceCreation) {
exp = (Expression) arg;
break;
}
}
}
}
if (exp != null && exp instanceof ClassInstanceCreation) {
ClassInstanceCreation cic = (ClassInstanceCreation) exp;
boolean isRouteBuilder = false;
if (cic.getType() instanceof SimpleType) {
SimpleType st = (SimpleType) cic.getType();
isRouteBuilder = "RouteBuilder".equals(st.getName().toString());
}
if (isRouteBuilder && cic.getAnonymousClassDeclaration() != null) {
List body = cic.getAnonymousClassDeclaration().bodyDeclarations();
for (int j = 0; j < body.size(); j++) {
Object line = body.get(j);
if (line instanceof MethodDeclaration) {
MethodDeclaration amd = (MethodDeclaration) line;
if ("configure".equals(amd.getName().toString())) {
return new AnonymousMethodSource(clazz, amd);
}
}
}
}
}
}
}
return null;
}
use of org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation in project camel by apache.
the class CamelJavaParserHelper method doParseCamelSimple.
private static void doParseCamelSimple(String node, JavaClassSource clazz, Block block, MethodInvocation mi, List<ParserResult> expressions) {
String name = mi.getName().getIdentifier();
if ("simple".equals(name)) {
List args = mi.arguments();
// the first argument is a string parameter for the simple expression
if (args != null && args.size() >= 1) {
// it is a String type
Object arg = args.get(0);
String simple = getLiteralValue(clazz, block, (Expression) arg);
if (!Strings.isBlank(simple)) {
// is this a simple expression that is called as a predicate or expression
boolean predicate = false;
Expression parent = mi.getExpression();
if (parent == null) {
// maybe its an argument
// simple maybe be passed in as an argument
List list = mi.arguments();
// must be a single argument
if (list != null && list.size() == 1) {
ASTNode o = (ASTNode) list.get(0);
ASTNode p = o.getParent();
if (p instanceof MethodInvocation) {
// this is simple
String pName = ((MethodInvocation) p).getName().getIdentifier();
if ("simple".equals(pName)) {
// okay find the parent of simple which is the method that uses simple
parent = (Expression) p.getParent();
}
}
}
}
if (parent != null && parent instanceof MethodInvocation) {
MethodInvocation emi = (MethodInvocation) parent;
String parentName = emi.getName().getIdentifier();
predicate = isSimplePredicate(parentName);
}
int position = ((Expression) arg).getStartPosition();
ParserResult result = new ParserResult(node, position, simple);
result.setPredicate(predicate);
expressions.add(result);
}
}
}
// simple maybe be passed in as an argument
List args = mi.arguments();
if (args != null) {
for (Object arg : args) {
if (arg instanceof MethodInvocation) {
MethodInvocation ami = (MethodInvocation) arg;
doParseCamelSimple(node, clazz, block, ami, expressions);
}
}
}
}
use of org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation in project camel by apache.
the class CamelJavaParserHelper method parseExpression.
private static void parseExpression(JavaClassSource clazz, Block block, Expression exp, List<ParserResult> uris, boolean consumers, boolean producers, boolean strings, boolean fields) {
if (exp == null) {
return;
}
if (exp instanceof MethodInvocation) {
MethodInvocation mi = (MethodInvocation) exp;
doParseCamelUris(clazz, block, mi, uris, consumers, producers, strings, fields);
// if the method was called on another method, then recursive
exp = mi.getExpression();
parseExpression(clazz, block, exp, uris, consumers, producers, strings, fields);
}
}
Aggregations