Skip to content

Commit

Permalink
API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
n1hility committed Oct 27, 2014
1 parent 03ff523 commit 52eccb6
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 35 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/jboss/jandex/AnnotationTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@
*/
public interface AnnotationTarget {

public enum Kind {CLASS, FIELD, METHOD, METHOD_PARAMETER, TYPE}

Kind kind();

}
81 changes: 80 additions & 1 deletion src/main/java/org/jboss/jandex/AnnotationValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -518,6 +532,11 @@ public Byte value() {
return value;
}

@Override
public Kind kind() {
return Kind.BYTE;
}

public int asInt() {
return value;
}
Expand Down Expand Up @@ -575,6 +594,11 @@ public Character value() {
return value;
}

@Override
public Kind kind() {
return Kind.CHARACTER;
}

public char asChar() {
return value;
}
Expand Down Expand Up @@ -612,6 +636,11 @@ public Double value() {
return value;
}

@Override
public Kind kind() {
return Kind.DOUBLE;
}

public int asInt() {
return (int) value;
}
Expand Down Expand Up @@ -672,6 +701,11 @@ public Float value() {
return value;
}

@Override
public Kind kind() {
return Kind.FLOAT;
}

public int asInt() {
return (int) value;
}
Expand Down Expand Up @@ -730,6 +764,11 @@ public Short value() {
return value;
}

@Override
public Kind kind() {
return Kind.SHORT;
}

public int asInt() {
return value;
}
Expand Down Expand Up @@ -788,6 +827,11 @@ public Integer value() {
return value;
}

@Override
public Kind kind() {
return Kind.INTEGER;
}

public int asInt() {
return value;
}
Expand Down Expand Up @@ -846,6 +890,11 @@ public Long value() {
return value;
}

@Override
public Kind kind() {
return Kind.LONG;
}

public int asInt() {
return (int) value;
}
Expand Down Expand Up @@ -904,6 +953,11 @@ public Boolean value() {
return value;
}

@Override
public Kind kind() {
return Kind.BOOLEAN;
}

public boolean asBoolean() {
return value;
}
Expand All @@ -924,6 +978,11 @@ public String value() {
return value;
}

@Override
public Kind kind() {
return Kind.ENUM;
}

public String asEnum() {
return value;
}
Expand Down Expand Up @@ -968,6 +1027,11 @@ public Type value() {
return type;
}

@Override
public Kind kind() {
return Kind.CLASS;
}

public Type asClass() {
return type;
}
Expand Down Expand Up @@ -1006,6 +1070,11 @@ public AnnotationInstance value() {
return value;
}

@Override
public Kind kind() {
return Kind.NESTED;
}

public AnnotationInstance asNested() {
return value;
}
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jboss/jandex/ClassExtendsTypeTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/jboss/jandex/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jboss/jandex/EmptyTypeTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public boolean isReceiver() {
}

@Override
public final Kind kind() {
return Kind.EMPTY;
public final Usage usage() {
return Usage.EMPTY;
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jboss/jandex/FieldInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public final Type type() {
return internal.type();
}

public final Kind kind() {
return Kind.FIELD;
}

public List<AnnotationInstance> annotations() {
return internal.annotations();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jboss/jandex/IndexWriterV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jboss/jandex/IndexWriterV2Old.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/org/jboss/jandex/Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/org/jboss/jandex/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/jboss/jandex/MethodInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public final class MethodInfo implements AnnotationTarget {


MethodInfo() {
methodInternal = new MethodInternal();
}

MethodInfo(ClassInfo clazz, MethodInternal methodInternal) {
Expand Down Expand Up @@ -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
*
Expand Down
Loading

0 comments on commit 52eccb6

Please sign in to comment.