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

The following examples show how to use javax.lang.model.element.ElementKind#PARAMETER . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
@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 Project: netbeans   File: FindLocalUsagesQuery.java    License: 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 Project: netbeans   File: IntroduceConstantFix.java    License: 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 4
@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 5
@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 6
Source Project: netbeans   File: FindLocalUsagesQuery.java    License: 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 7
Source Project: netbeans   File: PreconditionsChecker.java    License: 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 8
@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 Project: netbeans   File: AssignmentIssues.java    License: 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 10
Source Project: netbeans   File: ELHyperlinkProvider.java    License: 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 11
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 12
private static boolean isLocalVariableClosure(Element el) {
    return el.getKind() == ElementKind.PARAMETER ||
           LOCAL_VARIABLES.contains(el.getKind());
}
 
Example 13
Source Project: netbeans   File: PreconditionsChecker.java    License: 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 Project: j2objc   File: ElementUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isParameter(Element element) {
  return element.getKind() == ElementKind.PARAMETER;
}
 
Example 15
Source Project: OnActivityResult   File: Utils.java    License: 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 16
Source Project: netbeans   File: AddAnnotationArgument.java    License: 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 17
Source Project: j2objc   File: ElementUtil.java    License: 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 18
private boolean shouldIgnore(ElementKind kind) {
  return kind != ElementKind.CLASS && kind != ElementKind.METHOD && kind != ElementKind.PARAMETER;
}
 
Example 19
Source Project: turbine   File: TurbineElement.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ElementKind getKind() {
  return ElementKind.PARAMETER;
}
 
Example 20
Source Project: j2objc   File: GeneratedVariableElement.java    License: 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);
}