Search in sources :

Example 1 with ModuleBinding

use of org.eclipse.jdt.internal.compiler.lookup.ModuleBinding in project spoon by INRIA.

the class ReferenceBuilder method getDeclaringReferenceFromImports.

/**
 * Try to get the declaring reference (package or type) from imports of the current
 * compilation unit declaration (current class). This method returns a CtReference
 * which can be a CtTypeReference if it retrieves the information in an static import,
 * a CtPackageReference if it retrieves the information in an standard import, otherwise
 * it returns null.
 *
 * @param expectedName Name expected in imports.
 * @return CtReference which can be a CtTypeReference, a CtPackageReference or null.
 */
CtReference getDeclaringReferenceFromImports(char[] expectedName) {
    CompilationUnitDeclaration cuDeclaration = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration;
    if (cuDeclaration == null) {
        return null;
    }
    LookupEnvironment environment = cuDeclaration.scope.environment;
    if (cuDeclaration.imports != null) {
        for (ImportReference anImport : cuDeclaration.imports) {
            if (CharOperation.equals(anImport.getImportName()[anImport.getImportName().length - 1], expectedName)) {
                if (anImport.isStatic()) {
                    int indexDeclaring = 2;
                    if ((anImport.bits & ASTNode.OnDemand) != 0) {
                        // With .*
                        indexDeclaring = 1;
                    }
                    char[][] packageName = CharOperation.subarray(anImport.getImportName(), 0, anImport.getImportName().length - indexDeclaring);
                    char[][] className = CharOperation.subarray(anImport.getImportName(), anImport.getImportName().length - indexDeclaring, anImport.getImportName().length - (indexDeclaring - 1));
                    PackageBinding aPackage;
                    try {
                        if (packageName.length != 0) {
                            aPackage = environment.createPackage(packageName);
                        } else {
                            aPackage = null;
                        }
                        final MissingTypeBinding declaringType = environment.createMissingType(aPackage, className);
                        this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports = true;
                        final CtTypeReference<Object> typeReference = getTypeReference(declaringType);
                        this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports = false;
                        return typeReference;
                    } catch (NullPointerException e) {
                        return null;
                    }
                } else {
                    PackageBinding packageBinding = null;
                    char[][] chars = CharOperation.subarray(anImport.getImportName(), 0, anImport.getImportName().length - 1);
                    // ArrayIndexOutOfBoundsException if `chars.length == 0`. Fixes #759.
                    if (chars.length > 0) {
                        Binding someBinding = cuDeclaration.scope.findImport(chars, false, false);
                        if (someBinding != null && someBinding.isValidBinding() && someBinding instanceof PackageBinding) {
                            packageBinding = (PackageBinding) someBinding;
                        } else {
                            try {
                                packageBinding = environment.createPackage(chars);
                            } catch (NullPointerException e) {
                                packageBinding = null;
                            }
                        }
                    }
                    if (packageBinding == null || packageBinding instanceof ProblemPackageBinding) {
                        // Big crisis here. We are already in noclasspath mode but JDT doesn't support always
                        // creation of a package in this mode. So, if we are in this brace, we make the job of JDT...
                        packageBinding = new PackageBinding(chars, null, environment, environment.module) {

                            // PackageBinding was a class instead of abstract class in earlier jdt versions.
                            // To circumvent this change to an abstract class, an anonymous class is used here.
                            @Override
                            public PlainPackageBinding getIncarnation(ModuleBinding arg0) {
                                // https://github.com/eclipse/eclipse.jdt.core/blob/master/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/PlainPackageBinding.java#L43
                                return null;
                            }
                        };
                    }
                    return getPackageReference(packageBinding);
                }
            }
        }
    }
    return null;
}
Also used : BinaryTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding) LocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding) FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) ProblemBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemBinding) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) CatchParameterBinding(org.eclipse.jdt.internal.compiler.lookup.CatchParameterBinding) ProblemPackageBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding) VoidTypeBinding(org.eclipse.jdt.internal.compiler.lookup.VoidTypeBinding) ArrayBinding(org.eclipse.jdt.internal.compiler.lookup.ArrayBinding) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) WildcardBinding(org.eclipse.jdt.internal.compiler.lookup.WildcardBinding) CaptureBinding(org.eclipse.jdt.internal.compiler.lookup.CaptureBinding) Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) PlainPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding) JDTTreeBuilderQuery.searchTypeBinding(spoon.support.compiler.jdt.JDTTreeBuilderQuery.searchTypeBinding) TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) PolyTypeBinding(org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) LocalTypeBinding(org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) BaseTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding) RawTypeBinding(org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding) ProblemReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding) PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding) ImportReference(org.eclipse.jdt.internal.compiler.ast.ImportReference) ProblemPackageBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding) PlainPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding) PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) ProblemPackageBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding) PlainPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding) LookupEnvironment(org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)

