Search in sources :

Example 1 with TypeElementImpl

use of org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl in project bazel-jdt-java-toolchain by salesforce.

the class RoundEnvImpl method getElementsAnnotatedWith.

/**
 * From the set of root elements and their enclosed elements, return the subset that are annotated
 * with {@code a}.  If {@code a} is annotated with the {@link java.lang.annotation.Inherited}
 * annotation, include those elements that inherit the annotation from their superclasses.
 * Note that {@link java.lang.annotation.Inherited} only applies to classes (i.e. TypeElements).
 */
@Override
public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
    if (a.getKind() != ElementKind.ANNOTATION_TYPE) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("Argument must represent an annotation type");
    }
    Binding annoBinding = ((TypeElementImpl) a)._binding;
    if (0 != (annoBinding.getAnnotationTagBits() & TagBits.AnnotationInherited)) {
        Set<Element> annotatedElements = new HashSet<>(_annoToUnit.getValues(a));
        // For all other root elements that are TypeElements, and for their recursively enclosed
        // types, add each element if it has a superclass are annotated with 'a'
        ReferenceBinding annoTypeBinding = (ReferenceBinding) annoBinding;
        for (TypeElement element : ElementFilter.typesIn(getRootElements())) {
            ReferenceBinding typeBinding = (ReferenceBinding) ((TypeElementImpl) element)._binding;
            addAnnotatedElements(annoTypeBinding, typeBinding, annotatedElements);
        }
        return Collections.unmodifiableSet(annotatedElements);
    }
    return Collections.unmodifiableSet(_annoToUnit.getValues(a));
}
Also used : MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) TypeElementImpl(org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) HashSet(java.util.HashSet)

Example 2 with TypeElementImpl

use of org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl in project bazel-jdt-java-toolchain by salesforce.

the class BaseMessagerImpl method createProblem.

/**
 * Create a CategorizedProblem that can be reported to an ICompilerRequestor, etc.
 *
 * @param e the element against which to report the message.  If the element is not
 * in the set of elements being compiled in the current round, the reference context
 * and filename will be set to null.
 * @return
 */
