Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read RuntimeInvisible*Annotations in Indexer #129

Merged
merged 4 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 58 additions & 6 deletions core/src/main/java/org/jboss/jandex/AnnotationInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public final class AnnotationInstance {
private final DotName name;
private AnnotationTarget target;
private final AnnotationValue[] values;
private final boolean runtimeVisible;

static class InstanceNameComparator implements Comparator<AnnotationInstance> {
public int compare(AnnotationInstance instance, AnnotationInstance instance2) {
Expand All @@ -54,15 +55,22 @@ public int compare(AnnotationInstance instance, AnnotationInstance instance2) {
}

AnnotationInstance(AnnotationInstance instance, AnnotationTarget target) {
this.name = instance.name;
this.values = instance.values;
this.target = target;
this(instance.name, target, instance.values, instance.runtimeVisible);
}

AnnotationInstance(DotName name, AnnotationTarget target, AnnotationValue[] values) {
AnnotationInstance(DotName name, AnnotationTarget target, AnnotationValue[] values, boolean runtimeVisible) {
this.name = name;
this.target = target;
this.values = values != null && values.length > 0 ? values : AnnotationValue.EMPTY_VALUE_ARRAY;
this.runtimeVisible = runtimeVisible;
}

static AnnotationInstance create(AnnotationInstance instance, AnnotationTarget target) {
return new AnnotationInstance(instance, target);
}

private static final AnnotationInstance create(DotName name) {
return new AnnotationInstance(name, null, AnnotationValue.EMPTY_VALUE_ARRAY, false);
}

/**
Expand All @@ -74,6 +82,20 @@ public int compare(AnnotationInstance instance, AnnotationInstance instance2) {
* @return the new mock Annotation Instance
*/
public static final AnnotationInstance create(DotName name, AnnotationTarget target, AnnotationValue[] values) {
return create(name, true, target, values);
}

/**
* Construct a new mock annotation instance. The passed values array will be defensively copied.
*
* @param name the name of the annotation instance
* @param visible whether the annotation is visible at runtime via the reflection API
* @param target the thing the annotation is declared on
* @param values the values of this annotation instance
* @return the new mock Annotation Instance
*/
public static final AnnotationInstance create(DotName name, boolean visible, AnnotationTarget target,
AnnotationValue[] values) {
if (name == null)
throw new IllegalArgumentException("Name can't be null");

Expand All @@ -89,7 +111,7 @@ public int compare(AnnotationValue o1, AnnotationValue o2) {
}
});

return new AnnotationInstance(name, target, values);
return new AnnotationInstance(name, target, values, visible);
}

/**
Expand All @@ -101,13 +123,33 @@ public int compare(AnnotationValue o1, AnnotationValue o2) {
* @return the new mock Annotation Instance
*/
public static final AnnotationInstance create(DotName name, AnnotationTarget target, List<AnnotationValue> values) {
return create(name, true, target, values);
}

/**
* Construct a new mock annotation instance. The passed values list will be defensively copied.
*
* @param name the name of the annotation instance
* @param visible whether the annotation is visible at runtime via the reflection API
* @param target the thing the annotation is declared on
* @param values the values of this annotation instance
* @return the new mock Annotation Instance
*/
public static final AnnotationInstance create(DotName name, boolean visible, AnnotationTarget target,
List<AnnotationValue> values) {
if (name == null)
throw new IllegalArgumentException("Name can't be null");

if (values == null)
throw new IllegalArgumentException("Values can't be null");

return create(name, target, values.toArray(ANNOTATION_VALUES_TYPE));
return create(name, visible, target, values.toArray(ANNOTATION_VALUES_TYPE));
}

static final AnnotationInstance binarySearch(AnnotationInstance[] annotations, DotName name) {
AnnotationInstance key = create(name);
int i = Arrays.binarySearch(annotations, key, AnnotationInstance.NAME_COMPARATOR);
return i >= 0 ? annotations[i] : null;
}

/**
Expand Down Expand Up @@ -278,6 +320,16 @@ AnnotationValue[] valueArray() {
return values;
}

/**
* Returns true if this annotation uses RetentionPolicy.RUNTIME and is
* visible to runtime reflection.
*
* @since 3.0
*/
public boolean runtimeVisible() {
return this.runtimeVisible;
}

/**
* Returns an optionally simplified string that represents this annotation instance.
* If simplified the output is smaller but missing information, such as the package
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/org/jboss/jandex/FieldInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ final AnnotationInstance[] annotationArray() {
}

final AnnotationInstance annotation(DotName name) {
AnnotationInstance key = new AnnotationInstance(name, null, null);
int i = Arrays.binarySearch(annotations, key, AnnotationInstance.NAME_COMPARATOR);
return i >= 0 ? annotations[i] : null;
return AnnotationInstance.binarySearch(annotations, name);
}

final boolean hasAnnotation(DotName name) {
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/jboss/jandex/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ private Collection<AnnotationInstance> getRepeatableAnnotations(DotName annotati
for (AnnotationInstance containingInstance : getAnnotations(containingAnnotationName)) {
for (AnnotationInstance nestedInstance : containingInstance.value().asNestedArray()) {
// We need to set the target of the containing instance
instances.add(new AnnotationInstance(nestedInstance.name(), containingInstance.target(),
nestedInstance.valueArray()));
instances.add(AnnotationInstance.create(nestedInstance, containingInstance.target()));
}
}
return instances;
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/jboss/jandex/IndexReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ public Index read() throws IOException {
readVersion();
}

return reader.read(version);
return reader.read();
}

private void initReader(int version) throws IOException {
IndexReaderImpl reader;
if (version >= IndexReaderV1.MIN_VERSION && version <= IndexReaderV1.MAX_VERSION) {
reader = new IndexReaderV1(input);
reader = new IndexReaderV1(input, version);
} else if (version >= IndexReaderV2.MIN_VERSION && version <= IndexReaderV2.MAX_VERSION) {
reader = new IndexReaderV2(input);
reader = new IndexReaderV2(input, version);
} else {
input.close();
throw new UnsupportedVersion("Can't read index version " + version
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jboss/jandex/IndexReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @author Jason T. Greene
*/
abstract class IndexReaderImpl {
abstract Index read(int version) throws IOException;
abstract Index read() throws IOException;

abstract int toDataVersion(int version);
}
17 changes: 10 additions & 7 deletions core/src/main/java/org/jboss/jandex/IndexReaderV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ final class IndexReaderV1 extends IndexReaderImpl {
private static final int AVALUE_ARRAY = 12;
private static final int AVALUE_NESTED = 13;

private PackedDataInputStream input;
private final PackedDataInputStream input;
private final int version;
private DotName[] classTable;
private String[] stringTable;
private HashMap<DotName, List<AnnotationInstance>> masterAnnotations;
Expand All @@ -75,8 +76,9 @@ final class IndexReaderV1 extends IndexReaderImpl {
*
* @param input a stream which points to a jandex index file
*/
IndexReaderV1(PackedDataInputStream input) {
IndexReaderV1(PackedDataInputStream input, int version) {
this.input = input;
this.version = version;
}

/**
Expand All @@ -88,21 +90,21 @@ final class IndexReaderV1 extends IndexReaderImpl {
* @throws IllegalArgumentException if the stream does not point to Jandex index data
* @throws org.jboss.jandex.UnsupportedVersion if the index data is tagged with a version not known to this reader
*/
Index read(int version) throws IOException {
Index read() throws IOException {
try {
PackedDataInputStream stream = this.input;
masterAnnotations = new HashMap<DotName, List<AnnotationInstance>>();
readClassTable(stream);
readStringTable(stream);
return readClasses(stream, version);
return readClasses(stream);
} finally {
classTable = null;
stringTable = null;
masterAnnotations = null;
}
}

private Index readClasses(PackedDataInputStream stream, int version) throws IOException {
private Index readClasses(PackedDataInputStream stream) throws IOException {
int entries = stream.readPackedU32();
HashMap<DotName, List<ClassInfo>> subclasses = new HashMap<DotName, List<ClassInfo>>();
HashMap<DotName, List<ClassInfo>> implementors = new HashMap<DotName, List<ClassInfo>>();
Expand Down Expand Up @@ -177,7 +179,7 @@ private void readAnnotations(PackedDataInputStream stream, Map<DotName, List<Ann
}

AnnotationValue[] values = readAnnotationValues(stream);
AnnotationInstance instance = new AnnotationInstance(annotationName, target, values);
AnnotationInstance instance = AnnotationInstance.create(annotationName, target, values);

recordAnnotation(masterAnnotations, annotationName, instance);
recordAnnotation(annotations, annotationName, instance);
Expand Down Expand Up @@ -235,7 +237,8 @@ private AnnotationValue[] readAnnotationValues(PackedDataInputStream stream) thr
break;
case AVALUE_NESTED: {
DotName nestedName = classTable[stream.readPackedU32()];
AnnotationInstance nestedInstance = new AnnotationInstance(nestedName, null, readAnnotationValues(stream));
AnnotationInstance nestedInstance = AnnotationInstance.create(nestedName, null,
readAnnotationValues(stream));
value = new AnnotationValue.NestedAnnotation(name, nestedInstance);
break;
}
Expand Down
43 changes: 24 additions & 19 deletions core/src/main/java/org/jboss/jandex/IndexReaderV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
*/
final class IndexReaderV2 extends IndexReaderImpl {
static final int MIN_VERSION = 6;
static final int MAX_VERSION = 10;
static final int MAX_VERSION = 11;
static final int MAX_DATA_VERSION = 4;
private static final byte NULL_TARGET_TAG = 0;
private static final byte FIELD_TAG = 1;
Expand Down Expand Up @@ -78,7 +78,8 @@ final class IndexReaderV2 extends IndexReaderImpl {
private static final int HAS_ENCLOSING_METHOD = 1;
private final static byte[] INIT_METHOD_NAME = Utils.toUTF8("<init>");

private PackedDataInputStream input;
private final PackedDataInputStream input;
private final int version;
private byte[][] byteTable;
private String[] stringTable;
private DotName[] nameTable;
Expand All @@ -90,11 +91,12 @@ final class IndexReaderV2 extends IndexReaderImpl {
private RecordComponentInternal[] recordComponentTable;
private HashMap<DotName, Set<DotName>> users;

IndexReaderV2(PackedDataInputStream input) {
IndexReaderV2(PackedDataInputStream input, int version) {
this.input = input;
this.version = version;
}

Index read(int version) throws IOException {
Index read() throws IOException {
try {
PackedDataInputStream stream = this.input;
int annotationsSize = stream.readPackedU32();
Expand All @@ -119,12 +121,12 @@ Index read(int version) throws IOException {
if (version >= 10) {
readUsers(stream, usersSize);
}
readMethodTable(stream, version);
readMethodTable(stream);
readFieldTable(stream);
if (version >= 10) {
readRecordComponentTable(stream);
}
return readClasses(stream, annotationsSize, implementorsSize, subclassesSize, version);
return readClasses(stream, annotationsSize, implementorsSize, subclassesSize);
} finally {
byteTable = null;
stringTable = null;
Expand Down Expand Up @@ -316,7 +318,11 @@ private AnnotationInstance readAnnotationEntry(PackedDataInputStream stream, Ann
DotName name = nameTable[stream.readPackedU32()];
AnnotationTarget target = readAnnotationTarget(stream, caller);
AnnotationValue[] values = readAnnotationValues(stream);
return new AnnotationInstance(name, target, values);
boolean visible = true;
if (version >= 11) {
visible = stream.readBoolean();
}
return AnnotationInstance.create(name, visible, target, values);
}

private Type[] readTypeListReference(PackedDataInputStream stream) throws IOException {
Expand Down Expand Up @@ -450,12 +456,12 @@ private AnnotationTarget readAnnotationTarget(PackedDataInputStream stream, Anno
throw new IllegalStateException("Invalid tag: " + tag);
}

private void readMethodTable(PackedDataInputStream stream, int version) throws IOException {
private void readMethodTable(PackedDataInputStream stream) throws IOException {
// Null holds the first slot
int size = stream.readPackedU32() + 1;
methodTable = new MethodInternal[size];
for (int i = 1; i < size; i++) {
methodTable[i] = readMethodEntry(stream, version);
methodTable[i] = readMethodEntry(stream);
}

}
Expand All @@ -478,7 +484,7 @@ private void readRecordComponentTable(PackedDataInputStream stream) throws IOExc
}
}

private MethodInternal readMethodEntry(PackedDataInputStream stream, int version) throws IOException {
private MethodInternal readMethodEntry(PackedDataInputStream stream) throws IOException {
byte[] name = byteTable[stream.readPackedU32()];
short flags = (short) stream.readPackedU32();
Type[] typeParameters = typeListTable[stream.readPackedU32()];
Expand Down Expand Up @@ -538,7 +544,7 @@ private RecordComponentInternal readRecordComponentEntry(PackedDataInputStream s
}

private ClassInfo readClassEntry(PackedDataInputStream stream,
Map<DotName, List<AnnotationInstance>> masterAnnotations, int version) throws IOException {
Map<DotName, List<AnnotationInstance>> masterAnnotations) throws IOException {
DotName name = nameTable[stream.readPackedU32()];
short flags = (short) stream.readPackedU32();
Type superType = typeTable[stream.readPackedU32()];
Expand All @@ -564,7 +570,7 @@ private ClassInfo readClassEntry(PackedDataInputStream stream,
if (hasNesting) {
enclosingClass = nameTable[stream.readPackedU32()];
simpleName = stringTable[stream.readPackedU32()];
enclosingMethod = hasEnclosingMethod ? readEnclosingMethod(stream, version) : null;
enclosingMethod = hasEnclosingMethod ? readEnclosingMethod(stream) : null;
}

int size = stream.readPackedU32();
Expand Down Expand Up @@ -784,7 +790,7 @@ private void updateAnnotationTargetInfo(AnnotationInstance[] annotations, ClassI
}
}

private ClassInfo.EnclosingMethodInfo readEnclosingMethod(PackedDataInputStream stream, int version) throws IOException {
private ClassInfo.EnclosingMethodInfo readEnclosingMethod(PackedDataInputStream stream) throws IOException {
if (version < 9 && stream.readUnsignedByte() != HAS_ENCLOSING_METHOD) {
return null;
}
Expand All @@ -797,7 +803,7 @@ private ClassInfo.EnclosingMethodInfo readEnclosingMethod(PackedDataInputStream
}

private Index readClasses(PackedDataInputStream stream,
int annotationsSize, int implementorsSize, int subclassesSize, int version) throws IOException {
int annotationsSize, int implementorsSize, int subclassesSize) throws IOException {
int classesSize = stream.readPackedU32();
HashMap<DotName, ClassInfo> classes = new HashMap<DotName, ClassInfo>(classesSize);
HashMap<DotName, List<ClassInfo>> subclasses = new HashMap<DotName, List<ClassInfo>>(subclassesSize);
Expand All @@ -806,7 +812,7 @@ private Index readClasses(PackedDataInputStream stream,
annotationsSize);

for (int i = 0; i < classesSize; i++) {
ClassInfo clazz = readClassEntry(stream, masterAnnotations, version);
ClassInfo clazz = readClassEntry(stream, masterAnnotations);
addClassToMap(subclasses, clazz.superName(), clazz);
for (Type interfaceType : clazz.interfaceTypeArray()) {
addClassToMap(implementors, interfaceType.name(), clazz);
Expand All @@ -827,22 +833,21 @@ private Index readClasses(PackedDataInputStream stream,
users = Collections.emptyMap();
}

Map<DotName, ModuleInfo> modules = (version >= 10) ? readModules(stream, masterAnnotations, version)
Map<DotName, ModuleInfo> modules = (version >= 10) ? readModules(stream, masterAnnotations)
: Collections.<DotName, ModuleInfo> emptyMap();

return new Index(masterAnnotations, subclasses, implementors, classes, modules, users);
}

private Map<DotName, ModuleInfo> readModules(PackedDataInputStream stream,
Map<DotName, List<AnnotationInstance>> masterAnnotations,
int version) throws IOException {
Map<DotName, List<AnnotationInstance>> masterAnnotations) throws IOException {

int modulesSize = stream.readPackedU32();
Map<DotName, ModuleInfo> modules = modulesSize > 0 ? new HashMap<DotName, ModuleInfo>(modulesSize)
: Collections.<DotName, ModuleInfo> emptyMap();

for (int i = 0; i < modulesSize; i++) {
ClassInfo clazz = readClassEntry(stream, masterAnnotations, version);
ClassInfo clazz = readClassEntry(stream, masterAnnotations);
ModuleInfo module = readModuleEntry(stream, clazz);
modules.put(module.name(), module);
}
Expand Down
Loading