Java Code Examples for javax.lang.model.element.ElementKind#PARAMETER

The following examples show how to use javax.lang.model.element.ElementKind#PARAMETER . 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: Symbol.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
    long flags = flags();
    if ((flags & PARAMETER) != 0) {
        if (isExceptionParameter())
            return ElementKind.EXCEPTION_PARAMETER;
        else
            return ElementKind.PARAMETER;
    } else if ((flags & ENUM) != 0) {
        return ElementKind.ENUM_CONSTANT;
    } else if (owner.kind == TYP || owner.kind == ERR) {
        return ElementKind.FIELD;
    } else if (isResourceVariable()) {
        return ElementKind.RESOURCE_VARIABLE;
    } else {
        return ElementKind.LOCAL_VARIABLE;
    }
}
 
Example 2
Source File: FindLocalUsagesQuery.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitCompilationUnit(CompilationUnitTree node, Void p) {
    if (!searchComment) {
        return super.visitCompilationUnit(node, p);
    }

    if (toFind.getKind() == ElementKind.PARAMETER) {
        renameParameterInMethodComments(toFind);
    } else {
        String originalName = toFind.getSimpleName().toString();
        if (originalName!=null) {
            TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language());

            while (ts.moveNext()) {
                Token<JavaTokenId> t = ts.token();
                
                if (isComment(t)) {
                    findAllInComment(t.text().toString(), ts.offset(), originalName);
                }
            }
        }
    }
    return super.visitCompilationUnit(node, p);
}
 
Example 3
Source File: Symbol.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
    long flags = flags();
    if ((flags & PARAMETER) != 0) {
        if (isExceptionParameter())
            return ElementKind.EXCEPTION_PARAMETER;
        else
            return ElementKind.PARAMETER;
    } else if ((flags & ENUM) != 0) {
        return ElementKind.ENUM_CONSTANT;
    } else if (owner.kind == TYP || owner.kind == ERR) {
        return ElementKind.FIELD;
    } else if (isResourceVariable()) {
        return ElementKind.RESOURCE_VARIABLE;
    } else {
        return ElementKind.LOCAL_VARIABLE;
    }
}
 
Example 4
Source File: IntroduceConstantFix.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static boolean checkConstantExpression(final CompilationInfo info, TreePath path) {
    InstanceRefFinder finder = new InstanceRefFinder(info, path) {
        @Override
        public Object visitIdentifier(IdentifierTree node, Object p) {
            Element el = info.getTrees().getElement(getCurrentPath());
            if (el == null || el.asType() == null || el.asType().getKind() == TypeKind.ERROR) {
                return null;
            }
            if (el.getKind() == ElementKind.LOCAL_VARIABLE || el.getKind() == ElementKind.PARAMETER) {
                throw new StopProcessing();
            } else if (el.getKind() == ElementKind.FIELD) {
                if (!el.getModifiers().contains(Modifier.FINAL)) {
                    throw new StopProcessing();
                }
            }
            return super.visitIdentifier(node, p);
        }
    };
    try {
        finder.process();
        return  !(finder.containsInstanceReferences() || finder.containsLocalReferences() || finder.containsReferencesToSuper());
    } catch (StopProcessing e) {
        return false;
    }
}
 
Example 5
Source File: AssignmentIssues.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToMethodParam", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToMethodParam", category = "assignment_issues", enabled = false, suppressWarnings = "AssignmentToMethodParameter", options=Options.QUERY) //NOI18N
@TriggerTreeKind({Kind.ASSIGNMENT, Kind.AND_ASSIGNMENT, Kind.DIVIDE_ASSIGNMENT,
    Kind.LEFT_SHIFT_ASSIGNMENT, Kind.MINUS_ASSIGNMENT, Kind.MULTIPLY_ASSIGNMENT,
    Kind.OR_ASSIGNMENT, Kind.PLUS_ASSIGNMENT, Kind.REMAINDER_ASSIGNMENT, Kind.RIGHT_SHIFT_ASSIGNMENT,
    Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT, Kind.XOR_ASSIGNMENT, Kind.PREFIX_INCREMENT,
    Kind.PREFIX_DECREMENT, Kind.POSTFIX_INCREMENT, Kind.POSTFIX_DECREMENT})