public static AptProblem createProblem(Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) {
    ReferenceContext referenceContext = null;
    Annotation[] elementAnnotations = null;
    int startPosition = 0;
    int endPosition = 0;
    if (e != null) {
        switch(e.getKind()) {
            case MODULE:
                ModuleElementImpl moduleElementImpl = (ModuleElementImpl) e;
                Binding moduleBinding = moduleElementImpl._binding;
                if (moduleBinding instanceof SourceModuleBinding) {
                    SourceModuleBinding sourceModuleBinding = (SourceModuleBinding) moduleBinding;
                    CompilationUnitDeclaration unitDeclaration = (CompilationUnitDeclaration) sourceModuleBinding.scope.referenceContext();
                    referenceContext = unitDeclaration;
                    elementAnnotations = unitDeclaration.moduleDeclaration.annotations;
                    startPosition = unitDeclaration.moduleDeclaration.sourceStart;
                    endPosition = unitDeclaration.moduleDeclaration.sourceEnd;
                }
                break;
            case ANNOTATION_TYPE:
            case INTERFACE:
            case CLASS:
            case ENUM:
                TypeElementImpl typeElementImpl = (TypeElementImpl) e;
                Binding typeBinding = typeElementImpl._binding;
                if (typeBinding instanceof SourceTypeBinding) {
                    SourceTypeBinding sourceTypeBinding = (SourceTypeBinding) typeBinding;
                    TypeDeclaration typeDeclaration = (TypeDeclaration) sourceTypeBinding.scope.referenceContext();
                    referenceContext = typeDeclaration;
                    elementAnnotations = typeDeclaration.annotations;
                    startPosition = typeDeclaration.sourceStart;
                    endPosition = typeDeclaration.sourceEnd;
                }
                break;
            case PACKAGE:
                // nothing to do: there is no reference context for a package
                break;
            case CONSTRUCTOR:
            case METHOD:
                ExecutableElementImpl executableElementImpl = (ExecutableElementImpl) e;
                Binding binding = executableElementImpl._binding;
                if (binding instanceof MethodBinding) {
                    MethodBinding methodBinding = (MethodBinding) binding;
                    AbstractMethodDeclaration sourceMethod = methodBinding.sourceMethod();
                    if (sourceMethod != null) {
                        referenceContext = sourceMethod;
                        elementAnnotations = sourceMethod.annotations;
                        startPosition = sourceMethod.sourceStart;
                        endPosition = sourceMethod.sourceEnd;
                    }
                }
                break;
            case ENUM_CONSTANT:
                break;
            case EXCEPTION_PARAMETER:
                break;
            case FIELD:
            case PARAMETER:
                VariableElementImpl variableElementImpl = (VariableElementImpl) e;
                binding = variableElementImpl._binding;
                if (binding instanceof FieldBinding) {
                    FieldBinding fieldBinding = (FieldBinding) binding;
                    FieldDeclaration fieldDeclaration = fieldBinding.sourceField();
                    if (fieldDeclaration != null) {
                        ReferenceBinding declaringClass = fieldBinding.declaringClass;
                        if (declaringClass instanceof SourceTypeBinding) {
                            SourceTypeBinding sourceTypeBinding = (SourceTypeBinding) declaringClass;
                            TypeDeclaration typeDeclaration = (TypeDeclaration) sourceTypeBinding.scope.referenceContext();
                            referenceContext = typeDeclaration;
                        }
                        elementAnnotations = fieldDeclaration.annotations;
                        startPosition = fieldDeclaration.sourceStart;
                        endPosition = fieldDeclaration.sourceEnd;
                    }
                } else if (binding instanceof AptSourceLocalVariableBinding) {
                    AptSourceLocalVariableBinding parameterBinding = (AptSourceLocalVariableBinding) binding;
                    LocalDeclaration parameterDeclaration = parameterBinding.declaration;
                    if (parameterDeclaration != null) {
                        MethodBinding methodBinding = parameterBinding.methodBinding;
                        if (methodBinding != null) {
                            referenceContext = methodBinding.sourceMethod();
                        }
                        elementAnnotations = parameterDeclaration.annotations;
                        startPosition = parameterDeclaration.sourceStart;
                        endPosition = parameterDeclaration.sourceEnd;
                    }
                }
                break;
            case INSTANCE_INIT:
            case STATIC_INIT:
                break;
            case LOCAL_VARIABLE:
                break;
            case TYPE_PARAMETER:
            default:
                break;
        }
    }
    StringBuilder builder = new StringBuilder();
    if (msg != null) {
        builder.append(msg);
    }
    if (a != null && elementAnnotations != null) {
        AnnotationBinding annotationBinding = ((AnnotationMirrorImpl) a)._binding;
        Annotation annotation = findAnnotation(elementAnnotations, annotationBinding);
        if (annotation != null) {
            startPosition = annotation.sourceStart;
            endPosition = annotation.sourceEnd;
            if (v != null && v instanceof AnnotationMemberValue) {
                MethodBinding methodBinding = ((AnnotationMemberValue) v).getMethodBinding();
                MemberValuePair[] memberValuePairs = annotation.memberValuePairs();
                MemberValuePair memberValuePair = null;
                for (int i = 0; memberValuePair == null && i < memberValuePairs.length; i++) {
                    if (methodBinding == memberValuePairs[i].binding) {
                        memberValuePair = memberValuePairs[i];
                    }
                }
                if (memberValuePair != null) {
                    startPosition = memberValuePair.sourceStart;
                    endPosition = memberValuePair.sourceEnd;
                }
            }
        }
    }
    int lineNumber = 0;
    int columnNumber = 1;
    char[] fileName = null;
    if (referenceContext != null) {
        CompilationResult result = referenceContext.compilationResult();
        fileName = result.fileName;
        int[] lineEnds = null;
        lineNumber = startPosition >= 0 ? Util.getLineNumber(startPosition, lineEnds = result.getLineSeparatorPositions(), 0, lineEnds.length - 1) : 0;
        columnNumber = startPosition >= 0 ? Util.searchColumnNumber(result.getLineSeparatorPositions(), lineNumber, startPosition) : 0;
    }
    int severity;
    switch(kind) {
        case ERROR:
            severity = ProblemSeverities.Error;
            break;
        case NOTE:
        case OTHER:
            severity = ProblemSeverities.Info;
            break;
        default:
            severity = ProblemSeverities.Warning;
            break;
    }
    return new AptProblem(referenceContext, fileName, String.valueOf(builder), 0, NO_ARGUMENTS, severity, startPosition, endPosition, lineNumber, columnNumber);
}
Also used : ModuleElementImpl(org.eclipse.jdt.internal.compiler.apt.model.ModuleElementImpl) TypeElementImpl(org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration) AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) MemberValuePair(org.eclipse.jdt.internal.compiler.ast.MemberValuePair) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) AnnotationMemberValue(org.eclipse.jdt.internal.compiler.apt.model.AnnotationMemberValue) ExecutableElementImpl(org.eclipse.jdt.internal.compiler.apt.model.ExecutableElementImpl) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) AnnotationMirrorImpl(org.eclipse.jdt.internal.compiler.apt.model.AnnotationMirrorImpl) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) AptSourceLocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.AptSourceLocalVariableBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) SourceModuleBinding(org.eclipse.jdt.internal.compiler.lookup.SourceModuleBinding) LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) VariableElementImpl(org.eclipse.jdt.internal.compiler.apt.model.VariableElementImpl) AptSourceLocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.AptSourceLocalVariableBinding) ReferenceContext(org.eclipse.jdt.internal.compiler.impl.ReferenceContext) FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) CompilationResult(org.eclipse.jdt.internal.compiler.CompilationResult) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) SourceModuleBinding(org.eclipse.jdt.internal.compiler.lookup.SourceModuleBinding)

Aggregations

TypeElementImpl (org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl)2 AnnotationBinding (org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding)2 Binding (org.eclipse.jdt.internal.compiler.lookup.Binding)2 FieldBinding (org.eclipse.jdt.internal.compiler.lookup.FieldBinding)2 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)2 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)2 SourceTypeBinding (org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding)2 HashSet (java.util.HashSet)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 AnnotationMemberValue (org.eclipse.jdt.internal.compiler.apt.model.AnnotationMemberValue)1 AnnotationMirrorImpl (org.eclipse.jdt.internal.compiler.apt.model.AnnotationMirrorImpl)1 ExecutableElementImpl (org.eclipse.jdt.internal.compiler.apt.model.ExecutableElementImpl)1 ModuleElementImpl (org.eclipse.jdt.internal.compiler.apt.model.ModuleElementImpl)1 VariableElementImpl (org.eclipse.jdt.internal.compiler.apt.model.VariableElementImpl)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)1 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)1