Java Code Examples for com.sun.source.tree.Scope#getEnclosingClass()

The following examples show how to use com.sun.source.tree.Scope#getEnclosingClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CopyFinder.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Iterable<? extends TreePath> fullPrepareThis(TreePath tp) {
    //XXX: is there a faster way to do this?
    Collection<TreePath> result = new LinkedList<TreePath>();
    Scope scope = info.getTrees().getScope(tp);
    TypeElement lastClass = null;

    while (scope != null && scope.getEnclosingClass() != null) {
        if (lastClass != scope.getEnclosingClass()) {
            ExpressionTree thisTree = info.getTreeUtilities().parseExpression("this", new SourcePositions[1]);

            info.getTreeUtilities().attributeTree(thisTree, scope);

            result.add(new TreePath(tp, thisTree));
        }
        
        scope = scope.getEnclosingScope();
    }

    return result;
}
 
Example 2
Source File: ElementUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**Return variables declared in the given scope.
 */
public Iterable<? extends Element> getLocalVars(Scope scope, ElementAcceptor acceptor) {
    ArrayList<Element> members = new ArrayList<Element>();
    Elements elements = JavacElements.instance(ctx);
    Types types = JavacTypes.instance(ctx);
    while(scope != null && scope.getEnclosingClass() != null) {
        for (Element local : scope.getLocalElements()) {
            if (acceptor == null || acceptor.accept(local, null)) {
                if (!isHidden(local, members, elements, types)) {
                    members.add(local);
                }
            }
        }
        scope = scope.getEnclosingScope();
    }
    return members;
}
 
Example 3
Source File: EditorContextSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static MethodArgument[] computeMethodArguments(CompilationController ci,
                                                      int methodLineNumber,
                                                      int offset,
                                                      ASTOperationCreationDelegate opCreationDelegate)
                                                        throws IOException {
    MethodArgument args[];
    if (!PreferredCCParser.toPhase(ci, JavaSource.Phase.RESOLVED, LOG)) {
        return null;
    }
    Scope scope = ci.getTreeUtilities().scopeFor(offset);
    Element clazz = scope.getEnclosingClass();
    if (clazz == null) {
        return null;
    }
    Tree methodTree = ci.getTrees().getTree(clazz);
    CompilationUnitTree cu = ci.getCompilationUnit();
    MethodArgumentsScanner scanner =
            new MethodArgumentsScanner(methodLineNumber, cu, ci.getTrees().getSourcePositions(), false,
                                       opCreationDelegate);
    args = methodTree.accept(scanner, null);
    args = scanner.getArguments();
    return args;
}
 
Example 4
Source File: FieldValidator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void run(CompilationController parameter) throws Exception {
    parameter.toPhase(JavaSource.Phase.RESOLVED);
    this.cinfo = parameter;
    if (targetHandle == null || srcHandle == null) {
        return;
    }
    TreePath srcPath = srcHandle.resolve(cinfo);
    if (srcPath == null) {
        return;
    }
    TreePath targetPath = targetHandle.resolve(cinfo);
    if (targetPath == null) {
        return;
    }
    initialScope = cinfo.getTrees().getScope(srcPath);
    Scope targetScope = cinfo.getTrees().getScope(targetPath);
    Map<? extends Element, Scope> visibleVariables = 
            cinfo.getElementUtilities().findElementsAndOrigins(initialScope, this);
    lastResult = null;
    Element target = cinfo.getTrees().getElement(targetPath);
    for (Element e : visibleVariables.keySet()) {
        switch (e.getKind()) {
            case FIELD: case ENUM_CONSTANT: case PARAMETER:
                Scope def = visibleVariables.get(e);
                if (def != targetScope) {
                    for (Scope s = def; s.getEnclosingClass() != null; s = s.getEnclosingScope()) {
                        if (s == target) {
                            lastResult = new MemberSearchResult(ElementHandle.create(e));
                            return;
                        }
                    }
                }
                TypeElement owner = def.getEnclosingClass();
                if (owner == null) {
                    // static import
                    lastResult = new MemberSearchResult(ElementHandle.create(e),
                        ElementHandle.create((TypeElement)e.getEnclosingElement()));
                } else if (owner == e.getEnclosingElement()) {
                    if (owner == target) {
                        lastResult = new MemberSearchResult(ElementHandle.create(e));
                        return;
                    } else {
                        lastResult = new MemberSearchResult(ElementHandle.create(e),
                            ElementHandle.create(owner));
                    }
                } else {
                    // special case - hiding superclass field
                    lastResult = new MemberSearchResult(ElementHandle.create(e),
                        ElementHandle.create(owner), null);
                }
                break;
            case EXCEPTION_PARAMETER:
            case LOCAL_VARIABLE: 
            case RESOURCE_VARIABLE: {
                TreePath locPath = findLocalPathWorkaround(cinfo, e, srcPath);
                if (locPath == null) {
                    lastResult = new MemberSearchResult(e.getKind());
                } else {
                    lastResult = new MemberSearchResult(TreePathHandle.create(locPath, cinfo), e.getKind());
                }
            }
                return;
            default:
                // another namespace
                return;
        }
    }
}