Example 2 with ModuleBinding

use of org.eclipse.jdt.internal.compiler.lookup.ModuleBinding in project spoon by INRIA.

the class JDTTreeBuilder method visit.

@Override
public boolean visit(CompilationUnitDeclaration compilationUnitDeclaration, CompilationUnitScope scope) {
    context.compilationunitdeclaration = scope.referenceContext;
    context.compilationUnitSpoon = getOrCreateCompilationUnit(context.compilationunitdeclaration, getFactory());
    ModuleBinding enclosingModule = scope.fPackage.enclosingModule;
    CtModule module;
    if (!enclosingModule.isUnnamed() && enclosingModule.shortReadableName() != null && enclosingModule.shortReadableName().length > 0) {
        module = getFactory().Module().getOrCreate(String.valueOf(enclosingModule.shortReadableName()));
    } else {
        module = getFactory().Module().getUnnamedModule();
    }
    context.compilationUnitSpoon.setDeclaredPackage(getFactory().Package().getOrCreate(CharOperation.toString(scope.currentPackageName), module));
    CtPackageDeclaration packageDeclaration = context.compilationUnitSpoon.getPackageDeclaration();
    if (packageDeclaration != null) {
        ImportReference packageRef = compilationUnitDeclaration.currentPackage;
        if (packageRef != null) {
            char[] content = context.getCompilationUnitContents();
            int declStart = packageRef.declarationSourceStart;
            // look for first comment
            int firstComment = PositionBuilder.findNextNonWhitespace(false, content, packageRef.sourceStart(), 0);
            if (firstComment < packageRef.sourceStart() && content[firstComment] == '/' && content[firstComment + 1] == '*') {
                // there is a `/*` or `/**`comment before package reference;
                // such comment is understood as compilation unit comment
                // all next comments belong to package declaration
                int commentEnd = PositionBuilder.getEndOfComment(content, packageRef.sourceStart(), firstComment);
                declStart = PositionBuilder.findNextNonWhitespace(false, content, packageRef.sourceStart(), commentEnd + 1);
            } else {
                declStart = firstComment;
            }
            packageDeclaration.setPosition(factory.Core().createCompoundSourcePosition(context.compilationUnitSpoon, packageRef.sourceStart(), packageRef.sourceEnd(), declStart, packageRef.declarationEnd, context.compilationUnitSpoon.getLineSeparatorPositions()));
            packageDeclaration.getReference().setPosition(factory.Core().createSourcePosition(context.compilationUnitSpoon, packageRef.sourceStart(), packageRef.sourceEnd(), context.compilationUnitSpoon.getLineSeparatorPositions()));
        }
    }
    return true;
}
Also used : ImportReference(org.eclipse.jdt.internal.compiler.ast.ImportReference) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) CtPackageDeclaration(spoon.reflect.declaration.CtPackageDeclaration) CtModule(spoon.reflect.declaration.CtModule)

Example 3 with ModuleBinding

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

the class ASTNode method resolveAnnotations.

/**
 * Resolve annotations, and check duplicates, answers combined tagBits
 * for recognized standard annotations. Return null if nothing new is
 * resolved.
 */
