use of com.sun.source.tree.AssignmentTree in project st-js by st-js.
the class ClassWriter method getAnnotationForElement.
private JS getAnnotationForElement(AnnotationTree ann, WriterVisitor<JS> visitor, GenerationContext<JS> context) {
// XXX: should use here type names?
if (AnnotationHelper.getRetentionType(ann.getAnnotationType()) == RetentionPolicy.SOURCE) {
return null;
}
String annEntryKey = ann.getAnnotationType().toString();
if (!context.getConfiguration().getAnnotations().contains(annEntryKey)) {
return null;
}
List<NameValue<JS>> annotationArgsDesc = new ArrayList<NameValue<JS>>();
for (ExpressionTree arg : ann.getArguments()) {
AssignmentTree assign = (AssignmentTree) arg;
annotationArgsDesc.add(NameValue.of(assign.getVariable().toString(), writeAnnotationValue(visitor, assign.getExpression(), context)));
}
return context.js().object(annotationArgsDesc);
}
use of com.sun.source.tree.AssignmentTree in project st-js by st-js.
the class NewClassWriter method getObjectInitializer.
/**
* special construction for object initialization new Object(){{x = 1; y = 2; }};
*/
private JS getObjectInitializer(WriterVisitor<JS> visitor, TreeWrapper<NewClassTree, JS> tw) {
NewClassTree tree = tw.getTree();
BlockTree initBlock = getDoubleBracesBlock(tree);
if (initBlock == null) {
if (tw.child(tree.getIdentifier()).isSyntheticType()) {
// the syntethic type will generate {} constructor even without the initBlock
return tw.getContext().js().object(new ArrayList<NameValue<JS>>());
}
return null;
}
if (!tw.child(tree.getIdentifier()).isSyntheticType()) {
// this is already checked and not allowed
return null;
}
List<NameValue<JS>> props = new ArrayList<NameValue<JS>>();
for (StatementTree stmt : initBlock.getStatements()) {
// check the right type of statements x=y is done in NewClassObjectInitCheck
ExpressionTree expr = ((ExpressionStatementTree) stmt).getExpression();
if (expr instanceof AssignmentTree) {
AssignmentTree assign = (AssignmentTree) expr;
props.add(NameValue.of(getPropertyName(assign.getVariable()), visitor.scan(assign.getExpression(), tw.getContext())));
} else {
MethodInvocationTree meth = (MethodInvocationTree) expr;
String propertyName = MethodToPropertyTemplate.getPropertyName(meth);
JS value = visitor.scan(meth.getArguments().get(0), tw.getContext());
props.add(NameValue.of(propertyName, value));
}
}
return tw.getContext().js().object(props);
}
use of com.sun.source.tree.AssignmentTree in project error-prone by google.
the class AbstractSuppressWarningsMatcher method getSuggestedFix.
protected final Fix getSuggestedFix(AnnotationTree annotationTree) {
List<String> values = new ArrayList<>();
for (ExpressionTree argumentTree : annotationTree.getArguments()) {
AssignmentTree assignmentTree = (AssignmentTree) argumentTree;
if (assignmentTree.getVariable().toString().equals("value")) {
ExpressionTree expressionTree = assignmentTree.getExpression();
switch(expressionTree.getKind()) {
case STRING_LITERAL:
values.add(((String) ((JCTree.JCLiteral) expressionTree).value));
break;
case NEW_ARRAY:
NewArrayTree newArrayTree = (NewArrayTree) expressionTree;
for (ExpressionTree elementTree : newArrayTree.getInitializers()) {
values.add((String) ((JCTree.JCLiteral) elementTree).value);
}
break;
default:
throw new AssertionError("Unknown kind: " + expressionTree.getKind());
}
processSuppressWarningsValues(values);
} else {
throw new AssertionError("SuppressWarnings has an element other than value=");
}
}
if (values.isEmpty()) {
return SuggestedFix.delete(annotationTree);
} else if (values.size() == 1) {
return SuggestedFix.replace(annotationTree, "@SuppressWarnings(\"" + values.get(0) + "\")");
} else {
return SuggestedFix.replace(annotationTree, "@SuppressWarnings({\"" + Joiner.on("\", \"").join(values) + "\"})");
}
}
use of com.sun.source.tree.AssignmentTree in project error-prone by google.
the class AnnotationMatcherUtils method getArgument.
/**
* Gets the value for an argument, or null if the argument does not exist.
*
* @param annotationTree the AST node for the annotation
* @param name the name of the argument whose value to get
* @return the value of the argument, or null if the argument does not exist
*/
public static ExpressionTree getArgument(AnnotationTree annotationTree, String name) {
for (ExpressionTree argumentTree : annotationTree.getArguments()) {
if (argumentTree.getKind() != Tree.Kind.ASSIGNMENT) {
continue;
}
AssignmentTree assignmentTree = (AssignmentTree) argumentTree;
if (!assignmentTree.getVariable().toString().equals(name)) {
continue;
}
ExpressionTree expressionTree = assignmentTree.getExpression();
return expressionTree;
}
return null;
}
use of com.sun.source.tree.AssignmentTree in project error-prone by google.
the class JdkObsolete method getTargetType.
static Type getTargetType(VisitorState state) {
Tree parent = state.getPath().getParentPath().getLeaf();
Type type;
if (parent instanceof VariableTree || parent instanceof AssignmentTree) {
type = ASTHelpers.getType(parent);
} else if (parent instanceof ReturnTree || parent instanceof LambdaExpressionTree) {
type = getMethodOrLambdaReturnType(state);
} else if (parent instanceof MethodInvocationTree) {
MethodInvocationTree tree = (MethodInvocationTree) parent;
int idx = tree.getArguments().indexOf(state.getPath().getLeaf());
if (idx == -1) {
return null;
}
Type methodType = ASTHelpers.getType(tree.getMethodSelect());
if (idx >= methodType.getParameterTypes().size()) {
return null;
}
return methodType.getParameterTypes().get(idx);
} else {
return null;
}
Tree tree = state.getPath().getLeaf();
if (tree instanceof MemberReferenceTree) {
type = state.getTypes().findDescriptorType(ASTHelpers.getType(tree)).getReturnType();
}
return type;
}
Aggregations