diff --git a/src/main/java/org/jboss/jandex/AnnotationTarget.java b/src/main/java/org/jboss/jandex/AnnotationTarget.java index ee94655a..fc93feb2 100644 --- a/src/main/java/org/jboss/jandex/AnnotationTarget.java +++ b/src/main/java/org/jboss/jandex/AnnotationTarget.java @@ -31,4 +31,8 @@ */ public interface AnnotationTarget { + public enum Kind {CLASS, FIELD, METHOD, METHOD_PARAMETER, TYPE} + + Kind kind(); + } diff --git a/src/main/java/org/jboss/jandex/AnnotationValue.java b/src/main/java/org/jboss/jandex/AnnotationValue.java index 4cedb42a..237321dc 100644 --- a/src/main/java/org/jboss/jandex/AnnotationValue.java +++ b/src/main/java/org/jboss/jandex/AnnotationValue.java @@ -67,6 +67,9 @@ public abstract class AnnotationValue { static final AnnotationValue[] EMPTY_VALUE_ARRAY = new AnnotationValue[0]; + public enum Kind {BYTE, SHORT, INTEGER, CHARACTER, FLOAT, DOUBLE, LONG, + BOOLEAN, CLASS, STRING, ENUM, ARRAY, NESTED, UNKNOWN} + private final String name; AnnotationValue(String name) { @@ -141,10 +144,16 @@ public final String name() { * Returns a detyped value that represents the underlying annotation value. * It is recommended that the type specific methods be used instead. * - * @return the underly value + * @return the underlying value */ public abstract Object value(); + public abstract Kind kind(); + + public Kind componentKind() { + throw new IllegalArgumentException("Not an array"); + } + /** * Converts the underlying numerical type to an integer as if it was * casted in Java. @@ -477,6 +486,11 @@ public String value() { return value; } + @Override + public Kind kind() { + return Kind.STRING; + } + public String toString() { StringBuilder builder = new StringBuilder(); if (super.name.length() > 0) @@ -518,6 +532,11 @@ public Byte value() { return value; } + @Override + public Kind kind() { + return Kind.BYTE; + } + public int asInt() { return value; } @@ -575,6 +594,11 @@ public Character value() { return value; } + @Override + public Kind kind() { + return Kind.CHARACTER; + } + public char asChar() { return value; } @@ -612,6 +636,11 @@ public Double value() { return value; } + @Override + public Kind kind() { + return Kind.DOUBLE; + } + public int asInt() { return (int) value; } @@ -672,6 +701,11 @@ public Float value() { return value; } + @Override + public Kind kind() { + return Kind.FLOAT; + } + public int asInt() { return (int) value; } @@ -730,6 +764,11 @@ public Short value() { return value; } + @Override + public Kind kind() { + return Kind.SHORT; + } + public int asInt() { return value; } @@ -788,6 +827,11 @@ public Integer value() { return value; } + @Override + public Kind kind() { + return Kind.INTEGER; + } + public int asInt() { return value; } @@ -846,6 +890,11 @@ public Long value() { return value; } + @Override + public Kind kind() { + return Kind.LONG; + } + public int asInt() { return (int) value; } @@ -904,6 +953,11 @@ public Boolean value() { return value; } + @Override + public Kind kind() { + return Kind.BOOLEAN; + } + public boolean asBoolean() { return value; } @@ -924,6 +978,11 @@ public String value() { return value; } + @Override + public Kind kind() { + return Kind.ENUM; + } + public String asEnum() { return value; } @@ -968,6 +1027,11 @@ public Type value() { return type; } + @Override + public Kind kind() { + return Kind.CLASS; + } + public Type asClass() { return type; } @@ -1006,6 +1070,11 @@ public AnnotationInstance value() { return value; } + @Override + public Kind kind() { + return Kind.NESTED; + } + public AnnotationInstance asNested() { return value; } @@ -1044,6 +1113,16 @@ public AnnotationValue[] value() { return value; } + @Override + public Kind kind() { + return Kind.ARRAY; + } + + @Override + public Kind componentKind() { + return value.length > 0 ? value[0].kind() : Kind.UNKNOWN; + } + AnnotationValue[] asArray() { return value; } diff --git a/src/main/java/org/jboss/jandex/ClassExtendsTypeTarget.java b/src/main/java/org/jboss/jandex/ClassExtendsTypeTarget.java index 1551c786..47d82292 100644 --- a/src/main/java/org/jboss/jandex/ClassExtendsTypeTarget.java +++ b/src/main/java/org/jboss/jandex/ClassExtendsTypeTarget.java @@ -13,8 +13,8 @@ public ClassExtendsTypeTarget(AnnotationTarget enclosingTarget, Type target, int } @Override - public final Kind kind() { - return Kind.CLASS_EXTENDS; + public final Usage usage() { + return Usage.CLASS_EXTENDS; } @Override diff --git a/src/main/java/org/jboss/jandex/ClassInfo.java b/src/main/java/org/jboss/jandex/ClassInfo.java index 73829ff8..69d7af0d 100644 --- a/src/main/java/org/jboss/jandex/ClassInfo.java +++ b/src/main/java/org/jboss/jandex/ClassInfo.java @@ -152,6 +152,11 @@ public static ClassInfo create(DotName name, DotName superName, short flags, Dot return new ClassInfo(name, superClassType, flags, interfaceTypes, annotations, hasNoArgsConstructor); } + @Override + public final Kind kind() { + return Kind.CLASS; + } + public String toString() { return name.toString(); } diff --git a/src/main/java/org/jboss/jandex/EmptyTypeTarget.java b/src/main/java/org/jboss/jandex/EmptyTypeTarget.java index a6b8be66..885d2402 100644 --- a/src/main/java/org/jboss/jandex/EmptyTypeTarget.java +++ b/src/main/java/org/jboss/jandex/EmptyTypeTarget.java @@ -21,8 +21,8 @@ public boolean isReceiver() { } @Override - public final Kind kind() { - return Kind.EMPTY; + public final Usage usage() { + return Usage.EMPTY; } @Override diff --git a/src/main/java/org/jboss/jandex/FieldInfo.java b/src/main/java/org/jboss/jandex/FieldInfo.java index 3c0bdae5..933a6aaf 100644 --- a/src/main/java/org/jboss/jandex/FieldInfo.java +++ b/src/main/java/org/jboss/jandex/FieldInfo.java @@ -97,6 +97,10 @@ public final Type type() { return internal.type(); } + public final Kind kind() { + return Kind.FIELD; + } + public List annotations() { return internal.annotations(); } diff --git a/src/main/java/org/jboss/jandex/IndexWriterV2.java b/src/main/java/org/jboss/jandex/IndexWriterV2.java index fe8ad262..595a70ef 100644 --- a/src/main/java/org/jboss/jandex/IndexWriterV2.java +++ b/src/main/java/org/jboss/jandex/IndexWriterV2.java @@ -326,7 +326,7 @@ private void writeAnnotationTarget(PackedDataOutputStream stream, AnnotationTarg } private void writeTypeTarget(PackedDataOutputStream stream, TypeTarget typeTarget) throws IOException { - switch (typeTarget.kind()) { + switch (typeTarget.usage()) { case EMPTY: { writeTypeTargetFields(stream, EMPTY_TYPE_TAG, typeTarget); stream.writeByte(typeTarget.asEmpty().isReceiver() ? 1 : 0); diff --git a/src/main/java/org/jboss/jandex/IndexWriterV2Old.java b/src/main/java/org/jboss/jandex/IndexWriterV2Old.java index 33aefc11..4693bf4a 100644 --- a/src/main/java/org/jboss/jandex/IndexWriterV2Old.java +++ b/src/main/java/org/jboss/jandex/IndexWriterV2Old.java @@ -269,7 +269,7 @@ private void writeAnnotationTarget(PackedDataOutputStream stream, AnnotationTarg } private void writeTypeTarget(PackedDataOutputStream stream, TypeTarget typeTarget) throws IOException { - switch (typeTarget.kind()) { + switch (typeTarget.usage()) { case EMPTY: { writeTypeTargetFields(stream, EMPTY_TYPE_TAG, typeTarget); stream.writeByte(typeTarget.asEmpty().isReceiver() ? 1 : 0); diff --git a/src/main/java/org/jboss/jandex/Indexer.java b/src/main/java/org/jboss/jandex/Indexer.java index 55c27d45..dabd4cf9 100644 --- a/src/main/java/org/jboss/jandex/Indexer.java +++ b/src/main/java/org/jboss/jandex/Indexer.java @@ -32,7 +32,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; -import java.util.Deque; import java.util.HashMap; import java.util.IdentityHashMap; import java.util.List; @@ -566,7 +565,7 @@ private void resolveTypeAnnotation(AnnotationTarget target, TypeAnnotationState TypeTarget typeTarget = typeAnnotationState.target; - if (typeTarget.kind() == TypeTarget.Kind.TYPE_PARAMETER_BOUND) { + if (typeTarget.usage() == TypeTarget.Usage.TYPE_PARAMETER_BOUND) { TypeParameterBoundTypeTarget bound = (TypeParameterBoundTypeTarget) typeTarget; Type[] types = copyTypeParameters(target); int index = bound.position(); @@ -582,7 +581,7 @@ private void resolveTypeAnnotation(AnnotationTarget target, TypeAnnotationState type = type.copyType(boundIndex, resolveTypePath(type.boundArray()[boundIndex], typeAnnotationState)); types[index] = intern(type); setTypeParameters(target, intern(types)); - } else if (typeTarget.kind() == TypeTarget.Kind.TYPE_PARAMETER) { + } else if (typeTarget.usage() == TypeTarget.Usage.TYPE_PARAMETER) { TypeParameterTypeTarget parameter = (TypeParameterTypeTarget) typeTarget; Type[] types = copyTypeParameters(target); int index = parameter.position(); @@ -592,7 +591,7 @@ private void resolveTypeAnnotation(AnnotationTarget target, TypeAnnotationState types[index] = resolveTypePath(types[index], typeAnnotationState); setTypeParameters(target, intern(types)); - } else if (typeTarget.kind() == TypeTarget.Kind.CLASS_EXTENDS) { + } else if (typeTarget.usage() == TypeTarget.Usage.CLASS_EXTENDS) { ClassInfo clazz = (ClassInfo) target; ClassExtendsTypeTarget extendsTarget = (ClassExtendsTypeTarget) typeTarget; int index = extendsTarget.position(); @@ -603,7 +602,7 @@ private void resolveTypeAnnotation(AnnotationTarget target, TypeAnnotationState types[index] = resolveTypePath(types[index], typeAnnotationState); clazz.setInterfaceTypes(intern(types)); } - } else if (typeTarget.kind() == TypeTarget.Kind.METHOD_PARAMETER) { + } else if (typeTarget.usage() == TypeTarget.Usage.METHOD_PARAMETER) { MethodInfo method = (MethodInfo) target; MethodParameterTypeTarget parameter = (MethodParameterTypeTarget) typeTarget; int index = parameter.position(); @@ -615,17 +614,17 @@ private void resolveTypeAnnotation(AnnotationTarget target, TypeAnnotationState types[index] = resolveTypePath(types[index], typeAnnotationState); method.setParameters(intern(types)); - } else if (typeTarget.kind() == TypeTarget.Kind.EMPTY && target instanceof FieldInfo) { + } else if (typeTarget.usage() == TypeTarget.Usage.EMPTY && target instanceof FieldInfo) { FieldInfo field = (FieldInfo) target; field.setType(resolveTypePath(field.type(), typeAnnotationState)); - } else if (typeTarget.kind() == TypeTarget.Kind.EMPTY && target instanceof MethodInfo) { + } else if (typeTarget.usage() == TypeTarget.Usage.EMPTY && target instanceof MethodInfo) { MethodInfo method = (MethodInfo) target; if (((EmptyTypeTarget) typeTarget).isReceiver()) { method.setReceiverType(resolveTypePath(method.receiverType(), typeAnnotationState)); } else { method.setReturnType(resolveTypePath(method.returnType(), typeAnnotationState)); } - } else if (typeTarget.kind() == TypeTarget.Kind.THROWS && target instanceof MethodInfo) { + } else if (typeTarget.usage() == TypeTarget.Usage.THROWS && target instanceof MethodInfo) { MethodInfo method = (MethodInfo) target; int position = ((ThrowsTypeTarget)typeTarget).position(); Type[] exceptions = method.copyExceptions(); @@ -708,7 +707,7 @@ private void updateTypeTarget(AnnotationTarget enclosingTarget, TypeAnnotationSt TypeTarget target = typeAnnotationState.target; Type type; - switch (target.kind()) { + switch (target.usage()) { case EMPTY: { if (enclosingTarget instanceof FieldInfo) { type = ((FieldInfo)enclosingTarget).type(); @@ -744,7 +743,7 @@ private void updateTypeTarget(AnnotationTarget enclosingTarget, TypeAnnotationSt break; } default: - throw new IllegalStateException("Unknown type target: " + target.kind()); + throw new IllegalStateException("Unknown type target: " + target.usage()); } type = searchTypePath(type, typeAnnotationState); diff --git a/src/main/java/org/jboss/jandex/Main.java b/src/main/java/org/jboss/jandex/Main.java index 775a9e0c..1fa79e90 100644 --- a/src/main/java/org/jboss/jandex/Main.java +++ b/src/main/java/org/jboss/jandex/Main.java @@ -67,7 +67,6 @@ private void execute(String[] args) { long start = System.currentTimeMillis(); index = getIndex(start); - //index.printAnnotations(); outputFile = null; source = null; } catch (Exception e) { @@ -160,9 +159,19 @@ private void scanFile(File source, Indexer indexer) throws FileNotFoundException System.err.println("ERROR: Could not index " + source.getName() + ": " + message); if (verbose) e.printStackTrace(System.err); + } finally { + safeClose(input); } + } - return; + private void safeClose(FileInputStream input) { + if (input != null) { + try { + input.close(); + } catch (Throwable t) { + // EAT + } + } } private static void printUsage() { diff --git a/src/main/java/org/jboss/jandex/MethodInfo.java b/src/main/java/org/jboss/jandex/MethodInfo.java index 847c9d6f..96d9209a 100644 --- a/src/main/java/org/jboss/jandex/MethodInfo.java +++ b/src/main/java/org/jboss/jandex/MethodInfo.java @@ -38,7 +38,6 @@ public final class MethodInfo implements AnnotationTarget { MethodInfo() { - methodInternal = new MethodInternal(); } MethodInfo(ClassInfo clazz, MethodInternal methodInternal) { @@ -92,6 +91,10 @@ public final String name() { return methodInternal.name(); } + public final Kind kind() { + return Kind.METHOD; + } + /** * Returns the class that declared this method * diff --git a/src/main/java/org/jboss/jandex/MethodParameterInfo.java b/src/main/java/org/jboss/jandex/MethodParameterInfo.java index 2b030962..8724af73 100644 --- a/src/main/java/org/jboss/jandex/MethodParameterInfo.java +++ b/src/main/java/org/jboss/jandex/MethodParameterInfo.java @@ -69,4 +69,9 @@ public final short position() { public String toString() { return method + " #" + parameter; } + + @Override + public Kind kind() { + return Kind.METHOD_PARAMETER; + } } diff --git a/src/main/java/org/jboss/jandex/MethodParameterTypeTarget.java b/src/main/java/org/jboss/jandex/MethodParameterTypeTarget.java index c17eda7e..4079096f 100644 --- a/src/main/java/org/jboss/jandex/MethodParameterTypeTarget.java +++ b/src/main/java/org/jboss/jandex/MethodParameterTypeTarget.java @@ -14,8 +14,8 @@ public class MethodParameterTypeTarget extends PositionBasedTypeTarget { } @Override - public final Kind kind() { - return Kind.METHOD_PARAMETER; + public final Usage usage() { + return Usage.METHOD_PARAMETER; } @Override diff --git a/src/main/java/org/jboss/jandex/ThrowsTypeTarget.java b/src/main/java/org/jboss/jandex/ThrowsTypeTarget.java index 8369ac52..be447113 100644 --- a/src/main/java/org/jboss/jandex/ThrowsTypeTarget.java +++ b/src/main/java/org/jboss/jandex/ThrowsTypeTarget.java @@ -13,8 +13,8 @@ public class ThrowsTypeTarget extends PositionBasedTypeTarget { } @Override - public Kind kind() { - return Kind.THROWS; + public Usage usage() { + return Usage.THROWS; } @Override diff --git a/src/main/java/org/jboss/jandex/TypeParameterBoundTypeTarget.java b/src/main/java/org/jboss/jandex/TypeParameterBoundTypeTarget.java index eb87484c..68c67872 100644 --- a/src/main/java/org/jboss/jandex/TypeParameterBoundTypeTarget.java +++ b/src/main/java/org/jboss/jandex/TypeParameterBoundTypeTarget.java @@ -21,8 +21,8 @@ public final int boundPosition() { } @Override - public final Kind kind() { - return Kind.TYPE_PARAMETER_BOUND; + public final Usage usage() { + return Usage.TYPE_PARAMETER_BOUND; } @Override diff --git a/src/main/java/org/jboss/jandex/TypeParameterTypeTarget.java b/src/main/java/org/jboss/jandex/TypeParameterTypeTarget.java index 20f0f986..34526504 100644 --- a/src/main/java/org/jboss/jandex/TypeParameterTypeTarget.java +++ b/src/main/java/org/jboss/jandex/TypeParameterTypeTarget.java @@ -14,8 +14,8 @@ public class TypeParameterTypeTarget extends PositionBasedTypeTarget { } @Override - public Kind kind() { - return Kind.TYPE_PARAMETER; + public Usage usage() { + return Usage.TYPE_PARAMETER; } @Override diff --git a/src/main/java/org/jboss/jandex/TypeTarget.java b/src/main/java/org/jboss/jandex/TypeTarget.java index 05b20aa9..2ffbfcb9 100644 --- a/src/main/java/org/jboss/jandex/TypeTarget.java +++ b/src/main/java/org/jboss/jandex/TypeTarget.java @@ -1,7 +1,5 @@ package org.jboss.jandex; -import java.util.Deque; - /** * @author Jason T. Greene */ @@ -9,7 +7,7 @@ public abstract class TypeTarget implements AnnotationTarget { private final AnnotationTarget enclosingTarget; private Type target; - public enum Kind {EMPTY, CLASS_EXTENDS, METHOD_PARAMETER, TYPE_PARAMETER, TYPE_PARAMETER_BOUND, THROWS} + public enum Usage {EMPTY, CLASS_EXTENDS, METHOD_PARAMETER, TYPE_PARAMETER, TYPE_PARAMETER_BOUND, THROWS} TypeTarget(AnnotationTarget enclosingTarget, Type target) { this.enclosingTarget = enclosingTarget; @@ -24,6 +22,11 @@ void setTarget(Type target) { this.target = target; } + @Override + public final Kind kind() { + return Kind.TYPE; + } + public AnnotationTarget enclosingTarget() { return enclosingTarget; } @@ -32,7 +35,7 @@ public Type target() { return target; } - public abstract Kind kind(); + public abstract Usage usage(); public EmptyTypeTarget asEmpty() { throw new IllegalArgumentException("Not an empty type target"); diff --git a/src/test/java/org/jboss/jandex/test/AnnotationInstanceFilterTestCase.java b/src/test/java/org/jboss/jandex/test/AnnotationInstanceFilterTestCase.java index c41fcec8..efda8d0d 100644 --- a/src/test/java/org/jboss/jandex/test/AnnotationInstanceFilterTestCase.java +++ b/src/test/java/org/jboss/jandex/test/AnnotationInstanceFilterTestCase.java @@ -43,8 +43,18 @@ import org.junit.Test; public class AnnotationInstanceFilterTestCase { - private static class Hit implements AnnotationTarget{} - private static class Miss implements AnnotationTarget{} + private static class Hit implements AnnotationTarget{ + @Override + public Kind kind() { + return null; + } + } + private static class Miss implements AnnotationTarget{ + @Override + public Kind kind() { + return null; + } + } @Test public void testFilter() throws Exception { diff --git a/src/test/java/org/jboss/jandex/test/BasicTestCase.java b/src/test/java/org/jboss/jandex/test/BasicTestCase.java index d449b19a..bae46d2c 100644 --- a/src/test/java/org/jboss/jandex/test/BasicTestCase.java +++ b/src/test/java/org/jboss/jandex/test/BasicTestCase.java @@ -49,6 +49,11 @@ import org.junit.Test; public class BasicTestCase { + @Retention(RetentionPolicy.RUNTIME) + public @interface FieldAnnotation { + + } + @Retention(RetentionPolicy.RUNTIME) public @interface TestAnnotation { String name(); @@ -84,6 +89,9 @@ public class DummyClass implements Serializable { void doSomething(int x, long y, Long foo){} void doSomething(int x, long y){} + @FieldAnnotation + private int x; + @MethodAnnotation1 @MethodAnnotation2 @MethodAnnotation4 @@ -129,7 +137,7 @@ public void testWriteRead() throws IOException { index = new IndexReader(new ByteArrayInputStream(baos.toByteArray())).read(); - verifyDummy(index, false); + verifyDummy(index, true); } @Test diff --git a/src/test/java/org/jboss/jandex/test/LoadTest.java b/src/test/java/org/jboss/jandex/test/LoadTest.java index 3192a184..f0343013 100644 --- a/src/test/java/org/jboss/jandex/test/LoadTest.java +++ b/src/test/java/org/jboss/jandex/test/LoadTest.java @@ -4,4 +4,7 @@ * @author Jason T. Greene */ public class LoadTest { + + public void main(String[] blah) { + } } diff --git a/src/test/java/org/jboss/jandex/test/TypeAnnotationTestCase.java b/src/test/java/org/jboss/jandex/test/TypeAnnotationTestCase.java index e71ef6e9..055c0ebd 100644 --- a/src/test/java/org/jboss/jandex/test/TypeAnnotationTestCase.java +++ b/src/test/java/org/jboss/jandex/test/TypeAnnotationTestCase.java @@ -25,6 +25,8 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; @@ -60,6 +62,14 @@ public void testIndexer() throws IOException { Index index = indexer.complete(); + FileOutputStream out = new FileOutputStream("/tmp/blahblahblah"); + IndexWriter writer = new IndexWriter(out); + writer.write(index); + out.close(); + + IndexReader reader = new IndexReader(new FileInputStream("/tmp/blahblahblah")); + index = reader.read(); + for (FieldInfo field : index.getClassByName(DotName.createSimple("org.wildfly.security.TExample")).fields()) { System.out.println(field.type()); }