public static AnnotationBinding[] resolveAnnotations(BlockScope scope, Annotation[] sourceAnnotations, Binding recipient, boolean copySE8AnnotationsToType) {
    AnnotationBinding[] annotations = null;
    int length = sourceAnnotations == null ? 0 : sourceAnnotations.length;
    if (recipient != null) {
        switch(recipient.kind()) {
            case Binding.PACKAGE:
                PackageBinding packageBinding = (PackageBinding) recipient;
                if ((packageBinding.tagBits & TagBits.AnnotationResolved) != 0)
                    return annotations;
                packageBinding.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
                break;
            case Binding.TYPE:
            case Binding.GENERIC_TYPE:
                ReferenceBinding type = (ReferenceBinding) recipient;
                if ((type.tagBits & TagBits.AnnotationResolved) != 0)
                    return annotations;
                type.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
                if (length > 0) {
                    annotations = new AnnotationBinding[length];
                    type.setAnnotations(annotations, false);
                }
                break;
            case Binding.METHOD:
                MethodBinding method = (MethodBinding) recipient;
                if ((method.tagBits & TagBits.AnnotationResolved) != 0)
                    return annotations;
                method.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
                if (length > 0) {
                    annotations = new AnnotationBinding[length];
                    method.setAnnotations(annotations, false);
                }
                break;
            case Binding.FIELD:
                FieldBinding field = (FieldBinding) recipient;
                if ((field.tagBits & TagBits.AnnotationResolved) != 0)
                    return annotations;
                field.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
                if (length > 0) {
                    annotations = new AnnotationBinding[length];
                    field.setAnnotations(annotations, false);
                }
                break;
            case Binding.RECORD_COMPONENT:
                RecordComponentBinding rcb = (RecordComponentBinding) recipient;
                if ((rcb.tagBits & TagBits.AnnotationResolved) != 0)
                    return annotations;
                rcb.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
                if (length > 0) {
                    annotations = new AnnotationBinding[length];
                    rcb.setAnnotations(annotations, false);
                }
                break;
            case Binding.LOCAL:
                LocalVariableBinding local = (LocalVariableBinding) recipient;
                if ((local.tagBits & TagBits.AnnotationResolved) != 0)
                    return annotations;
                local.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
                if (length > 0) {
                    annotations = new AnnotationBinding[length];
                    local.setAnnotations(annotations, scope, false);
                }
                break;
            case Binding.TYPE_PARAMETER:
            case Binding.TYPE_USE:
                // deliberately don't set the annotation resolved tagbits, it is not material and also we are working with a dummy static object.
                annotations = new AnnotationBinding[length];
                break;
            case Binding.MODULE:
                ModuleBinding module = (ModuleBinding) recipient;
                if ((module.tagBits & TagBits.AnnotationResolved) != 0)
                    return annotations;
                module.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
                if (length > 0) {
                    annotations = new AnnotationBinding[length];
                    module.setAnnotations(annotations, scope, false);
                }
                break;
            default:
                return annotations;
        }
    }
    if (sourceAnnotations == null)
        return annotations;
    for (int i = 0; i < length; i++) {
        Annotation annotation = sourceAnnotations[i];
        final Binding annotationRecipient = annotation.recipient;
        if (annotationRecipient != null && recipient != null) {
            // only local and field can share annotations and their types.
            switch(recipient.kind()) {
                case Binding.TYPE_USE:
                    if (annotations != null) {
                        // need to fill the instances array
                        for (int j = 0; j < length; j++) {
                            annotations[j] = sourceAnnotations[j].getCompilerAnnotation();
                        }
                    }
                    break;
                case Binding.FIELD:
                    FieldBinding field = (FieldBinding) recipient;
                    field.tagBits = ((FieldBinding) annotationRecipient).tagBits;
                    if (annotations != null) {
                        // need to fill the instances array
                        for (int j = 0; j < length; j++) {
                            Annotation annot = sourceAnnotations[j];
                            annotations[j] = annot.getCompilerAnnotation();
                        }
                    }
                    break;
                case Binding.RECORD_COMPONENT:
                    RecordComponentBinding recordComponentBinding = (RecordComponentBinding) recipient;
                    recordComponentBinding.tagBits = ((RecordComponentBinding) annotationRecipient).tagBits;
                    if (annotations != null) {
                        // need to fill the instances array
                        for (int j = 0; j < length; j++) {
                            Annotation annot = sourceAnnotations[j];
                            annotations[j] = annot.getCompilerAnnotation();
                        }
                    }
                    break;
                case Binding.LOCAL:
                    LocalVariableBinding local = (LocalVariableBinding) recipient;
                    // Note for JDK>=14, this could be LVB or RCB, hence typecasting to VB
                    long otherLocalTagBits = ((VariableBinding) annotationRecipient).tagBits;
                    // Make sure we retain the TagBits.IsArgument bit
                    local.tagBits = otherLocalTagBits | (local.tagBits & TagBits.IsArgument);
                    if ((otherLocalTagBits & TagBits.AnnotationSuppressWarnings) == 0) {
                        // need to fill the instances array
                        if (annotations != null) {
                            for (int j = 0; j < length; j++) {
                                Annotation annot = sourceAnnotations[j];
                                annotations[j] = annot.getCompilerAnnotation();
                            }
                        }
                    } else if (annotations != null) {
                        // One of the annotations at least is a SuppressWarnings annotation
                        LocalDeclaration localDeclaration = local.declaration;
                        int declarationSourceEnd = localDeclaration.declarationSourceEnd;
                        int declarationSourceStart = localDeclaration.declarationSourceStart;
                        for (int j = 0; j < length; j++) {
                            Annotation annot = sourceAnnotations[j];
                            /*
								 * Annotations are shared between two locals, but we still need to record
								 * the suppress annotation range for the second local
								 */
                            AnnotationBinding annotationBinding = annot.getCompilerAnnotation();
                            annotations[j] = annotationBinding;
                            if (annotationBinding != null) {
                                final ReferenceBinding annotationType = annotationBinding.getAnnotationType();
                                if (annotationType != null && annotationType.id == TypeIds.T_JavaLangSuppressWarnings) {
                                    annot.recordSuppressWarnings(scope, declarationSourceStart, declarationSourceEnd, scope.compilerOptions().suppressWarnings);
                                }
                            }
                        }
                    }
                    // copy the se8 annotations.
                    if (annotationRecipient instanceof RecordComponentBinding && copySE8AnnotationsToType)
                        copySE8AnnotationsToType(scope, recipient, sourceAnnotations, false);
                    break;
            }
            return annotations;
        } else {
            annotation.recipient = recipient;
            annotation.resolveType(scope);
            // null if receiver is a package binding
            if (annotations != null) {
                annotations[i] = annotation.getCompilerAnnotation();
            }
        }
    }
    /* See if the recipient is meta-annotated with @Repeatable and if so validate constraints. We can't do this during resolution of @Repeatable itself as @Target and
		   @Retention etc could come later
		*/
    if (recipient != null && recipient.isTaggedRepeatable()) {
        for (int i = 0; i < length; i++) {
            Annotation annotation = sourceAnnotations[i];
            ReferenceBinding annotationType = annotations[i] != null ? annotations[i].getAnnotationType() : null;
            if (annotationType != null && annotationType.id == TypeIds.T_JavaLangAnnotationRepeatable)
                annotation.checkRepeatableMetaAnnotation(scope);
        }
    }
    // check duplicate annotations
    if (annotations != null && length > 1) {
        // only copy after 1st duplicate is detected
        AnnotationBinding[] distinctAnnotations = annotations;
        Map implicitContainerAnnotations = null;
        for (int i = 0; i < length; i++) {
            AnnotationBinding annotation = distinctAnnotations[i];
            if (annotation == null)
                continue;
            ReferenceBinding annotationType = annotation.getAnnotationType();
            boolean foundDuplicate = false;
            ContainerAnnotation container = null;
            for (int j = i + 1; j < length; j++) {
                AnnotationBinding otherAnnotation = distinctAnnotations[j];
                if (otherAnnotation == null)
                    continue;
                if (TypeBinding.equalsEquals(otherAnnotation.getAnnotationType(), annotationType)) {
                    if (distinctAnnotations == annotations) {
                        System.arraycopy(distinctAnnotations, 0, distinctAnnotations = new AnnotationBinding[length], 0, length);
                    }
                    // report/process it only once
                    distinctAnnotations[j] = null;
                    if (annotationType.isRepeatableAnnotationType()) {
                        Annotation persistibleAnnotation = sourceAnnotations[i].getPersistibleAnnotation();
                        if (persistibleAnnotation instanceof ContainerAnnotation)
                            container = (ContainerAnnotation) persistibleAnnotation;
                        if (container == null) {
                            // first encounter with a duplicate.
                            ReferenceBinding containerAnnotationType = annotationType.containerAnnotationType();
                            container = new ContainerAnnotation(sourceAnnotations[i], containerAnnotationType, scope);
                            if (implicitContainerAnnotations == null)
                                implicitContainerAnnotations = new HashMap(3);
                            implicitContainerAnnotations.put(containerAnnotationType, sourceAnnotations[i]);
                            Annotation.checkForInstancesOfRepeatableWithRepeatingContainerAnnotation(scope, annotationType, sourceAnnotations);
                        }
                        container.addContainee(sourceAnnotations[j]);
                    } else {
                        foundDuplicate = true;
                        scope.problemReporter().duplicateAnnotation(sourceAnnotations[j], scope.compilerOptions().sourceLevel);
                    }
                }
            }
            if (container != null) {
                container.resolveType(scope);
            }
            if (foundDuplicate) {
                scope.problemReporter().duplicateAnnotation(sourceAnnotations[i], scope.compilerOptions().sourceLevel);
            }
        }
        // Check for presence of repeating annotation together with the containing annotation
        if (implicitContainerAnnotations != null) {
            for (int i = 0; i < length; i++) {
                if (distinctAnnotations[i] == null)
                    continue;
                Annotation annotation = sourceAnnotations[i];
                ReferenceBinding annotationType = distinctAnnotations[i].getAnnotationType();
                if (implicitContainerAnnotations.containsKey(annotationType)) {
                    scope.problemReporter().repeatedAnnotationWithContainer((Annotation) implicitContainerAnnotations.get(annotationType), annotation);
                }
            }
        }
    }
    if (copySE8AnnotationsToType)
        copySE8AnnotationsToType(scope, recipient, sourceAnnotations, false);
    return annotations;
}
Also used : SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) LocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding) WildcardBinding(org.eclipse.jdt.internal.compiler.lookup.WildcardBinding) FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) ParameterizedMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedMethodBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) ArrayBinding(org.eclipse.jdt.internal.compiler.lookup.ArrayBinding) PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding) AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) ParameterizedGenericMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding) RecordComponentBinding(org.eclipse.jdt.internal.compiler.lookup.RecordComponentBinding) HashMap(java.util.HashMap) PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) LocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding) AnnotationBinding(org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding) FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) ParameterizedMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedMethodBinding) ParameterizedGenericMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedGenericMethodBinding) RecordComponentBinding(org.eclipse.jdt.internal.compiler.lookup.RecordComponentBinding) LocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding) TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ModuleBinding

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