public static ErrorDescription assignmentToMethodParam(HintContext context) {
    final TreePath path = context.getPath();
    Element element = null;
    switch (path.getLeaf().getKind()) {
        case ASSIGNMENT:
            element = context.getInfo().getTrees().getElement(TreePath.getPath(path, ((AssignmentTree) path.getLeaf()).getVariable()));
            break;
        case PREFIX_INCREMENT:
        case PREFIX_DECREMENT:
        case POSTFIX_INCREMENT:
        case POSTFIX_DECREMENT:
            element = context.getInfo().getTrees().getElement(TreePath.getPath(path, ((UnaryTree) path.getLeaf()).getExpression()));
            break;
        default:
            element = context.getInfo().getTrees().getElement(TreePath.getPath(path, ((CompoundAssignmentTree) path.getLeaf()).getVariable()));
    }
    if (element != null && element.getKind() == ElementKind.PARAMETER) {
        return ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class, "MSG_AssignmentToMethodParam", element.getSimpleName())); //NOI18N
    }
    return null;
}
 
Example 6
Source File: DeepLinkInjectProcessor.java    From OkDeepLink with Apache License 2.0 5 votes vote down vote up
private List<Element> generateQueryElements(RoundEnvironment roundEnv) {
    Set<? extends Element> deepLinkPathElements = roundEnv.getElementsAnnotatedWith(Query.class);
    List<Element> queryElements = new ArrayList<>();
    for (Element element : deepLinkPathElements) {
        Query deepLinkPathAnnotation = element.getAnnotation(Query.class);
        ElementKind kind = element.getKind();
        if (kind != ElementKind.PARAMETER && kind != ElementKind.FIELD) {
            logger.error("Only classes and methods can be annotated with @" + Query.class.getCanonicalName(), element);
        }
        String queryKey = deepLinkPathAnnotation.value();
        if (queryKey == null || queryKey.length() == 0) {
            logger.error("The inject query cannot be null @" + Query.class.getCanonicalName(), element);
        }

        if (kind == ElementKind.FIELD) {
            Element enclosingElement = element.getEnclosingElement();
            if (enclosingElement.getKind() != CLASS) {
                logger.error("@" + Query.class.getCanonicalName() + "only be contained in classes", element);
            }
            TypeElement typeElement = (TypeElement) enclosingElement;
            boolean support = isSupportInject(typeElement);
            if (!support) {
                logger.error("@" + Query.class.getCanonicalName() + "only support inject in activity or fragment", element);
            }
            if (element.getModifiers().contains(Modifier.PRIVATE)) {
                logger.error("The inject query fields can not be private, please check field @" + Query.class.getCanonicalName() + "in class" + typeElement.getQualifiedName(), element);
            }
            queryElements.add(element);
        }
    }
    return queryElements;
}
 
Example 7
Source File: ELHyperlinkProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitVariable(VariableElement e, Boolean highlightName) {
    modifier(e.getModifiers());

    result.append(getTypeName(info, e.asType(), true));

    result.append(' ');

    boldStartCheck(highlightName);

    result.append(e.getSimpleName());

    boldStopCheck(highlightName);

    if (highlightName) {
        if (e.getConstantValue() != null) {
            result.append(" = ");
            result.append(e.getConstantValue().toString());
        }

        Element enclosing = e.getEnclosingElement();

        if (e.getKind() != ElementKind.PARAMETER && e.getKind() != ElementKind.LOCAL_VARIABLE && e.getKind() != ElementKind.EXCEPTION_PARAMETER) {
            result.append(" in ");

            //short typename:
            result.append(getTypeName(info, enclosing.asType(), true));
        }
    }

    return null;
}
 
Example 8
Source File: ComparatorParameterNotUsed.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitIdentifier(IdentifierTree node, Object p) {
    Element el = ci.getTrees().getElement(getCurrentPath());
    if (el != null && el.getKind() == ElementKind.PARAMETER) {
        unusedVars.remove(el);
        if (unusedVars.isEmpty()) {
            throw new StopProcessing();
        }
    }
    return super.visitIdentifier(node, p);
}
 
Example 9
Source File: PreconditionsChecker.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean isLocalVariable(IdentifierTree id, Trees trees) {
    Element el = trees.getElement(TreePath.getPath(treePath, id));
    if (el != null) {
        return el.getKind() == ElementKind.LOCAL_VARIABLE || el.getKind() == ElementKind.PARAMETER;
    }
    return false;
}
 
