use of com.github.javaparser.ast.expr.Expression in project checker-framework by typetools.
the class StubParser method handleExpr.
/**
* Adds an annotation element (argument) to {@code builder}. The element is a Java expression.
*
* @return true if the expression was parsed and added to {@code builder}, false otherwise
*/
private boolean handleExpr(AnnotationBuilder builder, String name, Expression expr) {
ExecutableElement var = builder.findElement(name);
TypeMirror expected = var.getReturnType();
TypeKind valueKind;
if (expected.getKind() == TypeKind.ARRAY) {
valueKind = ((ArrayType) expected).getComponentType().getKind();
} else {
valueKind = expected.getKind();
}
if (expr instanceof ArrayInitializerExpr) {
if (expected.getKind() != TypeKind.ARRAY) {
stubWarn("unhandled annotation attribute type: " + expr + " and expected: " + expected);
return false;
}
List<Expression> arrayExpressions = ((ArrayInitializerExpr) expr).getValues();
Object[] values = new Object[arrayExpressions.size()];
for (int i = 0; i < arrayExpressions.size(); ++i) {
values[i] = getValueOfExpressionInAnnotation(name, arrayExpressions.get(i), valueKind);
if (values[i] == null) {
return false;
}
}
builder.setValue(name, values);
} else {
Object value = getValueOfExpressionInAnnotation(name, expr, valueKind);
if (value == null) {
return false;
}
if (expected.getKind() == TypeKind.ARRAY) {
Object[] valueArray = { value };
builder.setValue(name, valueArray);
} else {
builderSetValue(builder, name, value);
}
}
return true;
}
use of com.github.javaparser.ast.expr.Expression in project checker-framework by typetools.
the class StubParser method getAnnotation.
/**
* Convert {@code annotation} into an AnnotationMirror. Returns null if the annotation isn't
* supported by the checker or if some error occurred while converting it.
*/
private AnnotationMirror getAnnotation(AnnotationExpr annotation, Map<String, AnnotationMirror> allStubAnnotations) {
AnnotationMirror annoMirror;
if (annotation instanceof MarkerAnnotationExpr) {
String annoName = ((MarkerAnnotationExpr) annotation).getNameAsString();
annoMirror = allStubAnnotations.get(annoName);
} else if (annotation instanceof NormalAnnotationExpr) {
NormalAnnotationExpr nrmanno = (NormalAnnotationExpr) annotation;
String annoName = nrmanno.getNameAsString();
annoMirror = allStubAnnotations.get(annoName);
if (annoMirror == null) {
// Not a supported qualifier -> ignore
return null;
}
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoMirror);
List<MemberValuePair> pairs = nrmanno.getPairs();
if (pairs != null) {
for (MemberValuePair mvp : pairs) {
String member = mvp.getNameAsString();
Expression exp = mvp.getValue();
boolean success = handleExpr(builder, member, exp);
if (!success) {
stubWarn("Annotation expression, %s, could not be processed for annotation: %s. ", exp, annotation);
return null;
}
}
}
return builder.build();
} else if (annotation instanceof SingleMemberAnnotationExpr) {
SingleMemberAnnotationExpr sglanno = (SingleMemberAnnotationExpr) annotation;
String annoName = sglanno.getNameAsString();
annoMirror = allStubAnnotations.get(annoName);
if (annoMirror == null) {
// Not a supported qualifier -> ignore
return null;
}
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoMirror);
Expression valexpr = sglanno.getMemberValue();
boolean success = handleExpr(builder, "value", valexpr);
if (!success) {
stubWarn("Annotation expression, %s, could not be processed for annotation: %s. ", valexpr, annotation);
return null;
}
return builder.build();
} else {
ErrorReporter.errorAbort("StubParser: unknown annotation type: " + annotation);
// dead code
annoMirror = null;
}
return annoMirror;
}
use of com.github.javaparser.ast.expr.Expression in project vue-gwt by Axellience.
the class TemplateParser method processJavaExpression.
/**
* Process the given string as a Java expression.
* @param expressionString A valid Java expression
* @return A processed expression, should be placed in the HTML in place of the original
* expression
*/
private TemplateExpression processJavaExpression(String expressionString) {
Expression expression;
try {
expression = JavaParser.parseExpression(expressionString);
} catch (ParseProblemException parseException) {
logger.error("Couldn't parse Expression, make sure it is valid Java.", expressionString);
throw parseException;
}
resolveTypesUsingImports(expression);
resolveStaticMethodsUsingImports(expression);
checkMethodNames(expression);
// Find the parameters used by the expression
List<VariableInfo> expressionParameters = new LinkedList<>();
findExpressionParameters(expression, expressionParameters);
// If there is a cast first, we use this as the type of our expression
if (currentProp == null)
expression = getTypeFromCast(expression);
// Update the expression as it might have been changed
expressionString = expression.toString();
// Add the resulting expression to our result
return result.addExpression(expressionString, currentExpressionReturnType, currentProp == null, expressionParameters);
}
use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class CloneVisitor method visit.
@Override
public Node visit(ArrayCreationLevel _n, Object _arg) {
List<AnnotationExpr> ann = visit(_n.getAnnotations(), _arg);
Expression dimension_ = cloneNodes(_n.getDimension(), _arg);
ArrayCreationLevel r = new ArrayCreationLevel(_n.getRange(), dimension_, ann);
Comment comment = cloneNodes(_n.getComment(), _arg);
r.setComment(comment);
return r;
}
use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.
the class ModifierVisitorAdapter method visit.
@Override
public Node visit(final IfStmt n, final A arg) {
visitComment(n, arg);
final Expression condition = (Expression) n.getCondition().accept(this, arg);
if (condition == null) {
return null;
}
n.setCondition(condition);
final Statement thenStmt = (Statement) n.getThenStmt().accept(this, arg);
if (thenStmt == null) {
// then-clause.
return null;
}
n.setThenStmt(thenStmt);
if (n.getElseStmt() != null) {
n.setElseStmt((Statement) n.getElseStmt().accept(this, arg));
}
return n;
}
Aggregations