the class ASTNode method isMethodUseDeprecated.

/* Answer true if the method use is considered deprecated.
	* An access in the same compilation unit is allowed.
	*/
public final boolean isMethodUseDeprecated(MethodBinding method, Scope scope, boolean isExplicitUse, InvocationSite invocation) {
    reportPreviewAPI(scope, method.tagBits);
    // ignore references insing Javadoc comments
    if ((this.bits & ASTNode.InsideJavadoc) == 0 && method.isOrEnclosedByPrivateType() && !scope.isDefinedInMethod(method)) {
        // ignore cases where method is used from inside itself (e.g. direct recursions)
        method.original().modifiers |= ExtraCompilerModifiers.AccLocallyUsed;
    }
    // Caveat: this was not the case when access restriction funtion was added.
    if (isExplicitUse && (method.modifiers & ExtraCompilerModifiers.AccRestrictedAccess) != 0) {
        // note: explicit constructors calls warnings are kept despite the 'new C1()' case (two
        // warnings, one on type, the other on constructor), because of the 'super()' case.
        ModuleBinding module = method.declaringClass.module();
        LookupEnvironment env = (module == null) ? scope.environment() : module.environment;
        AccessRestriction restriction = env.getAccessRestriction(method.declaringClass.erasure());
        if (restriction != null) {
            scope.problemReporter().forbiddenReference(method, invocation, restriction.classpathEntryType, restriction.classpathEntryName, restriction.getProblemId());
        }
    }
    if (!method.isViewedAsDeprecated())
        return false;
    // inside same unit - no report
    if (scope.isDefinedInSameUnit(method.declaringClass))
        return false;
    // non explicit use and non explicitly deprecated - no report
    if (!isExplicitUse && (method.modifiers & ClassFileConstants.AccDeprecated) == 0) {
        return false;
    }
    // if context is deprecated, may avoid reporting
    if (!scope.compilerOptions().reportDeprecationInsideDeprecatedCode && scope.isInsideDeprecatedCode())
        return false;
    return true;
}
Also used : LookupEnvironment(org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment) AccessRestriction(org.eclipse.jdt.internal.compiler.env.AccessRestriction) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding)

