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