use of org.eclipse.jdt.internal.compiler.codegen.AnnotationContext in project bazel-jdt-java-toolchain by salesforce.
the class ClassFile method completeRuntimeTypeAnnotations.
private int completeRuntimeTypeAnnotations(int attributesNumber, ASTNode node, Predicate<ASTNode> condition, Supplier<List<AnnotationContext>> supplier) {
int invisibleTypeAnnotationsCounter = 0;
int visibleTypeAnnotationsCounter = 0;
if (condition.test(node)) {
List<AnnotationContext> allTypeAnnotationContexts = supplier.get();
if (allTypeAnnotationContexts.size() > 0) {
AnnotationContext[] allTypeAnnotationContextsArray = new AnnotationContext[allTypeAnnotationContexts.size()];
allTypeAnnotationContexts.toArray(allTypeAnnotationContextsArray);
for (int j = 0, max2 = allTypeAnnotationContextsArray.length; j < max2; j++) {
AnnotationContext annotationContext = allTypeAnnotationContextsArray[j];
if ((annotationContext.visibility & AnnotationContext.INVISIBLE) != 0) {
invisibleTypeAnnotationsCounter++;
} else {
visibleTypeAnnotationsCounter++;
}
}
attributesNumber += generateRuntimeTypeAnnotations(allTypeAnnotationContextsArray, visibleTypeAnnotationsCounter, invisibleTypeAnnotationsCounter);
}
}
return attributesNumber;
}
use of org.eclipse.jdt.internal.compiler.codegen.AnnotationContext in project bazel-jdt-java-toolchain by salesforce.
the class ClassFile method addComponentAttributes.
private int addComponentAttributes(RecordComponentBinding recordComponentBinding, int componetAttributeOffset) {
// See JVMS 14 Table 4.7-C - Record Preview for allowed attributes
int attributesNumber = 0;
// add signature attribute
char[] genericSignature = recordComponentBinding.genericSignature();
if (genericSignature != null) {
attributesNumber += generateSignatureAttribute(genericSignature);
}
RecordComponent recordComponent = recordComponentBinding.sourceRecordComponent();
if (recordComponent != null) {
Annotation[] annotations = recordComponent.annotations;
if (annotations != null) {
attributesNumber += generateRuntimeAnnotations(annotations, TagBits.AnnotationForRecordComponent);
}
if ((this.produceAttributes & ClassFileConstants.ATTR_TYPE_ANNOTATION) != 0) {
List<AnnotationContext> allTypeAnnotationContexts = new ArrayList<>();
if (annotations != null && (recordComponent.bits & ASTNode.HasTypeAnnotations) != 0) {
recordComponent.getAllAnnotationContexts(AnnotationTargetTypeConstants.FIELD, allTypeAnnotationContexts);
}
TypeReference recordComponentType = recordComponent.type;
if (recordComponentType != null && ((recordComponentType.bits & ASTNode.HasTypeAnnotations) != 0)) {
recordComponentType.getAllAnnotationContexts(AnnotationTargetTypeConstants.RECORD_COMPONENT, allTypeAnnotationContexts);
}
int size = allTypeAnnotationContexts.size();
attributesNumber = completeRuntimeTypeAnnotations(attributesNumber, null, (node) -> size > 0, () -> allTypeAnnotationContexts);
}
}
if ((recordComponentBinding.tagBits & TagBits.HasMissingType) != 0) {
this.missingTypes = recordComponentBinding.type.collectMissingTypes(this.missingTypes);
}
return attributesNumber;
}
use of org.eclipse.jdt.internal.compiler.codegen.AnnotationContext in project bazel-jdt-java-toolchain by salesforce.
the class ClassFile method generateTypeAnnotationAttributeForTypeDeclaration.
private int generateTypeAnnotationAttributeForTypeDeclaration() {
TypeDeclaration typeDeclaration = this.referenceBinding.scope.referenceContext;
if ((typeDeclaration.bits & ASTNode.HasTypeAnnotations) == 0) {
return 0;
}
int attributesNumber = 0;
TypeReference superclass = typeDeclaration.superclass;
List<AnnotationContext> allTypeAnnotationContexts = new ArrayList<>();
if (superclass != null && (superclass.bits & ASTNode.HasTypeAnnotations) != 0) {
superclass.getAllAnnotationContexts(AnnotationTargetTypeConstants.CLASS_EXTENDS, -1, allTypeAnnotationContexts);
}
TypeReference[] superInterfaces = typeDeclaration.superInterfaces;
if (superInterfaces != null) {
for (int i = 0; i < superInterfaces.length; i++) {
TypeReference superInterface = superInterfaces[i];
if ((superInterface.bits & ASTNode.HasTypeAnnotations) == 0) {
continue;
}
superInterface.getAllAnnotationContexts(AnnotationTargetTypeConstants.CLASS_EXTENDS, i, allTypeAnnotationContexts);
}
}
// TODO: permittedTypes codegen
TypeParameter[] typeParameters = typeDeclaration.typeParameters;
if (typeParameters != null) {
for (int i = 0, max = typeParameters.length; i < max; i++) {
TypeParameter typeParameter = typeParameters[i];
if ((typeParameter.bits & ASTNode.HasTypeAnnotations) != 0) {
typeParameter.getAllAnnotationContexts(AnnotationTargetTypeConstants.CLASS_TYPE_PARAMETER, i, allTypeAnnotationContexts);
}
}
}
int size = allTypeAnnotationContexts.size();
attributesNumber = completeRuntimeTypeAnnotations(attributesNumber, null, (node) -> size > 0, () -> allTypeAnnotationContexts);
return attributesNumber;
}
use of org.eclipse.jdt.internal.compiler.codegen.AnnotationContext in project bazel-jdt-java-toolchain by salesforce.
the class ClassFile method completeMethodInfo.
/**
* INTERNAL USE-ONLY
* Complete the creation of a method info by setting up the number of attributes at the right offset.
*
* @param methodAttributeOffset <CODE>int</CODE>
* @param attributesNumber <CODE>int</CODE>
*/
public void completeMethodInfo(MethodBinding binding, int methodAttributeOffset, int attributesNumber) {
if ((this.produceAttributes & ClassFileConstants.ATTR_TYPE_ANNOTATION) != 0) {
List<AnnotationContext> allTypeAnnotationContexts = new ArrayList<>();
AbstractMethodDeclaration methodDeclaration = binding.sourceMethod();
if (methodDeclaration != null) {
if ((methodDeclaration.bits & ASTNode.HasTypeAnnotations) != 0) {
Argument[] arguments = methodDeclaration.arguments;
if (arguments != null) {
completeArgumentAnnotationInfo(arguments, allTypeAnnotationContexts);
}
Receiver receiver = methodDeclaration.receiver;
if (receiver != null && (receiver.type.bits & ASTNode.HasTypeAnnotations) != 0) {
receiver.type.getAllAnnotationContexts(AnnotationTargetTypeConstants.METHOD_RECEIVER, allTypeAnnotationContexts);
}
}
Annotation[] annotations = methodDeclaration.annotations;
if (annotations != null && !methodDeclaration.isClinit() && (methodDeclaration.isConstructor() || binding.returnType.id != T_void)) {
methodDeclaration.getAllAnnotationContexts(AnnotationTargetTypeConstants.METHOD_RETURN, allTypeAnnotationContexts);
}
if (!methodDeclaration.isConstructor() && !methodDeclaration.isClinit() && binding.returnType.id != T_void) {
MethodDeclaration declaration = (MethodDeclaration) methodDeclaration;
TypeReference typeReference = declaration.returnType;
if ((typeReference.bits & ASTNode.HasTypeAnnotations) != 0) {
typeReference.getAllAnnotationContexts(AnnotationTargetTypeConstants.METHOD_RETURN, allTypeAnnotationContexts);
}
}
TypeReference[] thrownExceptions = methodDeclaration.thrownExceptions;
if (thrownExceptions != null) {
for (int i = 0, max = thrownExceptions.length; i < max; i++) {
TypeReference thrownException = thrownExceptions[i];
thrownException.getAllAnnotationContexts(AnnotationTargetTypeConstants.THROWS, i, allTypeAnnotationContexts);
}
}
TypeParameter[] typeParameters = methodDeclaration.typeParameters();
if (typeParameters != null) {
for (int i = 0, max = typeParameters.length; i < max; i++) {
TypeParameter typeParameter = typeParameters[i];
if ((typeParameter.bits & ASTNode.HasTypeAnnotations) != 0) {
typeParameter.getAllAnnotationContexts(AnnotationTargetTypeConstants.METHOD_TYPE_PARAMETER, i, allTypeAnnotationContexts);
}
}
}
} else if (binding.sourceLambda() != null) {
// SyntheticMethodBinding, purpose : LambdaMethod.
LambdaExpression lambda = binding.sourceLambda();
if ((lambda.bits & ASTNode.HasTypeAnnotations) != 0) {
if (lambda.arguments != null)
completeArgumentAnnotationInfo(lambda.arguments, allTypeAnnotationContexts);
}
}
int size = allTypeAnnotationContexts.size();
attributesNumber = completeRuntimeTypeAnnotations(attributesNumber, null, (node) -> size > 0, () -> allTypeAnnotationContexts);
}
if ((this.produceAttributes & ClassFileConstants.ATTR_METHOD_PARAMETERS) != 0 || binding.isConstructor() && binding.declaringClass.isRecord()) {
attributesNumber += generateMethodParameters(binding);
}
// update the number of attributes
this.contents[methodAttributeOffset++] = (byte) (attributesNumber >> 8);
this.contents[methodAttributeOffset] = (byte) attributesNumber;
}
use of org.eclipse.jdt.internal.compiler.codegen.AnnotationContext in project bazel-jdt-java-toolchain by salesforce.
the class ClassFile method generateMethodInfoAttributes.
/**
* INTERNAL USE-ONLY
* That method generates the attributes of a code attribute.
* They could be:
* - an exception attribute for each try/catch found inside the method
* - a deprecated attribute
* - a synthetic attribute for synthetic access methods
*
* It returns the number of attributes created for the code attribute.
*
* @param methodBinding org.eclipse.jdt.internal.compiler.lookup.MethodBinding
* @return <CODE>int</CODE>
*/
public int generateMethodInfoAttributes(MethodBinding methodBinding) {
// leave two bytes for the attribute_number
this.contentsOffset += 2;
if (this.contentsOffset + 2 >= this.contents.length) {
resizeContents(2);
}
// now we can handle all the attribute for that method info:
// it could be:
// - a CodeAttribute
// - a ExceptionAttribute
// - a DeprecatedAttribute
// - a SyntheticAttribute
// Exception attribute
ReferenceBinding[] thrownsExceptions;
int attributesNumber = 0;
if ((thrownsExceptions = methodBinding.thrownExceptions) != Binding.NO_EXCEPTIONS) {
// The method has a throw clause. So we need to add an exception attribute
// check that there is enough space to write all the bytes for the exception attribute
attributesNumber += generateExceptionsAttribute(thrownsExceptions);
}
if (methodBinding.isDeprecated()) {
// Deprecated attribute
attributesNumber += generateDeprecatedAttribute();
}
if (this.targetJDK < ClassFileConstants.JDK1_5) {
if (methodBinding.isSynthetic()) {
attributesNumber += generateSyntheticAttribute();
}
if (methodBinding.isVarargs()) {
attributesNumber += generateVarargsAttribute();
}
}
// add signature attribute
char[] genericSignature = methodBinding.genericSignature();
if (genericSignature != null) {
attributesNumber += generateSignatureAttribute(genericSignature);
}
if (this.targetJDK >= ClassFileConstants.JDK1_4) {
AbstractMethodDeclaration methodDeclaration = methodBinding.sourceMethod();
if (methodBinding instanceof SyntheticMethodBinding) {
SyntheticMethodBinding syntheticMethod = (SyntheticMethodBinding) methodBinding;
if (syntheticMethod.purpose == SyntheticMethodBinding.SuperMethodAccess && CharOperation.equals(syntheticMethod.selector, syntheticMethod.targetMethod.selector))
methodDeclaration = ((SyntheticMethodBinding) methodBinding).targetMethod.sourceMethod();
if (syntheticMethod.recordComponentBinding != null) {
assert methodDeclaration == null;
long rcMask = TagBits.AnnotationForMethod | TagBits.AnnotationForTypeUse;
// record component (field) accessor method
ReferenceBinding declaringClass = methodBinding.declaringClass;
RecordComponent comp = getRecordComponent(declaringClass, methodBinding.selector);
if (comp != null) {
Annotation[] annotations = ASTNode.getRelevantAnnotations(comp.annotations, rcMask, null);
if (annotations != null) {
assert !methodBinding.isConstructor();
attributesNumber += generateRuntimeAnnotations(annotations, TagBits.AnnotationForMethod);
}
if ((this.produceAttributes & ClassFileConstants.ATTR_TYPE_ANNOTATION) != 0) {
List<AnnotationContext> allTypeAnnotationContexts = new ArrayList<>();
if (annotations != null && (comp.bits & ASTNode.HasTypeAnnotations) != 0) {
comp.getAllAnnotationContexts(AnnotationTargetTypeConstants.METHOD_RETURN, allTypeAnnotationContexts);
}
TypeReference compType = comp.type;
if (compType != null && ((compType.bits & ASTNode.HasTypeAnnotations) != 0)) {
compType.getAllAnnotationContexts(AnnotationTargetTypeConstants.METHOD_RETURN, allTypeAnnotationContexts);
}
int size = allTypeAnnotationContexts.size();
attributesNumber = completeRuntimeTypeAnnotations(attributesNumber, null, (node) -> size > 0, () -> allTypeAnnotationContexts);
}
}
}
}
if (methodDeclaration != null) {
Annotation[] annotations = methodDeclaration.annotations;
if (annotations != null) {
attributesNumber += generateRuntimeAnnotations(annotations, methodBinding.isConstructor() ? TagBits.AnnotationForConstructor : TagBits.AnnotationForMethod);
}
if ((methodBinding.tagBits & TagBits.HasParameterAnnotations) != 0) {
Argument[] arguments = methodDeclaration.arguments;
if (arguments != null) {
propagateRecordComponentArguments(methodDeclaration);
attributesNumber += generateRuntimeAnnotationsForParameters(arguments);
}
}
} else {
LambdaExpression lambda = methodBinding.sourceLambda();
if (lambda != null) {
if ((methodBinding.tagBits & TagBits.HasParameterAnnotations) != 0) {
Argument[] arguments = lambda.arguments();
if (arguments != null) {
int parameterCount = methodBinding.parameters.length;
int argumentCount = arguments.length;
if (parameterCount > argumentCount) {
// synthetics prefixed
int redShift = parameterCount - argumentCount;
System.arraycopy(arguments, 0, arguments = new Argument[parameterCount], redShift, argumentCount);
for (int i = 0; i < redShift; i++) arguments[i] = new Argument(CharOperation.NO_CHAR, 0, null, 0);
}
attributesNumber += generateRuntimeAnnotationsForParameters(arguments);
}
}
}
}
}
if ((methodBinding.tagBits & TagBits.HasMissingType) != 0) {
this.missingTypes = methodBinding.collectMissingTypes(this.missingTypes);
}
return attributesNumber;
}
Aggregations