Example 5 with ModuleBinding

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

the class ASTNode method isFieldUseDeprecated.

public final boolean isFieldUseDeprecated(FieldBinding field, Scope scope, int filteredBits) {
    if (// ignore references inside Javadoc comments
    (this.bits & ASTNode.InsideJavadoc) == 0 && // ignore write access
    (filteredBits & IsStrictlyAssigned) == 0 && field.isOrEnclosedByPrivateType() && // ignore cases where field is used from inside itself
    !scope.isDefinedInField(field)) {
        if (((filteredBits & IsCompoundAssigned) != 0))
            // used, but usage may not be relevant
            field.original().compoundUseFlag++;
        else
            field.original().modifiers |= ExtraCompilerModifiers.AccLocallyUsed;
    }
    reportPreviewAPI(scope, field.tagBits);
    if ((field.modifiers & ExtraCompilerModifiers.AccRestrictedAccess) != 0) {
        ModuleBinding module = field.declaringClass.module();
        LookupEnvironment env = (module == null) ? scope.environment() : module.environment;
        AccessRestriction restriction = env.getAccessRestriction(field.declaringClass.erasure());
        if (restriction != null) {
            scope.problemReporter().forbiddenReference(field, this, restriction.classpathEntryType, restriction.classpathEntryName, restriction.getProblemId());
        }
    }
    if (!field.isViewedAsDeprecated())
        return false;
    // inside same unit - no report
    if (scope.isDefinedInSameUnit(field.declaringClass))
        return false;
    // if context is deprecated, may avoid reporting
    if (!scope.compilerOptions().reportDeprecationInsideDeprecatedCode && scope.isInsideDeprecatedCode())
        return false;
    return true;
}
Also used : LookupEnvironment(org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment) AccessRestriction(org.eclipse.jdt.internal.compiler.env.AccessRestriction) ModuleBinding(org.eclipse.jdt.internal.compiler.lookup.ModuleBinding)

Aggregations

ModuleBinding (org.eclipse.jdt.internal.compiler.lookup.ModuleBinding)28 PackageBinding (org.eclipse.jdt.internal.compiler.lookup.PackageBinding)8 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)8 HashSet (java.util.HashSet)7 LookupEnvironment (org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment)6 PlainPackageBinding (org.eclipse.jdt.internal.compiler.lookup.PlainPackageBinding)6 TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)6 Binding (org.eclipse.jdt.internal.compiler.lookup.Binding)4 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)4 ProblemReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding)4 HashMap (java.util.HashMap)3 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)3 AccessRestriction (org.eclipse.jdt.internal.compiler.env.AccessRestriction)3 FieldBinding (org.eclipse.jdt.internal.compiler.lookup.FieldBinding)3 LocalVariableBinding (org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding)3 ProblemMethodBinding (org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding)3 SourceTypeBinding (org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding)3 TypeVariableBinding (org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding)3 ArrayList (java.util.ArrayList)2 Element (javax.lang.model.element.Element)2