Example 10
Source File: FindLocalUsagesQuery.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public DocTree visitText(TextTree node, Element p) {
    if(searchComment) {
        DocTrees trees = info.getDocTrees();
        DocSourcePositions sourcePositions = trees.getSourcePositions();
        DocTreePath currentDocPath = getCurrentPath();
        if(toFind.getKind() == ElementKind.PARAMETER) {
            VariableElement var = (VariableElement) toFind;
            Element method = trees.getElement(currentDocPath);
            if(!var.getEnclosingElement().equals(method)) {
                return super.visitText(node, p);
            }
        }
        String text = node.getBody();
        String name = toFind.getSimpleName().toString();
        if(text.contains(name)) {
            int start = (int) sourcePositions.getStartPosition(info.getCompilationUnit(), currentDocPath.getDocComment(), node);
            int length = name.length();
            int offset = -1;
            do {
                offset = text.indexOf(name, ++offset);
                if(offset != -1) {
                    try {
                        MutablePositionRegion region = createRegion(doc, start + offset, start + offset + length);
                        comments.add(region);
                    } catch(BadLocationException ex) {
                        Exceptions.printStackTrace(ex);
                    }
                }
            } while (offset != -1);
        }
    }
    return super.visitText(node, p);
}
 
Example 11
Source File: VariableElementImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public ElementKind getKind() {
	if (_binding instanceof FieldBinding) {
		if (((FieldBinding)_binding).declaringClass.isEnum()) {
			return ElementKind.ENUM_CONSTANT;
		}
		else {
			return ElementKind.FIELD;
		}
	}
	else {
		return ElementKind.PARAMETER;
	}
}
 
Example 12
Source File: ElementUtil.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public static boolean isParameter(Element element) {
  return element.getKind() == ElementKind.PARAMETER;
}
 
Example 13
Source File: PreconditionsChecker.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean isLocalVariable(Element el) {
    return el.getKind() == ElementKind.LOCAL_VARIABLE || el.getKind() == ElementKind.PARAMETER;
}
 
Example 14
Source File: Utils.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
static boolean isParameter(final Element element) {
    return element.getKind() == ElementKind.PARAMETER && element instanceof VariableElement;
}
 
Example 15
Source File: AddAnnotationArgument.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public ChangeInfo implement(){
    CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>(){
        public void cancel() {}
        
        public void run(WorkingCopy workingCopy) throws Exception {
            Element annotationElement  = annMirror.getAnnotationType().asElement();
            if ( annotationElement == null ){
                return;
            }
            if (element.getKind() == ElementKind.PARAMETER){
                Element method = element.getEnclosingElement();
                if ( method instanceof ExecutableElement ){
                    ExecutableElement methodElement = (ExecutableElement)method;
                    List<? extends VariableElement> parameters = methodElement.getParameters();
                    int index = parameters.indexOf( element );
                    if ( index == -1 ){
                        return;
                    }
                    Utilities.addAnnotationArgument(workingCopy, 
                            ElementHandle.create(methodElement), index,
                            ElementHandle.create(annotationElement), argumentName,
                            argumentValue);
                }
                else {
                    return;
                }
            }
            else {
                Utilities.addAnnotationArgument(workingCopy, 
                        ElementHandle.create(element), 
                        ElementHandle.create(annotationElement), argumentName,
                        argumentValue);
            }
        }
    };
    
    JavaSource javaSource = JavaSource.forFileObject(fileObject);
    
    try{
        if ( javaSource!= null){
            javaSource.runModificationTask(task).commit();
        }
    } catch (IOException e){
    }
    return null;
}
 
Example 16
Source File: ElementUtil.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public static boolean isVariable(Element element) {
  ElementKind kind = element.getKind();
  return kind == ElementKind.FIELD || kind == ElementKind.LOCAL_VARIABLE
      || kind == ElementKind.PARAMETER || kind == ElementKind.EXCEPTION_PARAMETER
      || kind == ElementKind.RESOURCE_VARIABLE || kind == ElementKind.ENUM_CONSTANT;
}
 
Example 17
Source File: AbstractRestAnnotationProcessor.java    From RADL with Apache License 2.0 4 votes vote down vote up
private boolean shouldIgnore(ElementKind kind) {
  return kind != ElementKind.CLASS && kind != ElementKind.METHOD && kind != ElementKind.PARAMETER;
}
 
Example 18
Source File: TurbineElement.java    From turbine with Apache License 2.0 4 votes vote down vote up
@Override
public ElementKind getKind() {
  return ElementKind.PARAMETER;
}
 
Example 19
Source File: GeneratedVariableElement.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public static GeneratedVariableElement newParameter(
    String name, TypeMirror type, Element enclosingElement) {
  return new GeneratedVariableElement(name, type, ElementKind.PARAMETER, enclosingElement, true);
}
 
Example 20
Source File: SemanticHighlighterBase.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isLocalVariableClosure(Element el) {
    return el.getKind() == ElementKind.PARAMETER ||
           LOCAL_VARIABLES.contains(el.getKind());
}