Java Code Examples for com.sun.source.tree.Tree.Kind#IDENTIFIER

The following examples show how to use com.sun.source.tree.Tree.Kind#IDENTIFIER . 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: OverridableMethodCallInConstructor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static boolean invocationOnThis(MethodInvocationTree mit) {
    Tree select = mit.getMethodSelect();
    
    switch (select.getKind()) {
        case IDENTIFIER:
            return true;
        case MEMBER_SELECT:
            if (((MemberSelectTree) select).getExpression().getKind() == Kind.IDENTIFIER) {
                IdentifierTree ident = (IdentifierTree) ((MemberSelectTree) select).getExpression();

                return ident.getName().contentEquals("this");
            }
    }

    return false;
}
 
Example 2
Source File: ThisInAnonymous.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@TriggerPattern(value="synchronized ($this) { $stmts$; }")
public static ErrorDescription hint(HintContext ctx) {
    TreePath thisVariable = ctx.getVariables().get("$this");
    if (thisVariable.getLeaf().getKind() != Kind.IDENTIFIER || !((IdentifierTree) thisVariable.getLeaf()).getName().contentEquals(THIS_KEYWORD)) {
        return null;
    }
    
    TreePath anonClassTP = getParentClass(ctx.getPath());
    Element annonClass = ctx.getInfo().getTrees().getElement(anonClassTP);
    String key = getKey(annonClass);
    if (key != null) {
        Element parent = ctx.getInfo().getTrees().getElement(getParentClass(anonClassTP.getParentPath()));

        if (parent == null || (!parent.getKind().isClass() && !parent.getKind().isInterface())) {
            return null;
        }
        
        Fix fix = new FixImpl(TreePathHandle.create(thisVariable, ctx.getInfo()),
                     ElementHandle.create((TypeElement) parent)).toEditorFix();

        String displayName = NbBundle.getMessage(ThisInAnonymous.class, key);
        return ErrorDescriptionFactory.forName(ctx, thisVariable, displayName, fix);
    }

    return null;
}
 
Example 3
Source File: GoToSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static TreePath adjustPathForModuleName(TreePath path) {
    TreePath tp = path;
    while (tp != null && (tp.getLeaf().getKind() == Kind.IDENTIFIER || tp.getLeaf().getKind() == Kind.MEMBER_SELECT)) {
        Tree parent = tp.getParentPath().getLeaf();
        if (parent.getKind() == Kind.MODULE && ((ModuleTree)parent).getName() == tp.getLeaf()) {
            return tp.getParentPath();
        }
        if (parent.getKind() == Kind.REQUIRES && ((RequiresTree)parent).getModuleName() == tp.getLeaf()
                || parent.getKind() == Kind.EXPORTS && ((ExportsTree)parent).getModuleNames() != null && ((ExportsTree)parent).getModuleNames().contains(tp.getLeaf())
                || parent.getKind() == Kind.OPENS && ((OpensTree)parent).getModuleNames() != null && ((OpensTree)parent).getModuleNames().contains(tp.getLeaf())) {
            return tp;
        }
        tp = tp.getParentPath();
    }
    return path;
}
 
Example 4
Source File: EqualsMethodHint.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    if (node.getArguments().isEmpty()) {
        if (node.getMethodSelect().getKind() == Kind.MEMBER_SELECT) {
            MemberSelectTree mst = (MemberSelectTree) node.getMethodSelect();
            Element e = info.getTrees().getElement(new TreePath(new TreePath(getCurrentPath(), mst), mst.getExpression()));

            if (parameter.equals(e) && mst.getIdentifier().contentEquals("getClass")) { // NOI18N
                throw new Found();
            }
        } else if (node.getMethodSelect().getKind() == Kind.IDENTIFIER) {
            IdentifierTree it = (IdentifierTree) node.getMethodSelect();

            if (it.getName().contentEquals("getClass")) { // NOI18N
                throw new Found();
            }
        }
    }
    
    return super.visitMethodInvocation(node, p);
}
 
Example 5
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static Token<JavaTokenId> findTokenWithText(CompilationInfo info, String text, int start, int end) {
    TokenHierarchy<?> th = info.getTokenHierarchy();
    TokenSequence<JavaTokenId> ts = th.tokenSequence(JavaTokenId.language()).subSequence(start, end);
    
    while (ts.moveNext()) {
        Token<JavaTokenId> t = ts.token();
        
        if (t.id() == JavaTokenId.IDENTIFIER) {
            boolean nameMatches;
            
            if (!(nameMatches = text.equals(info.getTreeUtilities().decodeIdentifier(t.text()).toString()))) {
                ExpressionTree expr = info.getTreeUtilities().parseExpression(t.text().toString(), new SourcePositions[1]);
                
                nameMatches = expr.getKind() == Kind.IDENTIFIER && text.contentEquals(((IdentifierTree) expr).getName());
            }
            
            if (nameMatches) {
                return t;
            }
        }
    }
    
    return null;
}
 
