本文整理汇总了Java中com.sun.source.tree.AssignmentTree.getExpression方法的典型用法代码示例。如果您正苦于以下问题:Java AssignmentTree.getExpression方法的具体用法?Java AssignmentTree.getExpression怎么用?Java AssignmentTree.getExpression使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.tree.AssignmentTree
的用法示例。
在下文中一共展示了AssignmentTree.getExpression方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitAssignment
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
@Override
public Void visitAssignment(AssignmentTree tree, EnumSet<UseTypes> d) {
handlePossibleIdentifier(new TreePath(getCurrentPath(), tree.getVariable()), EnumSet.of(UseTypes.WRITE));
Tree expr = tree.getExpression();
if (expr instanceof IdentifierTree) {
TreePath tp = new TreePath(getCurrentPath(), expr);
handlePossibleIdentifier(tp, EnumSet.of(UseTypes.READ));
}
scan(tree.getVariable(), EnumSet.of(UseTypes.WRITE));
scan(tree.getExpression(), EnumSet.of(UseTypes.READ));
return null;
}
示例2: findValue
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
public static ExpressionTree findValue(AnnotationTree m, String name) {
for (ExpressionTree et : m.getArguments()) {
if (et.getKind() == Kind.ASSIGNMENT) {
AssignmentTree at = (AssignmentTree) et;
String varName = ((IdentifierTree) at.getVariable()).getName().toString();
if (varName.equals(name)) {
return at.getExpression();
}
}
if (et instanceof LiteralTree/*XXX*/ && "value".equals(name)) {
return et;
}
}
return null;
}
示例3: TreeBackedAnnotationValue
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
TreeBackedAnnotationValue(
AnnotationValue underlyingAnnotationValue,
TreePath treePath,
PostEnterCanonicalizer canonicalizer) {
this.underlyingAnnotationValue = underlyingAnnotationValue;
Tree tree = treePath.getLeaf();
if (tree instanceof AssignmentTree) {
AssignmentTree assignmentTree = (AssignmentTree) tree;
valueTree = assignmentTree.getExpression();
this.treePath = new TreePath(treePath, valueTree);
} else {
valueTree = tree;
this.treePath = treePath;
}
this.canonicalizer = canonicalizer;
}
示例4: getAnnotationTreeAttributeValue
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
static String getAnnotationTreeAttributeValue( AnnotationTree annotation,
String attributeName )
{
AssignmentTree tree =
getAnnotationTreeAttribute(annotation, attributeName);
if (tree == null) {
return null;
}
else {
ExpressionTree expression = tree.getExpression();
if (expression instanceof LiteralTree) {
Object value = ((LiteralTree) expression).getValue();
return value == null ? null : value.toString();
}
return null;
}
}
示例5: computeAssignment
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
private static List<? extends TypeMirror> computeAssignment(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
AssignmentTree at = (AssignmentTree) parent.getLeaf();
TypeMirror type = null;
if (at.getVariable() == error) {
type = info.getTrees().getTypeMirror(new TreePath(parent, at.getExpression()));
if (type != null) {
//anonymous class?
type = JavaPluginUtils.convertIfAnonymous(type);
if (type.getKind() == TypeKind.EXECUTABLE) {
//TODO: does not actualy work, attempt to solve situations like:
//t = Collections.emptyList()
//t = Collections.<String>emptyList();
//see also testCreateFieldMethod1 and testCreateFieldMethod2 tests:
type = ((ExecutableType) type).getReturnType();
}
}
}
if (at.getExpression() == error) {
type = info.getTrees().getTypeMirror(new TreePath(parent, at.getVariable()));
}
//class or field:
if (type == null) {
return null;
}
types.add(ElementKind.PARAMETER);
types.add(ElementKind.LOCAL_VARIABLE);
types.add(ElementKind.FIELD);
return Collections.singletonList(type);
}
示例6: matchAssignment
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
@Override
public Description matchAssignment(AssignmentTree tree, VisitorState state) {
if (!matchWithinClass) {
return Description.NO_MATCH;
}
Type lhsType = ASTHelpers.getType(tree.getVariable());
if (lhsType != null && lhsType.isPrimitive()) {
return doUnboxingCheck(state, tree.getExpression());
}
Symbol assigned = ASTHelpers.getSymbol(tree.getVariable());
if (assigned == null || assigned.getKind() != ElementKind.FIELD) {
// not a field of nullable type
return Description.NO_MATCH;
}
if (Nullness.hasNullableAnnotation(assigned)) {
// field already annotated
return Description.NO_MATCH;
}
ExpressionTree expression = tree.getExpression();
if (mayBeNullExpr(state, expression)) {
String message = "assigning @Nullable expression to @NonNull field";
return createErrorDescriptionForNullAssignment(
MessageTypes.ASSIGN_FIELD_NULLABLE, tree, message, expression, state.getPath());
}
return Description.NO_MATCH;
}
示例7: getArgument
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
/**
* 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;
}
示例8: getSuggestedFix
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
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) + "\"})");
}
}
示例9: matches
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
@Override
public boolean matches(AnnotationTree annotationTree, VisitorState state) {
for (ExpressionTree argumentTree : annotationTree.getArguments()) {
if (argumentTree.getKind() == Tree.Kind.ASSIGNMENT) {
AssignmentTree assignmentTree = (AssignmentTree) argumentTree;
if (assignmentTree.getVariable().toString().equals(element)) {
ExpressionTree expressionTree = assignmentTree.getExpression();
while (expressionTree instanceof ParenthesizedTree) {
expressionTree = ((ParenthesizedTree) expressionTree).getExpression();
}
if (expressionTree instanceof NewArrayTree) {
NewArrayTree arrayTree = (NewArrayTree) expressionTree;
for (ExpressionTree elementTree : arrayTree.getInitializers()) {
if (valueMatcher.matches(elementTree, state)) {
return true;
}
}
return false;
}
return valueMatcher.matches(expressionTree, state);
}
}
}
return false;
}
示例10: getSuggestedFix
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
protected final Fix getSuggestedFix(AnnotationTree annotationTree) {
List<String> values = new ArrayList<String>();
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.size() == 0) {
return new SuggestedFix().delete(annotationTree);
} else if (values.size() == 1) {
return new SuggestedFix()
.replace(annotationTree, "@SuppressWarnings(\"" + values.get(0) + "\")");
} else {
StringBuilder sb = new StringBuilder("@SuppressWarnings({\"" + values.get(0) + "\"");
for (int i = 1; i < values.size(); i++) {
sb.append(", ");
sb.append("\"" + values.get(i) + "\"");
}
sb.append("})");
return new SuggestedFix().replace(annotationTree, sb.toString());
}
}
示例11: addServletWidgetsetWebInit
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
protected void addServletWidgetsetWebInit( TypeElement type,
WorkingCopy copy, AnnotationMirror annotation )
{
TreeMaker treeMaker = copy.getTreeMaker();
Tree tree = copy.getTrees().getTree(type, annotation);
Tree oldTree = null;
Tree newTree = null;
if (tree instanceof AnnotationTree) {
AnnotationTree annotationTree = (AnnotationTree) tree;
ExpressionTree expressionTree =
getAnnotationTreeAttribute(annotationTree,
JavaUtils.INIT_PARAMS);
if (expressionTree instanceof AssignmentTree) {
AssignmentTree assignmentTree =
(AssignmentTree) expressionTree;
ExpressionTree expression = assignmentTree.getExpression();
if (expression instanceof AnnotationTree) {
oldTree = expression;
List<ExpressionTree> initializers = new ArrayList<>(2);
initializers.add(expression);
initializers.add(createWidgetsetWebInit(treeMaker));
newTree =
treeMaker.NewArray(null, Collections
.<ExpressionTree> emptyList(),
initializers);
}
else if (expression instanceof NewArrayTree) {
NewArrayTree arrayTree = (NewArrayTree) expression;
oldTree = arrayTree;
newTree =
treeMaker.addNewArrayInitializer(arrayTree,
createWidgetsetWebInit(treeMaker));
}
}
}
if (oldTree != null && newTree != null) {
copy.rewrite(oldTree, newTree);
}
}
示例12: isAlreadyRegistered
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
private static boolean isAlreadyRegistered(TreePath treePath, String key) {
ModifiersTree modifiers;
Tree tree = treePath.getLeaf();
switch (tree.getKind()) {
case METHOD:
modifiers = ((MethodTree) tree).getModifiers();
break;
case VARIABLE:
modifiers = ((VariableTree) tree).getModifiers();
break;
case CLASS:
case ENUM:
case INTERFACE:
case ANNOTATION_TYPE:
modifiers = ((ClassTree) tree).getModifiers();
break;
default:
modifiers = null;
}
if (modifiers != null) {
for (AnnotationTree ann : modifiers.getAnnotations()) {
Tree annotationType = ann.getAnnotationType();
if (annotationType.toString().matches("((org[.]openide[.]util[.])?NbBundle[.])?Messages")) { // XXX see above
List<? extends ExpressionTree> args = ann.getArguments();
if (args.size() != 1) {
continue; // ?
}
AssignmentTree assign = (AssignmentTree) args.get(0);
if (!assign.getVariable().toString().equals("value")) {
continue; // ?
}
ExpressionTree arg = assign.getExpression();
if (arg.getKind() == Tree.Kind.STRING_LITERAL) {
if (isRegistered(key, arg)) {
return true;
}
} else if (arg.getKind() == Tree.Kind.NEW_ARRAY) {
for (ExpressionTree elt : ((NewArrayTree) arg).getInitializers()) {
if (isRegistered(key, elt)) {
return true;
}
}
} else {
// ?
}
}
}
}
TreePath parentPath = treePath.getParentPath();
if (parentPath == null) {
return false;
}
// XXX better to check all sources in the same package
return isAlreadyRegistered(parentPath, key);
}
示例13: computeAssignment
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
private static List<? extends TypeMirror> computeAssignment(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
AssignmentTree at = (AssignmentTree) parent.getLeaf();
TypeMirror type = null;
types.add(ElementKind.PARAMETER);
types.add(ElementKind.LOCAL_VARIABLE);
types.add(ElementKind.FIELD);
if (at.getVariable() == error) {
type = info.getTrees().getTypeMirror(new TreePath(parent, at.getExpression()));
if (type != null) {
//anonymous class?
type = org.netbeans.modules.java.hints.errors.Utilities.convertIfAnonymous(type);
if (type.getKind() == TypeKind.EXECUTABLE) {
//TODO: does not actualy work, attempt to solve situations like:
//t = Collections.emptyList()
//t = Collections.<String>emptyList();
//see also testCreateFieldMethod1 and testCreateFieldMethod2 tests:
type = ((ExecutableType) type).getReturnType();
}
}
if (parent.getParentPath() != null && parent.getParentPath().getLeaf().getKind() == Kind.TRY) {
types.clear();
types.add(ElementKind.RESOURCE_VARIABLE);
}
}
if (at.getExpression() == error) {
type = info.getTrees().getTypeMirror(new TreePath(parent, at.getVariable()));
}
//class or field:
if (type == null) {
if (ErrorHintsProvider.ERR.isLoggable(ErrorManager.INFORMATIONAL)) {
ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "offset=" + offset);
ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "errorTree=" + error);
ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "type=null");
}
return null;
}
return Collections.singletonList(type);
}
示例14: matchAssignment
import com.sun.source.tree.AssignmentTree; //导入方法依赖的package包/类
@Override
public Description matchAssignment(AssignmentTree tree, VisitorState state) {
Symbol assigned = ASTHelpers.getSymbol(tree.getVariable());
if (assigned == null
|| assigned.getKind() != ElementKind.FIELD
|| assigned.type.isPrimitive()) {
return Description.NO_MATCH; // not a field of nullable type
}
// Best-effort try to avoid running the dataflow analysis
// TODO(kmb): bail on more non-null expressions, such as "this", arithmethic, logical, and &&/||
ExpressionTree expression = tree.getExpression();
if (ASTHelpers.constValue(expression) != null) {
// This should include literals such as "true" or a string
return Description.NO_MATCH;
}
if (TrustingNullnessAnalysis.hasNullableAnnotation(assigned)) {
return Description.NO_MATCH; // field already annotated
}
VariableTree fieldDecl = findDeclaration(state, assigned);
if (fieldDecl == null) {
return Description.NO_MATCH; // skip fields declared elsewhere for simplicity
}
// Don't need dataflow to tell us that null is nullable
if (expression.getKind() == Tree.Kind.NULL_LITERAL) {
return makeFix(state, fieldDecl, tree, "Assigning null literal to field");
}
// OK let's see what dataflow says
Nullness nullness =
TrustingNullnessAnalysis.instance(state.context)
.getNullness(new TreePath(state.getPath(), expression), state.context);
if (nullness == null) {
// This can currently happen if the assignment is inside a finally block after a return.
// TODO(b/69154806): Make dataflow work for that case.
return Description.NO_MATCH;
}
switch (nullness) {
case BOTTOM:
case NONNULL:
return Description.NO_MATCH;
case NULL:
return makeFix(state, fieldDecl, tree, "Assigning null to field");
case NULLABLE:
return makeFix(state, fieldDecl, tree, "May assign null to field");
default:
throw new AssertionError("Impossible: " + nullness);
}
}