下面列出了com.sun.source.tree.ExpressionTree#getKind ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static int rowLength(List<? extends ExpressionTree> row) {
int size = 0;
for (ExpressionTree tree : row) {
if (tree.getKind() != NEW_ARRAY) {
size++;
continue;
}
NewArrayTree array = (NewArrayTree) tree;
if (array.getInitializers() == null) {
size++;
continue;
}
size += rowLength(array.getInitializers());
}
return size;
}
private void dotExpressionArgsAndParen(
ExpressionTree expression, Indent tyargIndent, Indent indent) {
Deque<ExpressionTree> indices = getArrayIndices(expression);
expression = getArrayBase(expression);
switch (expression.getKind()) {
case METHOD_INVOCATION:
builder.open(tyargIndent);
MethodInvocationTree methodInvocation = (MethodInvocationTree) expression;
addArguments(methodInvocation.getArguments(), indent);
builder.close();
break;
default:
break;
}
formatArrayIndices(indices);
}
static int rowLength(List<? extends ExpressionTree> row) {
int size = 0;
for (ExpressionTree tree : row) {
if (tree.getKind() != NEW_ARRAY) {
size++;
continue;
}
NewArrayTree array = (NewArrayTree) tree;
if (array.getInitializers() == null) {
size++;
continue;
}
size += rowLength(array.getInitializers());
}
return size;
}
private void dotExpressionArgsAndParen(
ExpressionTree expression, Indent tyargIndent, Indent indent) {
Deque<ExpressionTree> indices = getArrayIndices(expression);
expression = getArrayBase(expression);
switch (expression.getKind()) {
case METHOD_INVOCATION:
builder.open(tyargIndent);
MethodInvocationTree methodInvocation = (MethodInvocationTree) expression;
addArguments(methodInvocation.getArguments(), indent);
builder.close();
break;
default:
break;
}
formatArrayIndices(indices);
}
private void dotExpressionUpToArgs(ExpressionTree expression, Optional<BreakTag> tyargTag) {
expression = getArrayBase(expression);
switch (expression.getKind()) {
case MEMBER_SELECT:
MemberSelectTree fieldAccess = (MemberSelectTree) expression;
visit(fieldAccess.getIdentifier());
break;
case METHOD_INVOCATION:
MethodInvocationTree methodInvocation = (MethodInvocationTree) expression;
if (!methodInvocation.getTypeArguments().isEmpty()) {
builder.open(plusFour);
addTypeArguments(methodInvocation.getTypeArguments(), ZERO);
// TODO(jdd): Should indent the name -4.
builder.breakOp(Doc.FillMode.UNIFIED, "", ZERO, tyargTag);
builder.close();
}
visit(getMethodName(methodInvocation));
break;
case IDENTIFIER:
visit(((IdentifierTree) expression).getName());
break;
default:
scan(expression, null);
break;
}
}
private static String fullyQualifiedPath(TreePath path) {
ExpressionTree packageNameExpr = path.getCompilationUnit().getPackageName();
MemberSelectTree packageID = packageNameExpr.getKind() == Tree.Kind.MEMBER_SELECT
? ((MemberSelectTree) packageNameExpr) : null;
StringBuilder result = new StringBuilder();
if (packageID != null) {
result.append(packageID.getExpression().toString())
.append(".")
.append(packageID.getIdentifier().toString());
}
Tree.Kind kind = path.getLeaf().getKind();
String leafName = null;
if (kind == Tree.Kind.CLASS || kind == Tree.Kind.INTERFACE) {
leafName = ((ClassTree) path.getLeaf()).getSimpleName().toString();
} else if (kind == Tree.Kind.ENUM) {
if (path.getParentPath() != null) {
Tree parent = path.getParentPath().getLeaf();
if (parent.getKind() == Tree.Kind.CLASS || parent.getKind() == Tree.Kind.INTERFACE) {
result.append(((ClassTree) parent).getSimpleName().toString()).append(".");
}
leafName = ((ClassTree) path.getLeaf()).getSimpleName().toString();
}
}
// leafName can be empty for anonymous inner classes, for example.
boolean isUsefulLeaf = leafName != null && !leafName.isEmpty();
if (isUsefulLeaf) {
result.append(".").append(leafName);
}
return isUsefulLeaf ? result.toString() : null;
}
@Override
public List<Tree> visitConditionalExpression(ConditionalExpressionTree node, ExpressionScanner.ExpressionsInfo p) {
ExpressionTree condition = node.getCondition();
List<Tree> cond = scan(condition, p);
Tree lastCond = null;
Boolean resolvedCondition = null;
if (cond != null) {
lastCond = cond.get(cond.size() - 1);
} else {
if (condition.getKind() == Tree.Kind.BOOLEAN_LITERAL) {
resolvedCondition = Boolean.parseBoolean(condition.toString());
}
}
List<Tree> rT;
List<Tree> rF;
if (resolvedCondition != null) {
if (resolvedCondition) {
rT = scan(node.getTrueExpression(), p);
rF = null;
} else {
rT = null;
rF = scan(node.getFalseExpression(), p);
}
} else {
rT = scan(node.getTrueExpression(), p);
rF = scan(node.getFalseExpression(), p);
}
if (lastCond != null) {
if (rT != null) {
p.addNextExpression(lastCond, rT.get(0));
}
if (rF != null) {
p.addNextExpression(lastCond, rF.get(0));
}
}
return reduce(reduce(cond, rT), rF);
}
private boolean fillFirstArgument(
ExpressionTree e,
List<ExpressionTree> items,
Indent indent) {
// is there a trailing dereference?
if (items.size() < 2) {
return false;
}
// don't special-case calls nested inside expressions
if (e.getKind() != METHOD_INVOCATION) {
return false;
}
MethodInvocationTree methodInvocation = (MethodInvocationTree) e;
Name name = getMethodName(methodInvocation);
if (!(methodInvocation.getMethodSelect() instanceof IdentifierTree)
|| name.length() > 4
|| !methodInvocation.getTypeArguments().isEmpty()
|| methodInvocation.getArguments().size() != 1) {
return false;
}
builder.open(ZERO);
builder.open(indent);
visit(name);
token("(");
ExpressionTree arg = getOnlyElement(methodInvocation.getArguments());
scan(arg, null);
builder.close();
token(")");
builder.close();
return true;
}
private ExpressionTree transformInitializer(ExpressionTree initializer, Tree type, TreeMaker make) {
if(initializer.getKind() == Tree.Kind.NEW_ARRAY) {
NewArrayTree nat = (NewArrayTree) initializer;
if(nat.getType() == null) {
if(type.getKind() == Tree.Kind.ARRAY_TYPE) {
ArrayTypeTree arrayTypeTree = (ArrayTypeTree) type;
type = arrayTypeTree.getType();
}
return make.NewArray(type, nat.getDimensions(), nat.getInitializers());
}
}
return initializer;
}
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
for (String name : handledAnnotations) {
AnnotationTree at =
ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), name);
if (at != null) {
for (ExpressionTree et : at.getArguments()) {
if (et.getKind() == Kind.ASSIGNMENT) {
AssignmentTree assn = (AssignmentTree) et;
if (ASTHelpers.getSymbol(assn.getExpression())
.getQualifiedName()
.toString()
.endsWith(xpFlagName)) {
Description.Builder builder = buildDescription(tree);
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
if (isTreated) {
fixBuilder.delete(at);
decrementAllSymbolUsages(at, state, fixBuilder);
} else {
fixBuilder.delete(tree);
decrementAllSymbolUsages(tree, state, fixBuilder);
}
builder.addFix(fixBuilder.build());
return builder.build();
}
}
}
}
}
return Description.NO_MATCH;
}
@Override
public Boolean visitAnnotation(AnnotationTree node, TreePath p) {
if (p == null)
return super.visitAnnotation(node, p);
AnnotationTree t = (AnnotationTree) p.getLeaf();
List<? extends ExpressionTree> arguments = t.getArguments();
if (arguments.size() == 1) {
ExpressionTree arg = arguments.get(0);
if (arg.getKind() == Kind.ASSIGNMENT) {
AssignmentTree at = (AssignmentTree) arg;
if ( at.getVariable().getKind() == Kind.IDENTIFIER
&& isMultistatementWildcardTree(at.getExpression())
&& ((IdentifierTree) at.getVariable()).getName().contentEquals("value")) {
arguments = Collections.singletonList(at.getExpression());
}
}
}
if (!checkLists(node.getArguments(), arguments, p))
return false;
return scan(node.getAnnotationType(), t.getAnnotationType(), p);
}
private boolean fillFirstArgument(ExpressionTree e, List<ExpressionTree> items, Indent indent) {
// is there a trailing dereference?
if (items.size() < 2) {
return false;
}
// don't special-case calls nested inside expressions
if (e.getKind() != METHOD_INVOCATION) {
return false;
}
MethodInvocationTree methodInvocation = (MethodInvocationTree) e;
Name name = getMethodName(methodInvocation);
if (!(methodInvocation.getMethodSelect() instanceof IdentifierTree)
|| name.length() > 4
|| !methodInvocation.getTypeArguments().isEmpty()
|| methodInvocation.getArguments().size() != 1) {
return false;
}
builder.open(ZERO);
builder.open(indent);
visit(name);
token("(");
ExpressionTree arg = getOnlyElement(methodInvocation.getArguments());
scan(arg, null);
builder.close();
token(")");
builder.close();
return true;
}
@Override
public boolean onOverrideMayBeNullExpr(
NullAway analysis, ExpressionTree expr, VisitorState state, boolean exprMayBeNull) {
if (expr.getKind() == Tree.Kind.METHOD_INVOCATION
&& optionalIsGetCall((Symbol.MethodSymbol) ASTHelpers.getSymbol(expr), state.getTypes())) {
return true;
}
return exprMayBeNull;
}
private static String getMemberName(ExpressionTree node) {
switch (node.getKind()) {
case IDENTIFIER:
return node.toString();
case MEMBER_SELECT:
return ((MemberSelectTree) node).getIdentifier().toString();
default:
return null;
}
}
@Override
public Map<ExecutableElement, TreeBackedAnnotationValue> getElementValues() {
if (elementValues == null) {
Map<ExecutableElement, TreeBackedAnnotationValue> result = new LinkedHashMap<>();
Map<String, Tree> trees = new HashMap<>();
List<? extends ExpressionTree> arguments = tree.getArguments();
for (ExpressionTree argument : arguments) {
if (argument.getKind() != Tree.Kind.ASSIGNMENT) {
trees.put("value", argument);
} else {
AssignmentTree assignment = (AssignmentTree) argument;
IdentifierTree nameTree = (IdentifierTree) assignment.getVariable();
trees.put(nameTree.getName().toString(), argument);
}
}
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
underlyingAnnotationMirror.getElementValues().entrySet()) {
ExecutableElement underlyingKeyElement = entry.getKey();
Tree valueTree =
Objects.requireNonNull(trees.get(entry.getKey().getSimpleName().toString()));
result.put(
canonicalizer.getCanonicalElement(underlyingKeyElement),
new TreeBackedAnnotationValue(
entry.getValue(), new TreePath(treePath, valueTree), canonicalizer));
}
elementValues = Collections.unmodifiableMap(result);
}
return elementValues;
}
private List<ProspectiveOperation> getIfListRepresentation( StatementTree tree, boolean last) {
IfTree ifTree = (IfTree) tree;
List<ProspectiveOperation> ls = new ArrayList<ProspectiveOperation>();
if (ifTree.getElseStatement() == null) {
StatementTree then = ifTree.getThenStatement();
if (isOneStatementBlock(then)) {
then = ((BlockTree) then).getStatements().get(0);
}
if (then.getKind() == Tree.Kind.RETURN) {
ReturnTree returnTree = (ReturnTree) then;
ExpressionTree returnExpression = returnTree.getExpression();
if (returnExpression.getKind() == Tree.Kind.BOOLEAN_LITERAL && ((LiteralTree) returnExpression).getValue().equals(true)) {
ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.ANYMATCH, this.preconditionsChecker, this.workingCopy));
} else if (returnExpression.getKind() == Tree.Kind.BOOLEAN_LITERAL && ((LiteralTree) returnExpression).getValue().equals(false)) {
ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.NONEMATCH, this.preconditionsChecker, this.workingCopy));
}
} else {
ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.FILTER, this.preconditionsChecker, this.workingCopy));
ls.addAll(getListRepresentation(ifTree.getThenStatement(), last));
}
} else {
ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.MAP, this.preconditionsChecker, this.workingCopy));
}
return ls;
}
@Override
public Tree visitReturn(ReturnTree that, Trees trees) {
ExpressionTree thatExpression = that.getExpression();
if (!this.hasMatcherReturn && thatExpression != null && thatExpression.getKind() == Tree.Kind.BOOLEAN_LITERAL
&& thisIsMatcherReturn(that, this.getCurrentPath())) {
this.hasMatcherReturn = true;
} else {
this.hasReturns = true;
}
return super.visitReturn(that, trees);
}
private void negate(WorkingCopy copy, ExpressionTree original, Tree parent) {
TreeMaker make = copy.getTreeMaker();
ExpressionTree newTree;
switch (original.getKind()) {
case PARENTHESIZED:
ExpressionTree expr = ((ParenthesizedTree) original).getExpression();
negate(copy, expr, original);
return ;
case LOGICAL_COMPLEMENT:
newTree = ((UnaryTree) original).getExpression();
while (newTree.getKind() == Kind.PARENTHESIZED && !JavaFixUtilities.requiresParenthesis(((ParenthesizedTree) newTree).getExpression(), original, parent)) {
newTree = ((ParenthesizedTree) newTree).getExpression();
}
break;
case NOT_EQUAL_TO:
newTree = negateBinaryOperator(copy, original, Kind.EQUAL_TO, false);
break;
case EQUAL_TO:
newTree = negateBinaryOperator(copy, original, Kind.NOT_EQUAL_TO, false);
break;
case BOOLEAN_LITERAL:
newTree = make.Literal(!(Boolean) ((LiteralTree) original).getValue());
break;
case CONDITIONAL_AND:
newTree = negateBinaryOperator(copy, original, Kind.CONDITIONAL_OR, true);
break;
case CONDITIONAL_OR:
newTree = negateBinaryOperator(copy, original, Kind.CONDITIONAL_AND, true);
break;
case LESS_THAN:
newTree = negateBinaryOperator(copy, original, Kind.GREATER_THAN_EQUAL, false);
break;
case LESS_THAN_EQUAL:
newTree = negateBinaryOperator(copy, original, Kind.GREATER_THAN, false);
break;
case GREATER_THAN:
newTree = negateBinaryOperator(copy, original, Kind.LESS_THAN_EQUAL, false);
break;
case GREATER_THAN_EQUAL:
newTree = negateBinaryOperator(copy, original, Kind.LESS_THAN, false);
break;
default:
newTree = make.Unary(Kind.LOGICAL_COMPLEMENT, original);
if (JavaFixUtilities.requiresParenthesis(original, original, newTree)) {
newTree = make.Unary(Kind.LOGICAL_COMPLEMENT, make.Parenthesized(original));
}
break;
}
if (JavaFixUtilities.requiresParenthesis(newTree, original, parent)) {
newTree = make.Parenthesized(newTree);
}
copy.rewrite(original, newTree);
}
@Override
protected void performRewrite(TransformationContext ctx) {
WorkingCopy copy = ctx.getWorkingCopy();
TreePath path = ctx.getPath();
if (path != null) {
TreeMaker make = copy.getTreeMaker();
BinaryTree oldTree = (BinaryTree) path.getLeaf();
ExpressionTree left = oldTree.getLeftOperand();
ExpressionTree right = oldTree.getRightOperand();
ExpressionTree newTree;
if (kind == Kind.REVERSE_OPERANDS) {
// "str2".equals(str1)
ExpressionTree rightEquals = make.MemberSelect(right, "equals"); // NOI18N
ExpressionTree rightEqualsLeft = make.MethodInvocation(Collections.<ExpressionTree>emptyList(), rightEquals, Collections.singletonList(left));
rightEqualsLeft = matchSign(make, oldTree, rightEqualsLeft);
newTree = rightEqualsLeft;
} else {
ExpressionTree leftEquals = make.MemberSelect(left, "equals"); // NOI18N
ExpressionTree leftEqualsRight = make.MethodInvocation(Collections.<ExpressionTree>emptyList(), leftEquals, Collections.singletonList(right));
leftEqualsRight = matchSign(make, oldTree, leftEqualsRight);
if (kind == Kind.NO_NULL_CHECK) {
// str1.equals(str2)
newTree = leftEqualsRight;
} else {
ExpressionTree leftEqNull = make.Binary(Tree.Kind.EQUAL_TO, left, make.Identifier("null")); // NOI18N
ExpressionTree rightEqNull = make.Binary(oldTree.getKind(), right, make.Identifier("null")); // NOI18N
if (kind == Kind.NULL_CHECK_TERNARY) {
// str1 == null ? str2 == null : str1.equals(str2)
newTree = make.ConditionalExpression(leftEqNull, rightEqNull, leftEqualsRight);
} else {
ExpressionTree leftNeNull = make.Binary(Tree.Kind.NOT_EQUAL_TO, left, make.Identifier("null")); // NOI18N
ExpressionTree leftNeNullAndLeftEqualsRight = make.Binary(Tree.Kind.CONDITIONAL_AND, leftNeNull, leftEqualsRight);
if (right.getKind() == Tree.Kind.STRING_LITERAL) {
// str1 != null && str1.equals("str2")
newTree = leftNeNullAndLeftEqualsRight;
} else {
// (str1 == null && str2 == null) || (str1 != null && str1.equals(str2))
ExpressionTree leftEqNullAndRightEqNull = make.Binary(Tree.Kind.CONDITIONAL_AND, leftEqNull, rightEqNull);
newTree = make.Binary(Tree.Kind.CONDITIONAL_OR, make.Parenthesized(leftEqNullAndRightEqNull), make.Parenthesized(leftNeNullAndLeftEqualsRight));
}
}
if (path.getParentPath().getLeaf().getKind() != Tree.Kind.PARENTHESIZED) {
newTree = make.Parenthesized(newTree);
}
}
}
copy.rewrite(oldTree, newTree);
}
}
private void createStringResults(CompilationController controller, Env env)
throws IOException {
TreePath elementPath = env.getPath();
TreePath parentPath = elementPath.getParentPath();
Tree parent = parentPath.getLeaf();
Tree grandParent = parentPath.getParentPath().getLeaf();
switch (grandParent.getKind()) {
case ANNOTATION : {
switch (parent.getKind()) {
case ASSIGNMENT : {
ExpressionTree var = ((AssignmentTree)parent).getVariable();
if (var.getKind() == Kind.IDENTIFIER) {
Name name = ((IdentifierTree)var).getName();
if (!name.contentEquals("wsdlLocation") || //NOI18N
jaxWsSupport==null)
{
return;
}
controller.toPhase(Phase.ELEMENTS_RESOLVED);
TypeElement webMethodEl = controller
.getElements().getTypeElement(
"javax.jws.WebService"); // NOI18N
if (webMethodEl == null) {
hasErrors = true;
return;
}
TypeMirror el = controller.getTrees().getTypeMirror(
parentPath.getParentPath());
if (el == null || el.getKind() == TypeKind.ERROR) {
hasErrors = true;
return;
}
if (controller.getTypes().isSameType(el,
webMethodEl.asType()))
{
FileObject wsdlFolder = jaxWsSupport
.getWsdlFolder(false);
if (wsdlFolder != null) {
Enumeration<? extends FileObject> en =
wsdlFolder.getChildren(true);
while (en.hasMoreElements()) {
FileObject fo = en.nextElement();
if (!fo.isData() || !"wsdl" // NOI18N
.equalsIgnoreCase(fo.getExt()))
{
continue;
}
String wsdlPath = FileUtil.getRelativePath(
wsdlFolder.getParent()
.getParent(),fo);
// Temporary fix for wsdl
// files in EJB project
if (wsdlPath.startsWith("conf/")) // NOI18
{
wsdlPath = "META-INF/"+ wsdlPath// NOI18
.substring(5);
}
if (wsdlPath.startsWith(env.getPrefix()))
{
results.add(WSCompletionItem
.createWsdlFileItem(
wsdlFolder,
fo,
env.getOffset()));
}
}
}
}
}
} break; //ASSIGNMENT
}
} break; // ANNOTATION
}
}