Example 6
Source File: ImmutableTreeTranslator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Visitor method: Translate a single node.
    *  return type is guaranteed to be the same as the input type
    */
   public <T extends Tree> T translateStable(T tree) {
Tree t2 = translate(tree);
if(t2!=null && t2.getClass()!=tree.getClass()) {
    if(t2.getClass()!=tree.getClass()) {
               //possibly import analysis rewrote QualIdentTree->IdentifierTree or QIT->MemberSelectTree:
               if (   tree.getClass() != QualIdentTree.class
                   || (t2.getKind() != Kind.IDENTIFIER) && (t2.getKind() != Kind.MEMBER_SELECT)) {
                   System.err.println("Rewrite stability problem: got "+t2.getClass()
                       +"\n\t\texpected "+tree.getClass());
                   return tree;
               }
    }
}
return (T)t2;
   }
 
Example 7
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isKeyword(Tree tree) {
    if (tree.getKind() == Kind.IDENTIFIER) {
        return keywords.contains(((IdentifierTree) tree).getName().toString());
    }
    if (tree.getKind() == Kind.MEMBER_SELECT) {
        return keywords.contains(((MemberSelectTree) tree).getIdentifier().toString());
    }
    
    return false;
}
 
Example 8
Source File: AddParameterOrLocalFix.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** In case statement is an Assignment, replace it with variable declaration */
private boolean initExpression(StatementTree statement, TreeMaker make, final String name, TypeMirror proposedType, final WorkingCopy wc, TreePath tp) {
    ExpressionTree exp = ((ExpressionStatementTree) statement).getExpression();
    if (exp.getKind() == Kind.ASSIGNMENT) {
        AssignmentTree at = (AssignmentTree) exp;
        if (at.getVariable().getKind() == Kind.IDENTIFIER && ((IdentifierTree) at.getVariable()).getName().contentEquals(name)) {
            //replace the expression statement with a variable declaration:
            VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.Type(proposedType), at.getExpression());
            vt = Utilities.copyComments(wc, statement, vt);
            wc.rewrite(statement, vt);
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: JavaPluginUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static boolean isSynthetic(CompilationInfo info, CompilationUnitTree cut, Tree leaf) throws NullPointerException {
    JCTree tree = (JCTree) leaf;
    
    if (tree.pos == (-1))
        return true;
    
    if (leaf.getKind() == Kind.METHOD) {
        //check for synthetic constructor:
        return (((JCTree.JCMethodDecl)leaf).mods.flags & Flags.GENERATEDCONSTR) != 0L;
    }
    
    //check for synthetic superconstructor call:
    if (leaf.getKind() == Kind.EXPRESSION_STATEMENT) {
        ExpressionStatementTree est = (ExpressionStatementTree) leaf;
        
        if (est.getExpression().getKind() == Kind.METHOD_INVOCATION) {
            MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
            
            if (mit.getMethodSelect().getKind() == Kind.IDENTIFIER) {
                IdentifierTree it = (IdentifierTree) mit.getMethodSelect();
                
                if ("super".equals(it.getName().toString())) {
                    SourcePositions sp = info.getTrees().getSourcePositions();
                    
                    return sp.getEndPosition(cut, leaf) == (-1);
                }
            }
        }
    }
    
    return false;
}
 
Example 10
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * @param tp tested {@link TreePath}
 * @return true if <code>tp</code> is an IDENTIFIER in a VARIABLE in an ENHANCED_FOR_LOOP
 */
public static boolean isEnhancedForLoopIdentifier(TreePath tp) {
    if (tp == null || tp.getLeaf().getKind() != Kind.IDENTIFIER)
        return false;
    TreePath parent = tp.getParentPath();
    if (parent == null || parent.getLeaf().getKind() != Kind.VARIABLE)
        return false;
    TreePath context = parent.getParentPath();
    if (context == null || context.getLeaf().getKind() != Kind.ENHANCED_FOR_LOOP)
        return false;
    return true;
}
 
Example 11
Source File: JavacMethodIntrospector.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
@Override
public Name visitMemberSelect(MemberSelectTree node, Void p) {
  // A member select is an "own method invocation" if the expression is "this",
  // under the condition that we only hit this case from visitMethodInvocation.
  ExpressionTree lhs = node.getExpression();
  if (lhs.getKind() != Kind.IDENTIFIER) {
    return null;
  }
  if (!((IdentifierTree) lhs).getName().contentEquals("this")) {
    return null;
  }
  return node.getIdentifier();
}
 
Example 12
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean isNonCtorKeyword(Tree tree) {
    if (tree.getKind() == Kind.IDENTIFIER) {
        return nonCtorKeywords.contains(((IdentifierTree) tree).getName().toString());
    }
    if (tree.getKind() == Kind.MEMBER_SELECT) {
        return nonCtorKeywords.contains(((MemberSelectTree) tree).getIdentifier().toString());
    }

    return false;
}
 
Example 13
Source File: SemanticHighlighterBase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void addParameterInlineHint(Tree tree) {
    TreePath pp = getCurrentPath().getParentPath();
    Tree leaf = pp.getLeaf();
    if (leaf != null &&
        (leaf.getKind() == Kind.METHOD_INVOCATION || leaf.getKind() == Kind.NEW_CLASS)) {
        int pos = -1;
        if (leaf.getKind() == Kind.METHOD_INVOCATION) {
            pos = MethodInvocationTree.class.cast(leaf).getArguments().indexOf(tree);
        } else if (leaf.getKind() == Kind.NEW_CLASS) {
            pos = NewClassTree.class.cast(leaf).getArguments().indexOf(tree);
        }
        if (pos != (-1)) {
            Element invoked = info.getTrees().getElement(pp);
            if (invoked != null && (invoked.getKind() == ElementKind.METHOD || invoked.getKind() == ElementKind.CONSTRUCTOR)) {
                long start = sourcePositions.getStartPosition(info.getCompilationUnit(), tree);
                long end = start + 1;
                ExecutableElement invokedMethod = (ExecutableElement) invoked;
                pos = Math.min(pos, invokedMethod.getParameters().size() - 1);
                if (pos != (-1)) {
                    boolean shouldBeAdded = true;
                    if (tree.getKind() == Kind.IDENTIFIER &&
                            invokedMethod.getParameters().get(pos).getSimpleName().equals(
                                    IdentifierTree.class.cast(tree).getName())) {
                        shouldBeAdded = false;
                    }
                    if (shouldBeAdded) {
                        preText.put(new int[] {(int) start, (int) end},
                                    invokedMethod.getParameters().get(pos).getSimpleName() + ":");
                    }
                }
            }
        }
    }
}
 
Example 14
Source File: TreeUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
boolean isSynthetic(CompilationUnitTree cut, Tree leaf) throws NullPointerException {
    JCTree tree = (JCTree) leaf;
    
    if (tree.pos == (-1))
        return true;
    
    if (leaf.getKind() == Kind.METHOD) {
        //check for synthetic constructor:
        return (((JCMethodDecl)leaf).mods.flags & Flags.GENERATEDCONSTR) != 0L;
    }
    
    //check for synthetic superconstructor call:
    if (leaf.getKind() == Kind.EXPRESSION_STATEMENT) {
        ExpressionStatementTree est = (ExpressionStatementTree) leaf;
        
        if (est.getExpression().getKind() == Kind.METHOD_INVOCATION) {
            MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
            
            if (mit.getMethodSelect().getKind() == Kind.IDENTIFIER) {
                IdentifierTree it = (IdentifierTree) mit.getMethodSelect();
                
                if ("super".equals(it.getName().toString())) {
                    SourcePositions sp = info.getTrees().getSourcePositions();
                    
                    return sp.getEndPosition(cut, leaf) == (-1);
                }
            }
        }
    }
    
    return false;
}
 
Example 15
Source File: WSCompletionProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void createAssignmentResults(CompilationController controller, 
        Env env) throws IOException 
{
    TreePath elementPath = env.getPath();
    TreePath parentPath = elementPath.getParentPath();
    Tree parent = parentPath.getLeaf();
    switch (parent.getKind()) {                
        case ANNOTATION : {
            ExpressionTree var = ((AssignmentTree)elementPath.getLeaf()).
                getVariable();
            if (var!= null && var.getKind() == Kind.IDENTIFIER) {
                Name name = ((IdentifierTree)var).getName();
                if (name.contentEquals("value"))  {//NOI18N
                    controller.toPhase(Phase.ELEMENTS_RESOLVED);
                    TypeElement webParamEl = controller.getElements().
                        getTypeElement("javax.xml.ws.BindingType"); //NOI18N
                    if (webParamEl==null) {
                        hasErrors = true;
                        return;
                    }
                    TypeMirror el = controller.getTrees().getTypeMirror(parentPath);
                    if (el==null || el.getKind() == TypeKind.ERROR) {
                        hasErrors = true;
                        return;
                    }
                    if ( controller.getTypes().isSameType(el,webParamEl.asType())) 
                    {
                        for (String mode : BINDING_TYPES) {
                            if (mode.startsWith(env.getPrefix())){ 
                                        results.add(WSCompletionItem.
                                                createEnumItem(mode, 
                                                        "String", env.getOffset())); //NOI18N
                            }
                        }
                    }
                }
            }
        } break; // ANNOTATION
    }
}
 
Example 16
Source File: CopyFinder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean sameKind(Tree t1, Tree t2) {
    Kind k1 = t1.getKind();
    Kind k2 = t2.getKind();

    if (k1 == k2) {
        return true;
    }

    if (isSingleStatemenBlockAndStatement(t1, t2) || isSingleStatemenBlockAndStatement(t2, t1)) {
        return true;
    }

    if (k2 == Kind.BLOCK && StatementTree.class.isAssignableFrom(k1.asInterface())) {
        BlockTree bt = (BlockTree) t2;

        if (bt.isStatic()) {
            return false;
        }

        switch (bt.getStatements().size()) {
            case 1:
                return true;
            case 2:
                return    isMultistatementWildcardTree(bt.getStatements().get(0))
                       || isMultistatementWildcardTree(bt.getStatements().get(1));
            case 3:
                return    isMultistatementWildcardTree(bt.getStatements().get(0))
                       || isMultistatementWildcardTree(bt.getStatements().get(2));
        }

        return false;
    }

    if (    (k1 != Kind.MEMBER_SELECT && k1 != Kind.IDENTIFIER)
         || (k2 != Kind.MEMBER_SELECT && k2 != Kind.IDENTIFIER)) {
        return false;
    }

    return isPureMemberSelect(t1, true) && isPureMemberSelect(t2, true);
}
 
Example 17
Source File: WSCompletionProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
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
    }
}
 
