下面列出了com.sun.source.tree.AssignmentTree#getExpression ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
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);
}
@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, config)) {
// field already annotated
return Description.NO_MATCH;
}
ExpressionTree expression = tree.getExpression();
if (mayBeNullExpr(state, expression)) {
String message = "assigning @Nullable expression to @NonNull field";
return errorBuilder.createErrorDescriptionForNullAssignment(
new ErrorMessage(MessageTypes.ASSIGN_FIELD_NULLABLE, message),
expression,
buildDescription(tree),
state);
}
return Description.NO_MATCH;
}
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);
}
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);
}