下面列出了com.sun.source.tree.NewClassTree#getArguments ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Description matchNewClass(NewClassTree tree, VisitorState state) {
if (!matchWithinClass) {
return Description.NO_MATCH;
}
Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree);
if (methodSymbol == null) {
throw new RuntimeException("not expecting unresolved method here");
}
List<? extends ExpressionTree> actualParams = tree.getArguments();
if (tree.getClassBody() != null && actualParams.size() > 0) {
// passing parameters to constructor of anonymous class
// this constructor just invokes the constructor of the superclass, and
// in the AST does not have the parameter nullability annotations from the superclass.
// so, treat as if the superclass constructor is being invoked directly
// see https://github.com/uber/NullAway/issues/102
methodSymbol = getSymbolOfSuperConstructor(methodSymbol, state);
}
return handleInvocation(tree, state, methodSymbol, actualParams);
}
private TreeNode convertNewClass(NewClassTree node, TreePath parent) {
TreePath path = getTreePath(parent, node);
ClassInstanceCreation newNode = new ClassInstanceCreation();
Expression enclosingExpression = (Expression) convert(node.getEnclosingExpression(), path);
ExecutableElement executable = (ExecutableElement) getElement(path);
TypeMirror vargarsType = ((JCNewClass) node).varargsElement;
// Case where the first parameter of the constructor of an inner class is the outer class (e.g.
// new Outer().new Inner(...). Move the enclosing expression (e.g. new Outer()) as the first
// argument. A varargs parameter could unintentionally trigger this condition because it could
// map to zero arguments.
if (executable.getParameters().size() - node.getArguments().size() == 1
&& vargarsType == null) {
newNode.addArgument(enclosingExpression);
enclosingExpression = null;
}
for (ExpressionTree arg : node.getArguments()) {
newNode.addArgument((Expression) convert(arg, path));
}
return newNode
.setExecutablePair(new ExecutablePair(executable))
.setVarargsType(vargarsType)
.setExpression(enclosingExpression)
.setType(convertType(getTypeMirror(getTreePath(path, node.getIdentifier()))))
.setAnonymousClassDeclaration((TypeDeclaration) convert(node.getClassBody(), path));
}
private static List<? extends TypeMirror> computeNewClass(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
NewClassTree nct = (NewClassTree) parent.getLeaf();
boolean errorInRealArguments = false;
for (Tree param : nct.getArguments()) {
errorInRealArguments |= param == error;
}
if (errorInRealArguments) {
List<TypeMirror> proposedTypes = new ArrayList<TypeMirror>();
int[] proposedIndex = new int[1];
List<ExecutableElement> ee = fuzzyResolveMethodInvocation(info, parent, proposedTypes, proposedIndex);
if (ee.isEmpty()) { //cannot be resolved
return null;
}
types.add(ElementKind.PARAMETER);
types.add(ElementKind.LOCAL_VARIABLE);
types.add(ElementKind.FIELD);
return proposedTypes;
}
Tree id = nct.getIdentifier();
if (id.getKind() == Kind.PARAMETERIZED_TYPE) {
id = ((ParameterizedTypeTree) id).getType();
}
if (id == error) {
return resolveType(EnumSet.noneOf(ElementKind.class), info, parent.getParentPath(), nct, offset, null, null);
}
return null;
}
private int getLambdaIndexFromInvokingTree(Tree invokingTree) {
List<? extends ExpressionTree> invokingArgs;
if (invokingTree.getKind() == Tree.Kind.METHOD_INVOCATION) {
MethodInvocationTree invokingMethTree = ((MethodInvocationTree) invokingTree);
invokingArgs = invokingMethTree.getArguments();
} else if (invokingTree.getKind() == Tree.Kind.NEW_CLASS) {
NewClassTree invokingConstrTree = (NewClassTree) invokingTree;
invokingArgs = invokingConstrTree.getArguments();
} else {
return -1;
}
return getIndexOfLambdaInArgs(newClassTree, invokingArgs);
}
@Override
public Boolean visitNewClass(NewClassTree node, Void p) {
scan(node.getEnclosingExpression(), p);
for (ExpressionTree et : node.getArguments()) {
Boolean used = scan(et, p);
if (used == Boolean.TRUE) {
passedToMethod = true;
}
}
return false;
}
private static List<? extends TypeMirror> computeNewClass(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
NewClassTree nct = (NewClassTree) parent.getLeaf();
boolean errorInRealArguments = false;
for (Tree param : nct.getArguments()) {
errorInRealArguments |= param == error;
}
if (errorInRealArguments) {
List<TypeMirror> proposedTypes = new ArrayList<TypeMirror>();
int[] proposedIndex = new int[1];
List<ExecutableElement> ee = org.netbeans.modules.editor.java.Utilities.fuzzyResolveMethodInvocation(info, parent, proposedTypes, proposedIndex);
if (ee.isEmpty()) { //cannot be resolved
return null;
}
types.add(ElementKind.PARAMETER);
types.add(ElementKind.LOCAL_VARIABLE);
types.add(ElementKind.FIELD);
return proposedTypes;
}
Tree id = nct.getIdentifier();
if (id.getKind() == Kind.PARAMETERIZED_TYPE) {
id = ((ParameterizedTypeTree) id).getType();
}
if (id == error) {
return resolveType(EnumSet.noneOf(ElementKind.class), info, parent.getParentPath(), nct, offset, null, null);
}
return null;
}
@Override
public MethodArgument[] visitNewClass(NewClassTree node, Object p) {
if (!methodInvocation || offset != positions.getEndPosition(tree, node.getIdentifier())) {
return super.visitNewClass(node, p);
}
List<? extends ExpressionTree> args = node.getArguments();
List<? extends Tree> argTypes = node.getTypeArguments();
arguments = composeArguments(args, argTypes);
return arguments;
}
public static TreePath validateSelection(CompilationInfo ci, int start, int end, Set<TypeKind> ignoredTypes) {
if(start == end) {
TokenSequence<JavaTokenId> cts = ci.getTokenHierarchy().tokenSequence(JavaTokenId.language());
if (cts != null) {
cts.move(start);
if (cts.moveNext() && cts.token().id() != JavaTokenId.WHITESPACE && cts.offset() == start) {
start = end += 1;
}
}
}
TreePath tp = ci.getTreeUtilities().pathFor(start == end? start : (start + end) / 2 + 1);
for ( ; tp != null; tp = tp.getParentPath()) {
Tree leaf = tp.getLeaf();
if ( !ExpressionTree.class.isAssignableFrom(leaf.getKind().asInterface())
&& (leaf.getKind() != Tree.Kind.VARIABLE || ((VariableTree) leaf).getInitializer() == null)) {
continue;
}
long treeStart = ci.getTrees().getSourcePositions().getStartPosition(ci.getCompilationUnit(), leaf);
long treeEnd = ci.getTrees().getSourcePositions().getEndPosition(ci.getCompilationUnit(), leaf);
if (start != end) {
if (treeStart != start || treeEnd != end) {
continue;
}
} else {
if (treeStart != start && treeEnd != end) {
continue;
}
}
TypeMirror type = ci.getTrees().getTypeMirror(tp);
if (type != null && type.getKind() == TypeKind.ERROR) {
type = ci.getTrees().getOriginalType((ErrorType) type);
}
if (type == null || ignoredTypes.contains(type.getKind()))
continue;
if(tp.getLeaf().getKind() == Tree.Kind.ASSIGNMENT)
continue;
if (tp.getLeaf().getKind() == Tree.Kind.ANNOTATION)
continue;
if (!isInsideClass(tp))
return null;
TreePath candidate = tp;
tp = tp.getParentPath();
while (tp != null) {
switch (tp.getLeaf().getKind()) {
case VARIABLE:
VariableTree vt = (VariableTree) tp.getLeaf();
if (vt.getInitializer() == leaf) {
return candidate;
} else {
return null;
}
case NEW_CLASS:
NewClassTree nct = (NewClassTree) tp.getLeaf();
if (nct.getIdentifier().equals(candidate.getLeaf())) { //avoid disabling hint ie inside of anonymous class higher in treepath
for (Tree p : nct.getArguments()) {
if (p == leaf) {
return candidate;
}
}
return null;
}
}
leaf = tp.getLeaf();
tp = tp.getParentPath();
}
return candidate;
}
return null;
}
private static boolean checkMethodInvocation(HintContext ctx, TreePath invPath, TreePath valPath) {
Trees trees = ctx.getInfo().getTrees();
Tree invLeaf = invPath.getLeaf();
List<? extends ExpressionTree> arguments;
TypeMirror m;
switch (invLeaf.getKind()) {
case METHOD_INVOCATION: {
MethodInvocationTree mit = (MethodInvocationTree)invLeaf;
arguments = mit.getArguments();
m = trees.getTypeMirror(new TreePath(invPath, mit.getMethodSelect()));
break;
}
case NEW_CLASS: {
NewClassTree nct = (NewClassTree)invLeaf;
arguments = nct.getArguments();
Element e = trees.getElement(invPath);
TypeMirror cl = trees.getTypeMirror(invPath);
if (!Utilities.isValidType(cl) || cl.getKind().isPrimitive()) {
return false;
}
m = ctx.getInfo().getTypes().asMemberOf((DeclaredType)cl, e);
break;
}
default:
return false;
}
if (!Utilities.isValidType(m) || m.getKind() != TypeKind.EXECUTABLE) {
return false;
}
ExecutableType execType = (ExecutableType)m;
int idx = arguments.indexOf(ctx.getPath().getLeaf());
if (idx < 0 || idx >= execType.getParameterTypes().size()) {
return false;
}
TypeMirror paramType = execType.getParameterTypes().get(idx);
TypeMirror curType = trees.getTypeMirror(ctx.getPath());
TypeMirror valType = trees.getTypeMirror(valPath);
if (!paramType.getKind().isPrimitive() && valType.getKind().isPrimitive()) {
valType = ctx.getInfo().getTypes().boxedClass((PrimitiveType)valType).asType();
// ensure that the passed INSTANCE type will not change when the boxing is removed
if (!ctx.getInfo().getTypes().isSameType(curType, valType)) {
return false;
}
}
return Utilities.checkAlternativeInvocation(ctx.getInfo(), invPath, ctx.getPath(), valPath, null);
}
public static TreePath validateSelection(CompilationInfo ci, int start, int end, Set<TypeKind> ignoredTypes) {
int[] span = TreeUtils.ignoreWhitespaces(ci, Math.min(start, end), Math.max(start, end));
start = span[0];
end = span[1];
TreePath tp = ci.getTreeUtilities().pathFor((start + end) / 2 + 1);
for ( ; tp != null; tp = tp.getParentPath()) {
Tree leaf = tp.getLeaf();
if ( !ExpressionTree.class.isAssignableFrom(leaf.getKind().asInterface())
&& (leaf.getKind() != Kind.VARIABLE || ((VariableTree) leaf).getInitializer() == null))
continue;
long treeStart = ci.getTrees().getSourcePositions().getStartPosition(ci.getCompilationUnit(), leaf);
long treeEnd = ci.getTrees().getSourcePositions().getEndPosition(ci.getCompilationUnit(), leaf);
if (treeStart != start || treeEnd != end) {
continue;
}
TypeMirror type = ci.getTrees().getTypeMirror(tp);
if (type != null && type.getKind() == TypeKind.ERROR) {
type = ci.getTrees().getOriginalType((ErrorType) type);
}
if (type == null || ignoredTypes.contains(type.getKind()))
continue;
if(tp.getLeaf().getKind() == Kind.ASSIGNMENT)
continue;
if (tp.getLeaf().getKind() == Kind.ANNOTATION)
continue;
if (!TreeUtils.isInsideClass(tp))
return null;
TreePath candidate = tp;
tp = tp.getParentPath();
while (tp != null) {
switch (tp.getLeaf().getKind()) {
case VARIABLE:
VariableTree vt = (VariableTree) tp.getLeaf();
if (vt.getInitializer() == leaf) {
return candidate;
} else {
return null;
}
case NEW_CLASS:
NewClassTree nct = (NewClassTree) tp.getLeaf();
if (nct.getIdentifier().equals(candidate.getLeaf())) { //avoid disabling hint ie inside of anonymous class higher in treepath
for (Tree p : nct.getArguments()) {
if (p == leaf) {
return candidate;
}
}
return null;
}
}
leaf = tp.getLeaf();
tp = tp.getParentPath();
}
return candidate;
}
return null;
}
private List<Documentation> documentationImpl(String code, int cursor, boolean computeJavadoc) {
code = code.substring(0, cursor);
if (code.trim().isEmpty()) { //TODO: comment handling
code += ";";
}
if (guessKind(code) == Kind.IMPORT)
return Collections.emptyList();
OuterWrap codeWrap = proc.outerMap.wrapInTrialClass(Wrap.methodWrap(code));
AnalyzeTask at = proc.taskFactory.new AnalyzeTask(codeWrap, keepParameterNames);
SourcePositions sp = at.trees().getSourcePositions();
CompilationUnitTree topLevel = at.firstCuTree();
TreePath tp = pathFor(topLevel, sp, codeWrap.snippetIndexToWrapIndex(cursor));
if (tp == null)
return Collections.emptyList();
TreePath prevPath = null;
while (tp != null && tp.getLeaf().getKind() != Kind.METHOD_INVOCATION &&
tp.getLeaf().getKind() != Kind.NEW_CLASS && tp.getLeaf().getKind() != Kind.IDENTIFIER &&
tp.getLeaf().getKind() != Kind.MEMBER_SELECT) {
prevPath = tp;
tp = tp.getParentPath();
}
if (tp == null)
return Collections.emptyList();
Stream<Element> elements;
Iterable<Pair<ExecutableElement, ExecutableType>> candidates;
List<? extends ExpressionTree> arguments;
if (tp.getLeaf().getKind() == Kind.METHOD_INVOCATION || tp.getLeaf().getKind() == Kind.NEW_CLASS) {
if (tp.getLeaf().getKind() == Kind.METHOD_INVOCATION) {
MethodInvocationTree mit = (MethodInvocationTree) tp.getLeaf();
candidates = methodCandidates(at, tp);
arguments = mit.getArguments();
} else {
NewClassTree nct = (NewClassTree) tp.getLeaf();
candidates = newClassCandidates(at, tp);
arguments = nct.getArguments();
}
if (!isEmptyArgumentsContext(arguments)) {
List<TypeMirror> actuals = computeActualInvocationTypes(at, arguments, prevPath);
List<TypeMirror> fullActuals = actuals != null ? actuals : Collections.emptyList();
candidates =
this.filterExecutableTypesByArguments(at, candidates, fullActuals)
.stream()
.filter(method -> parameterType(method.fst, method.snd, fullActuals.size(), true).findAny().isPresent())
.collect(Collectors.toList());
}
elements = Util.stream(candidates).map(method -> method.fst);
} else if (tp.getLeaf().getKind() == Kind.IDENTIFIER || tp.getLeaf().getKind() == Kind.MEMBER_SELECT) {
Element el = at.trees().getElement(tp);
if (el == null ||
el.asType().getKind() == TypeKind.ERROR ||
(el.getKind() == ElementKind.PACKAGE && el.getEnclosedElements().isEmpty())) {
//erroneous element:
return Collections.emptyList();
}
elements = Stream.of(el);
} else {
return Collections.emptyList();
}
List<Documentation> result = Collections.emptyList();
try (JavadocHelper helper = JavadocHelper.create(at.task, findSources())) {
result = elements.map(el -> constructDocumentation(at, helper, el, computeJavadoc))
.filter(Objects::nonNull)
.collect(Collectors.toList());
} catch (IOException ex) {
proc.debug(ex, "JavadocHelper.close()");
}
return result;
}