Example 18
Source File: JackpotTrees.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Kind getKind() {
    return Kind.IDENTIFIER;
}
 
Example 19
Source File: StaticAccess.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected static Fix computeFixes(CompilationInfo info, TreePath treePath, int[] bounds, int[] kind, String[] simpleName) {
    if (treePath.getLeaf().getKind() != Kind.MEMBER_SELECT) {
        return null;
    }
    MemberSelectTree mst = (MemberSelectTree)treePath.getLeaf();
    Tree expression = mst.getExpression();
    TreePath expr = new TreePath(treePath, expression);
    
    TypeMirror tm = info.getTrees().getTypeMirror(expr);
    if (!Utilities.isValidType(tm)) {
        return null;
    }
    Element el = info.getTypes().asElement(tm);
    if (el == null || (!el.getKind().isClass() && !el.getKind().isInterface())) {
        return null;
    }
    
    TypeElement type = (TypeElement)el;
    
    if (isError(type)) {
        return null;
    }
    
    Name idName = null;
    
    if (expression.getKind() == Kind.MEMBER_SELECT) {
        MemberSelectTree exprSelect = (MemberSelectTree)expression;
        idName = exprSelect.getIdentifier();
    }
    
    if (expression.getKind() == Kind.IDENTIFIER) {
        IdentifierTree idt = (IdentifierTree)expression;
        idName = idt.getName();
    }
    
    if (idName != null) {
        if (idName.equals(type.getSimpleName())) {
            return null;
        }
        if (idName.equals(type.getQualifiedName())) {
            return null;
        }
    }
    
    Element used = info.getTrees().getElement(treePath);
    
    if (used == null || !used.getModifiers().contains(Modifier.STATIC)) {
        return null;
    }
    
    if (isError(used)) {
        return null;
    }
    
    if (used.getKind().isField()) {
        kind[0] = 0;
    } else {
        if (used.getKind() == ElementKind.METHOD) {
            kind[0] = 1;
        } else {
            kind[0] = 2;
        }
    }
    
    simpleName[0] = used.getSimpleName().toString();
    
    return new FixImpl(info, expr, type).toEditorFix();
}
 
Example 20
Source File: NFABasedBulkSearch.java    From netbeans with Apache License 2.0 3 votes vote down vote up
private boolean multiModifiers(Tree t) {
    if (t.getKind() != Kind.MODIFIERS) return false;
    
    List<AnnotationTree> annotations = new ArrayList<AnnotationTree>(((ModifiersTree) t).getAnnotations());

    return !annotations.isEmpty() && annotations.get(0).getAnnotationType().getKind() == Kind.IDENTIFIER;
}