本文整理汇总了Java中com.sun.source.util.Trees.getElement方法的典型用法代码示例。如果您正苦于以下问题:Java Trees.getElement方法的具体用法?Java Trees.getElement怎么用?Java Trees.getElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.util.Trees
的用法示例。
在下文中一共展示了Trees.getElement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findTopClassElems
import com.sun.source.util.Trees; //导入方法依赖的package包/类
/**
*
* @return list of {@code Element}s representing top classes,
* or an empty list of none were found
*/
private static List<TypeElement> findTopClassElems(
CompilationInfo compInfo,
CompilationUnitTree compilationUnit,
Filter filter) {
List<? extends Tree> typeDecls = compilationUnit.getTypeDecls();
if ((typeDecls == null) || typeDecls.isEmpty()) {
return Collections.<TypeElement>emptyList();
}
List<TypeElement> result = new ArrayList<TypeElement>(typeDecls.size());
Trees trees = compInfo.getTrees();
for (Tree typeDecl : typeDecls) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) {
Element element = trees.getElement(
new TreePath(new TreePath(compilationUnit), typeDecl));
TypeElement typeElement = (TypeElement) element;
if (filter.passes(typeElement, compInfo)) {
result.add(typeElement);
}
}
}
return result;
}
示例2: visitNewClass
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Tree visitNewClass(NewClassTree tree, Element p) {
if (constructorRefactoring && !compatible && !workingCopy.getTreeUtilities().isSynthetic(getCurrentPath())) {
ExecutableElement constructor = (ExecutableElement) p;
final Trees trees = workingCopy.getTrees();
Element el = trees.getElement(getCurrentPath());
el = resolveAnonymousClassConstructor(el, tree, trees);
if (isMethodMatch(el, p)) {
List<ExpressionTree> arguments = getNewArguments(tree.getArguments(), false, constructor);
NewClassTree nju = make.NewClass(tree.getEnclosingExpression(),
(List<ExpressionTree>)tree.getTypeArguments(),
tree.getIdentifier(),
arguments,
tree.getClassBody());
rewrite(tree, nju);
}
}
return super.visitNewClass(tree, p);
}
示例3: assignmentToForLoopParam
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToForLoopParam", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToForLoopParam", category = "assignment_issues", enabled = false, suppressWarnings = "AssignmentToForLoopParameter", options=Options.QUERY) //NOI18N
@TriggerPatterns({
@TriggerPattern(value = "for ($paramType $param = $init; $expr; $update) $statement;"), //NOI18N
@TriggerPattern(value = "for ($paramType $param : $expr) $statement;") //NOI18N
})
public static List<ErrorDescription> assignmentToForLoopParam(HintContext context) {
final Trees trees = context.getInfo().getTrees();
final TreePath paramPath = context.getVariables().get("$param"); //NOI18N
final Element param = trees.getElement(paramPath);
if (param == null || param.getKind() != ElementKind.LOCAL_VARIABLE) {
return null;
}
final TreePath stat = context.getVariables().get("$statement"); //NOI18N
final List<TreePath> paths = new LinkedList<TreePath>();
new AssignmentFinder(trees, param).scan(stat, paths);
final List<ErrorDescription> ret = new ArrayList<ErrorDescription>(paths.size());
for (TreePath path : paths) {
ret.add(ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class, "MSG_AssignmentToForLoopParam", param.getSimpleName()))); //NOI18N
}
return ret;
}
示例4: visitNewClass
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Tree visitNewClass(NewClassTree node, Element p) {
if(isCancelled.get()) {
return null;
}
Trees trees = workingCopy.getTrees();
ClassTree classTree = ((NewClassTree) node).getClassBody();
if (classTree != null && p.getKind() == ElementKind.CONSTRUCTOR) {
Element anonClass = workingCopy.getTrees().getElement(TreePath.getPath(workingCopy.getCompilationUnit(), classTree));
if (anonClass == null) {
Logger.getLogger("org.netbeans.modules.refactoring.java").log(Level.SEVERE, "FindUsages cannot resolve {0}", classTree); // NOI18N
} else {
for (ExecutableElement c : ElementFilter.constructorsIn(anonClass.getEnclosedElements())) {
MethodTree t = workingCopy.getTrees().getTree(c);
TreePath superCall = trees.getPath(workingCopy.getCompilationUnit(), ((ExpressionStatementTree) t.getBody().getStatements().get(0)).getExpression());
Element superCallElement = trees.getElement(superCall);
if (superCallElement != null && superCallElement.equals(p) && !workingCopy.getTreeUtilities().isSynthetic(superCall)) {
addUsage(superCall);
}
}
}
} else {
addIfMatch(getCurrentPath(), node, p);
}
return super.visitNewClass(node, p);
}
示例5: assignmentToCatchBlockParameter
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToCatchBlockParameter", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToCatchBlockParameter", category = "assignment_issues", enabled = false, suppressWarnings = "AssignmentToCatchBlockParameter", options=Options.QUERY) //NOI18N
@TriggerTreeKind(Kind.CATCH)
public static List<ErrorDescription> assignmentToCatchBlockParameter(HintContext context) {
final Trees trees = context.getInfo().getTrees();
final TreePath catchPath = context.getPath();
final Element param = trees.getElement(TreePath.getPath(catchPath, ((CatchTree) catchPath.getLeaf()).getParameter()));
if (param == null || param.getKind() != ElementKind.EXCEPTION_PARAMETER) {
return null;
}
final TreePath block = TreePath.getPath(catchPath, ((CatchTree) catchPath.getLeaf()).getBlock());
final List<TreePath> paths = new LinkedList<TreePath>();
new AssignmentFinder(trees, param).scan(block, paths);
final List<ErrorDescription> ret = new ArrayList<ErrorDescription>(paths.size());
for (TreePath path : paths) {
ret.add(ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class, "MSG_AssignmentToCatchBlockParameter", param.getSimpleName()))); //NOI18N
}
return ret;
}
示例6: visitVariable
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Tree visitVariable(VariableTree variableDeclTree, Trees trees) {
//check for shadowed variable
if (isNameAlreadyUsed(variableDeclTree.getName())) {
TreePath path = getCurrentPath();
CharSequence newName = getUniqueName(variableDeclTree.getName());
Element el = trees.getElement(path);
if (el != null) {
originalToNewName.put(el, newName);
}
VariableTree newTree = copy.getTreeMaker()
.Variable(variableDeclTree.getModifiers(), newName, variableDeclTree.getType(), variableDeclTree.getInitializer());
copy.rewrite(variableDeclTree, newTree);
}
return super.visitVariable(variableDeclTree, trees);
}
示例7: getForwardReferences
import com.sun.source.util.Trees; //导入方法依赖的package包/类
/**
* Returns all elements of the given scope that are declared after given position in a source.
* @param path to the given search scope
* @param pos position in the source
* @param sourcePositions
* @param trees
* @return collection of forward references
*
* @since 0.136
*/
public static Collection<? extends Element> getForwardReferences(TreePath path, int pos, SourcePositions sourcePositions, Trees trees) {
HashSet<Element> refs = new HashSet<>();
Element el;
while(path != null) {
switch(path.getLeaf().getKind()) {
case VARIABLE:
el = trees.getElement(path);
if (el != null) {
refs.add(el);
}
TreePath parent = path.getParentPath();
if (TreeUtilities.CLASS_TREE_KINDS.contains(parent.getLeaf().getKind())) {
boolean isStatic = ((VariableTree)path.getLeaf()).getModifiers().getFlags().contains(Modifier.STATIC);
for(Tree member : ((ClassTree)parent.getLeaf()).getMembers()) {
if (member.getKind() == Tree.Kind.VARIABLE && sourcePositions.getStartPosition(path.getCompilationUnit(), member) >= pos &&
(isStatic || !((VariableTree)member).getModifiers().getFlags().contains(Modifier.STATIC))) {
el = trees.getElement(new TreePath(parent, member));
if (el != null) {
refs.add(el);
}
}
}
}
break;
case ENHANCED_FOR_LOOP:
EnhancedForLoopTree efl = (EnhancedForLoopTree)path.getLeaf();
if (sourcePositions.getEndPosition(path.getCompilationUnit(), efl.getExpression()) >= pos) {
el = trees.getElement(new TreePath(path, efl.getVariable()));
if (el != null) {
refs.add(el);
}
}
}
path = path.getParentPath();
}
return refs;
}
示例8: resolveAnonymousClassConstructor
import com.sun.source.util.Trees; //导入方法依赖的package包/类
/**
* special treatment for anonymous classes to resolve the proper constructor
* of extended class instead of the synthetic one.
* @see <a href="https://netbeans.org/bugzilla/show_bug.cgi?id=168775">#168775</a>
*/
private Element resolveAnonymousClassConstructor(Element el, NewClassTree tree, final Trees trees) {
if (el != null && tree.getClassBody() != null) {
Tree t = trees.getTree(el);
if (t != null && t.getKind() == Tree.Kind.METHOD) {
MethodTree constructorTree = (MethodTree) t;
Tree superCall = constructorTree.getBody().getStatements().get(0);
TreePath superCallPath = trees.getPath(
getCurrentPath().getCompilationUnit(),
((ExpressionStatementTree) superCall).getExpression());
el = trees.getElement(superCallPath);
}
}
return el;
}
示例9: updateSuperThisReferences
import com.sun.source.util.Trees; //导入方法依赖的package包/类
private BlockTree updateSuperThisReferences(BlockTree body, final TreePath mpath) {
final Map<ExpressionTree, ExpressionTree> original2Translated = new HashMap<ExpressionTree, ExpressionTree>();
final Trees trees = workingCopy.getTrees();
ErrorAwareTreeScanner<Boolean, Void> idScan = new ErrorAwareTreeScanner<Boolean, Void>() {
@Override
public Boolean visitMemberSelect(MemberSelectTree node, Void nothing) {
String isThis = node.getExpression().toString();
if (isThis.equals("super") || isThis.endsWith(".super")) { //NOI18N
TreePath currentPath = new TreePath(mpath, node);
Element el = trees.getElement(currentPath);
if (el != null && el.getEnclosingElement().equals(targetType)) {
original2Translated.put(node, make.Identifier(node.getIdentifier()));
return Boolean.TRUE;
}
}
return super.visitMemberSelect(node, nothing);
}
@Override
public Boolean reduce(Boolean r1, Boolean r2) {
return (r1 == Boolean.TRUE || r2 == Boolean.TRUE);
}
};
boolean update = idScan.scan(body, null) == Boolean.TRUE;
if (update) {
body = (BlockTree) workingCopy.getTreeUtilities().translate(body, original2Translated);
}
return body;
}
示例10: getTopLevelClassElement
import com.sun.source.util.Trees; //导入方法依赖的package包/类
public static TypeElement getTopLevelClassElement(CompilationController controller) {
ClassTree classTree = getTopLevelClassTree(controller);
if (classTree == null) {
return null;
}
Trees trees = controller.getTrees();
TreePath path = trees.getPath(controller.getCompilationUnit(), classTree);
return (TypeElement) trees.getElement(path);
}
示例11: findClass
import com.sun.source.util.Trees; //导入方法依赖的package包/类
private static TypeElement findClass(CompilationController ctrl, String className) {
CompilationUnitTree cunit = ctrl.getCompilationUnit();
for (Tree declTree : cunit.getTypeDecls()) {
ClassTree classTree = (ClassTree) declTree;
if (className.equals(classTree.getSimpleName().toString())) {
Trees trees = ctrl.getTrees();
TypeElement classElm = (TypeElement) trees.getElement(trees.getPath(cunit, classTree));
return classElm;
}
}
return null;
}
示例12: getEnclosingClass
import com.sun.source.util.Trees; //导入方法依赖的package包/类
private static TypeElement getEnclosingClass (TreePath path, final Trees trees) {
while (path != null && path.getLeaf().getKind() != Tree.Kind.COMPILATION_UNIT) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(path.getLeaf().getKind())) {
return (TypeElement) trees.getElement(path);
}
path = path.getParentPath();
}
return null;
}
示例13: visitClass
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Void visitClass(ClassTree t, Void v) {
if (variableElement == null) {
// try to find the component's field variable in the class
List<? extends Tree> members = (List<? extends Tree>) t.getMembers();
Iterator<? extends Tree> it = members.iterator();
while(it.hasNext()){
Tree tr = it.next();
if (tr.getKind() == Tree.Kind.VARIABLE) {
Trees trees = info.getTrees();
TreePath path = new TreePath(getCurrentPath(), tr);
Element el = trees.getElement(path);
if (el != null) { // Issue 185420
String sname = el.getSimpleName().toString();
if(sname.equals(this.member)){
this.handle = TreePathHandle.create(path, info);
variableElement = el;
if (findUsages) {
usagesPositions = new ArrayList<Integer>();
}
}
}
}
}
}
if (findUsages) {
super.visitClass(t, v);
}
return null;
}
示例14: visitVariable
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Tree visitVariable(VariableTree varTree, Element elementToMatch) {
TreePath treePath = getCurrentPath();
VariableElement varElement = (VariableElement) workingCopy.
getTrees().getElement(treePath);
//This check shouldn't be needed (ideally).
if (varElement == null) {
return super.visitVariable(varTree, elementToMatch);
}
TreePath parentPath = treePath.getParentPath();
if(parentPath != null && parentPath.getLeaf().getKind() == Tree.Kind.CATCH) {
// Do not change in catch statement
return super.visitVariable(varTree, elementToMatch);
}
Types types = workingCopy.getTypes();
TypeMirror varTypeErasure = types.erasure(varElement.asType());
TypeMirror elToMatchErasure = types.erasure(subTypeElement.asType());
if (types.isSameType(varTypeErasure, elToMatchErasure)) {
//Check for overloaded methods
boolean clashWithOverload = false;
if(parentPath != null && parentPath.getLeaf().getKind() == Tree.Kind.METHOD) {
Trees trees = workingCopy.getTrees();
ExecutableElement parent = (ExecutableElement) trees.getElement(parentPath);
TreePath enclosing = JavaRefactoringUtils.findEnclosingClass(workingCopy, parentPath, true, true, true, true, true);
TypeElement typeEl = (TypeElement) (enclosing == null? null : trees.getElement(enclosing));
if(parent != null && typeEl != null) {
Name simpleName = parent.getSimpleName();
int size = parent.getParameters().size();
OUTER: for (ExecutableElement method : ElementFilter.methodsIn(workingCopy.getElements().getAllMembers(typeEl))) {
if (method != parent &&
method.getKind() == parent.getKind() &&
size == method.getParameters().size() &&
simpleName.contentEquals(method.getSimpleName())) {
for (int i = 0; i < parent.getParameters().size(); i++) {
VariableElement par = parent.getParameters().get(i);
TypeMirror parErasure = types.erasure(par.asType());
TypeMirror par2Erasure = types.erasure(method.getParameters().get(i).asType());
if(!types.isSameType(parErasure, par2Erasure)) {
if(types.isAssignable(types.erasure(superTypeElement.asType()), par2Erasure)) {
clashWithOverload = true;
break OUTER;
}
if(types.isSubtype(parErasure, par2Erasure)) {
clashWithOverload = true;
break OUTER;
}
}
}
}
}
}
}
if (!clashWithOverload && isReplaceCandidate(varElement)) {
replaceWithSuperType(treePath, varTree, varElement, superTypeElement);
}
}
return super.visitVariable(varTree, elementToMatch);
}
示例15: asElement
import com.sun.source.util.Trees; //导入方法依赖的package包/类
private Element asElement(Tree tree) {
Trees treeUtil = workingCopy.getTrees();
TreePath treePath = treeUtil.getPath(workingCopy.getCompilationUnit(), tree);
Element element = treeUtil.getElement(treePath);
return element;
}