Search in sources :

Example 1 with AnnotationMirrorImpl

use of org.eclipse.jdt.internal.compiler.apt.model.AnnotationMirrorImpl 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

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 TypeElementImpl (org.eclipse.jdt.internal.compiler.apt.model.TypeElementImpl)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 LocalDeclaration (org.eclipse.jdt.internal.compiler.ast.LocalDeclaration)1 MemberValuePair (org.eclipse.jdt.internal.compiler.ast.MemberValuePair)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1 ReferenceContext (org.eclipse.jdt.internal.compiler.impl.ReferenceContext)1 AnnotationBinding (org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding)1 AptSourceLocalVariableBinding (org.eclipse.jdt.internal.compiler.lookup.AptSourceLocalVariableBinding)1 Binding (org.eclipse.jdt.internal.compiler.lookup.Binding)1 FieldBinding (org.eclipse.jdt.internal.compiler.lookup.FieldBinding)1 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)1