Search in sources :

Example 16 with AnnotationBinding

use of org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding in project bazel-jdt-java-toolchain by salesforce.

the class AnnotationDiscoveryVisitor method resolveAnnotations.

private void resolveAnnotations(BlockScope scope, Annotation[] annotations, Binding currentBinding) {
    int length = annotations == null ? 0 : annotations.length;
    if (length == 0)
        return;
    boolean old = scope.insideTypeAnnotation;
    scope.insideTypeAnnotation = true;
    currentBinding.getAnnotationTagBits();
    scope.insideTypeAnnotation = old;
    ElementImpl element = (ElementImpl) _factory.newElement(currentBinding);
    // discovery is never in terms of repeating annotation.
    AnnotationBinding[] annotationBindings = element.getPackedAnnotationBindings();
    for (AnnotationBinding binding : annotationBindings) {
        ReferenceBinding annotationType = binding.getAnnotationType();
        if (binding != null && Annotation.isAnnotationTargetAllowed(scope, annotationType, currentBinding)) {
            // binding should be resolved, but in case it's not, ignore it: it could have been wrapped into a container.
            TypeElement anno = (TypeElement) _factory.newElement(annotationType);
            _annoToElement.put(anno, element);
        }
    }
}
Also used : ElementImpl(org.eclipse.jdt.internal.compiler.apt.model.ElementImpl) AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) TypeElement(javax.lang.model.element.TypeElement) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)

Example 17 with AnnotationBinding

use of org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding 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)

Example 18 with AnnotationBinding

use of org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding in project bazel-jdt-java-toolchain by salesforce.

the class Factory method getPackedAnnotationBindings.

/* Wrap repeating annotations into their container, return an array of bindings.
	   Incoming array is not modified. The resulting array may be null but will not contain null
	   entries.
	*/
public static AnnotationBinding[] getPackedAnnotationBindings(AnnotationBinding[] annotations) {
    int length = annotations == null ? 0 : annotations.length;
    if (length == 0)
        return annotations;
    // only replicate if repackaging.
    AnnotationBinding[] repackagedBindings = annotations;
    for (int i = 0; i < length; i++) {
        AnnotationBinding annotation = repackagedBindings[i];
        if (annotation == null)
            continue;
        ReferenceBinding annotationType = annotation.getAnnotationType();
        if (!annotationType.isRepeatableAnnotationType())
            continue;
        ReferenceBinding containerType = annotationType.containerAnnotationType();
        if (containerType == null)
            // FUBAR.
            continue;
        MethodBinding[] values = containerType.getMethods(TypeConstants.VALUE);
        if (values == null || values.length != 1)
            // FUBAR.
            continue;
        MethodBinding value = values[0];
        if (value.returnType == null || value.returnType.dimensions() != 1 || TypeBinding.notEquals(value.returnType.leafComponentType(), annotationType))
            // FUBAR
            continue;
        // We have a kosher repeatable annotation with a kosher containing type. See if actually repeats.
        List<AnnotationBinding> containees = null;
        for (int j = i + 1; j < length; j++) {
            AnnotationBinding otherAnnotation = repackagedBindings[j];
            if (otherAnnotation == null)
                continue;
            if (otherAnnotation.getAnnotationType() == annotationType) {
                // $IDENTITY-COMPARISON$
                if (repackagedBindings == annotations)
                    System.arraycopy(repackagedBindings, 0, repackagedBindings = new AnnotationBinding[length], 0, length);
                // so it is not double packed.
                repackagedBindings[j] = null;
                if (containees == null) {
                    containees = new ArrayList<>();
                    containees.add(annotation);
                }
                containees.add(otherAnnotation);
            }
        }
        if (containees != null) {
            ElementValuePair[] elementValuePairs = new ElementValuePair[] { new ElementValuePair(TypeConstants.VALUE, containees.toArray(), value) };
            repackagedBindings[i] = new AnnotationBinding(containerType, elementValuePairs);
        }
    }
    int finalTally = 0;
    for (int i = 0; i < length; i++) {
        if (repackagedBindings[i] != null)
            finalTally++;
    }
    if (repackagedBindings == annotations && finalTally == length) {
        return annotations;
    }
    annotations = new AnnotationBinding[finalTally];
    for (int i = 0, j = 0; i < length; i++) {
        if (repackagedBindings[i] != null)
            annotations[j++] = repackagedBindings[i];
    }
    return annotations;
}
Also used : AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) ElementValuePair(org.eclipse.jdt.internal.compiler.lookup.ElementValuePair) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)

Example 19 with AnnotationBinding

use of org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding in project bazel-jdt-java-toolchain by salesforce.

the class Factory method getAnnotations.

// for cast of newProxyInstance() to A
@SuppressWarnings("unchecked")
private <A extends Annotation> A[] getAnnotations(AnnotationBinding[] annoInstances, Class<A> annotationClass, boolean justTheFirst) {
    if (annoInstances == null || annoInstances.length == 0 || annotationClass == null)
        return null;
    String annoTypeName = annotationClass.getName();
    if (annoTypeName == null)
        return null;
    List<A> list = new ArrayList<>(annoInstances.length);
    for (AnnotationBinding annoInstance : annoInstances) {
        if (annoInstance == null)
            continue;
        AnnotationMirrorImpl annoMirror = createAnnotationMirror(annoTypeName, annoInstance);
        if (annoMirror != null) {
            list.add((A) Proxy.newProxyInstance(annotationClass.getClassLoader(), new Class[] { annotationClass }, annoMirror));
            if (justTheFirst)
                break;
        }
    }
    A[] result = (A[]) Array.newInstance(annotationClass, list.size());
    return list.size() > 0 ? (A[]) list.toArray(result) : null;
}
Also used : AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) ArrayList(java.util.ArrayList)

Example 20 with AnnotationBinding

use of org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding in project bazel-jdt-java-toolchain by salesforce.

the class PackageElementImpl method getAnnotationBindings.

@Override
protected AnnotationBinding[] getAnnotationBindings() {
    PackageBinding packageBinding = (PackageBinding) this._binding;
    char[][] compoundName = CharOperation.arrayConcat(packageBinding.compoundName, TypeConstants.PACKAGE_INFO_NAME);
    ReferenceBinding type = packageBinding.environment.getType(compoundName);
    AnnotationBinding[] annotations = null;
    if (type != null && type.isValidBinding()) {
        annotations = type.getAnnotations();
    }
    return annotations;
}
Also used : AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)

Aggregations

AnnotationBinding (org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding)23 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)10 TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)9 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)8 FieldBinding (org.eclipse.jdt.internal.compiler.lookup.FieldBinding)6 ParameterizedTypeBinding (org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding)6 SourceTypeBinding (org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding)6 ArrayList (java.util.ArrayList)5 LookupEnvironment (org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment)5 ParameterizedGenericMethodBinding (org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding)3 ParameterizedMethodBinding (org.eclipse.jdt.internal.compiler.lookup.ParameterizedMethodBinding)3 ProblemMethodBinding (org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding)3 TypeVariableBinding (org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding)3 Field (java.lang.reflect.Field)2 TypeElement (javax.lang.model.element.TypeElement)2 ArrayBinding (org.eclipse.jdt.internal.compiler.lookup.ArrayBinding)2 Binding (org.eclipse.jdt.internal.compiler.lookup.Binding)2 BlockScope (org.eclipse.jdt.internal.compiler.lookup.BlockScope)2 ElementValuePair (org.eclipse.jdt.internal.compiler.lookup.ElementValuePair)2 LocalTypeBinding (org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding)2