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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations