Skip to content

Commit

Permalink
Add remaing type annotation variationns.
Browse files Browse the repository at this point in the history
Improve generic type resolution to cover bounds
Improve printing and safety
  • Loading branch information
n1hility committed Oct 4, 2014
1 parent 39bb0e5 commit 7f65ab5
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 140 deletions.
20 changes: 11 additions & 9 deletions src/main/java/org/jboss/jandex/AnnotationInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,18 @@ public List<AnnotationValue> values() {
return Collections.unmodifiableList(Arrays.asList(values));
}

public String toString() {
StringBuilder builder = new StringBuilder("@").append(name.local()).append("(");
for (int i = 0; i < values.length; i++) {
builder.append(values[i]);
if (i < values.length - 1)
builder.append(",");
public String toString(boolean simple) {
StringBuilder builder = new StringBuilder("@").append(simple ? name.local() : name);

if (values.length > 0) {
builder.append("(");
for (int i = 0; i < values.length; i++) {
builder.append(values[i]);
if (i < values.length - 1)
builder.append(",");
}
builder.append(')');
}
builder.append(')');
if (target != null)
builder.append(" on ").append(target);

return builder.toString();
}
Expand Down
46 changes: 18 additions & 28 deletions src/main/java/org/jboss/jandex/ArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,42 +61,32 @@ public DotName name() {

public String toString() {
StringBuilder builder = new StringBuilder();
//appendAnnotations(builder);
// builder.append(component);
// for (int i = 0; i < dimensions; i++) {
// builder.append("[]");
// }
ArrayDeque<Type> types = buildComponentTree(null);

builder.append(types.pollLast());
for (Type type : types) {
ArrayType arrayType = type.asArrayType();
if (arrayType.annotationArray().length > 0) {
builder.append(' ');
arrayType.appendAnnotations(builder);
}
for (int i = 0; i < arrayType.dimensions; i++) {
builder.append("[]");
}
}

appendRootComponent(builder);
appendArraySyntax(builder);

return builder.toString();
}

private ArrayDeque<Type> buildComponentTree(ArrayDeque<Type> tree) {
if (tree == null) {
tree = new ArrayDeque<Type>();
}

tree.add(this);

private void appendRootComponent(StringBuilder builder) {
if (component.kind() == Kind.ARRAY) {
component.asArrayType().buildComponentTree(tree);
component.asArrayType().appendRootComponent(builder);
} else {
tree.add(component);
builder.append(component);
}
}

return tree;
private void appendArraySyntax(StringBuilder builder) {
if (annotationArray().length > 0) {
builder.append(' ');
appendAnnotations(builder);
}
for (int i = 0; i < dimensions; i++) {
builder.append("[]");
}
if (component.kind() == Kind.ARRAY) {
component.asArrayType().appendArraySyntax(builder);
}
}

public int dimensions() {
Expand Down
40 changes: 23 additions & 17 deletions src/main/java/org/jboss/jandex/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.jboss.jandex;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -48,26 +49,26 @@ public final class ClassInfo implements AnnotationTarget {
private final DotName name;
private final short flags;
private final DotName superName;
private final DotName[] interfaces;
private final List<DotName> interfaces;
private final Map<DotName, List<AnnotationInstance>> annotations;
private Collection<Type> interfaceTypes;
private List<Type> interfaceTypes;
private Type superClassType;
private List<Type> typeParameters;
private Collection<MethodInfo> methods;
private Collection<FieldInfo> fields;
private List<MethodInfo> methods;
private List<FieldInfo> fields;

// Not final to allow lazy initialization, immutable once published
private boolean hasNoArgsConstructor;

ClassInfo(DotName name, DotName superName, short flags, DotName[] interfaces, Map<DotName, List<AnnotationInstance>> annotations) {
ClassInfo(DotName name, DotName superName, short flags, List<DotName> interfaces, Map<DotName, List<AnnotationInstance>> annotations) {
this(name, superName, flags, interfaces, annotations, false);
}

ClassInfo(DotName name, DotName superName, short flags, DotName[] interfaces, Map<DotName, List<AnnotationInstance>> annotations, boolean hasNoArgsConstructor) {
ClassInfo(DotName name, DotName superName, short flags, List<DotName> interfaces, Map<DotName, List<AnnotationInstance>> annotations, boolean hasNoArgsConstructor) {
this.name = name;
this.superName = superName;
this.flags = flags;
this.interfaces = interfaces;
this.interfaces = Collections.unmodifiableList(interfaces);
this.annotations = Collections.unmodifiableMap(annotations);
this.hasNoArgsConstructor = hasNoArgsConstructor;
}
Expand All @@ -84,7 +85,7 @@ public final class ClassInfo implements AnnotationTarget {
* @return a new mock class representation
*/
public static ClassInfo create(DotName name, DotName superName, short flags, DotName[] interfaces, Map<DotName, List<AnnotationInstance>> annotations, boolean hasNoArgsConstructor) {
return new ClassInfo(name, superName, flags, interfaces, annotations, hasNoArgsConstructor);
return new ClassInfo(name, superName, flags, Arrays.asList(interfaces), annotations, hasNoArgsConstructor);
}

public String toString() {
Expand All @@ -103,23 +104,28 @@ public final DotName superName() {
return superName;
}

@Deprecated
public final DotName[] interfaces() {
return interfaces;
return interfaces.toArray(new DotName[interfaces.size()]);
}

public final Map<DotName, List<AnnotationInstance>> annotations() {
return annotations;
}

public final Collection<MethodInfo> methods() {
public final List<MethodInfo> methods() {
return methods;
}

public final Collection<FieldInfo> fields() {
public final List<FieldInfo> fields() {
return fields;
}

public final Collection<Type> interfaceTypes() {
public final List<DotName> interfaceNamess() {
return interfaces;
}

public final List<Type> interfaceTypes() {
return interfaceTypes;
}

Expand Down Expand Up @@ -149,22 +155,22 @@ void setHasNoArgsConstructor(boolean hasNoArgsConstructor) {
}

void setFields(List<FieldInfo> fields) {
this.fields = fields;
this.fields = Collections.unmodifiableList(fields);
}

void setMethods(List<MethodInfo> methods) {
this.methods = methods;
this.methods = Collections.unmodifiableList(methods);
}

void setSuperClassType(Type superClassType) {
this.superClassType = superClassType;
}

void setInterfaceTypes(Collection<Type> interfaceTypes) {
this.interfaceTypes = interfaceTypes;
void setInterfaceTypes(List<Type> interfaceTypes) {
this.interfaceTypes = Collections.unmodifiableList(interfaceTypes);
}

void setTypeParameters(List<Type> typeParameters) {
this.typeParameters = typeParameters;
this.typeParameters = Collections.unmodifiableList(typeParameters);
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/jboss/jandex/ClassType.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
public final class ClassType extends Type {

public static final ClassType OBJECT_TYPE = new ClassType(DotName.OBJECT_NAME);

ClassType(DotName name) {
this(name, null);
}
Expand All @@ -44,4 +46,8 @@ public ClassType asClassType() {
Type copyType(AnnotationInstance[] newAnnotations) {
return new ClassType(name(), newAnnotations);
}

ParameterizedType toParameterizedType() {
return new ParameterizedType(name(), null, null, annotationArray());
}
}
40 changes: 30 additions & 10 deletions src/main/java/org/jboss/jandex/GenericSignatureParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ public String toString() {

static class MethodSignature {
private final Type[] typeParameters;
private final Type[] methodParameters;
private final List<Type> methodParameters;
private final Type returnType;
private final Type[] throwables;

private MethodSignature(Type[] typeParameters, Type[] methodParameters, Type returnType, Type[] throwables) {
private MethodSignature(Type[] typeParameters, List<Type> methodParameters, Type returnType, Type[] throwables) {
this.typeParameters = typeParameters;
this.methodParameters = methodParameters;
this.returnType = returnType;
Expand All @@ -178,7 +178,7 @@ public Type returnType() {
return returnType;
}

public Type[] methodParameters() {
public List<Type> methodParameters() {
return methodParameters;
}

Expand All @@ -199,11 +199,11 @@ public String toString() {
}

builder.append(returnType).append(" (");
if (methodParameters.length > 0) {
builder.append(methodParameters[0]);
if (methodParameters.size() > 0) {
builder.append(methodParameters.get(0));

for (int i = 1; i < methodParameters.length; i++) {
builder.append(", ").append(methodParameters[i]);
for (int i = 1; i < methodParameters.size(); i++) {
builder.append(", ").append(methodParameters.get(i));
}
}
builder.append(')');
Expand Down Expand Up @@ -279,7 +279,7 @@ MethodSignature parseMethodSignature(String signature) {
throwables.add(parseReferenceType());
}

return new MethodSignature(typeParameters, parameters.toArray(new Type[parameters.size()]), returnType,
return new MethodSignature(typeParameters, parameters, returnType,
throwables.toArray(new Type[throwables.size()]));

}
Expand Down Expand Up @@ -446,20 +446,40 @@ private void resolveTypeList(ArrayList<Type> list) {
Type type = resolveType(list.get(i));
if (type != null) {
list.set(i, type);
typeParameters.put(type.asTypeVariable().identifier(), type.asTypeVariable());
}
}
}

private Type resolveType(Type type) {
if (type instanceof TypeVariable) {
TypeVariable typeVariable = resolveBounds(type);

return typeVariable != type ? typeVariable : null;
}


if (! (type instanceof UnresolvedTypeVariable)) {
return null;
}

return resolveType(((UnresolvedTypeVariable) type).identifier());
}

private Type resolveType(String identifier) {
Type ret = elementTypeParameters.get(identifier);
private TypeVariable resolveBounds(Type type) {
TypeVariable typeVariable = type.asTypeVariable();
Type[] bounds = typeVariable.boundArray();
for (int i = 0; i < bounds.length; i++) {
Type newType = resolveType(bounds[i]);
if (newType != null && newType != bounds[i]) {
typeVariable = typeVariable.copyType(i, newType);
}
}
return typeVariable;
}

private TypeVariable resolveType(String identifier) {
TypeVariable ret = elementTypeParameters.get(identifier);
return ret == null ? classTypeParameteres.get(identifier) : ret;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jboss/jandex/IndexReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ private Index readClasses(PackedDataInputStream stream, byte version) throws IOE
boolean hasNoArgsConstructor = version >= 3 && stream.readBoolean();

int numIntfs = stream.readPackedU32();
DotName[] interfaces = new DotName[numIntfs];
List<DotName> interfaces = new ArrayList<DotName>(numIntfs);
for (int j = 0; j < numIntfs; j++) {
interfaces[j] = classTable[stream.readPackedU32()];
interfaces.add(classTable[stream.readPackedU32()]);
}

Map<DotName, List<AnnotationInstance>> annotations = new HashMap<DotName, List<AnnotationInstance>>();
Expand Down Expand Up @@ -263,9 +263,9 @@ private AnnotationValue[] readAnnotationValues(PackedDataInputStream stream) thr
private MethodInfo readMethod(ClassInfo clazz, PackedDataInputStream stream) throws IOException {
String name = stringTable[stream.readPackedU32()];
int numArgs = stream.readPackedU32();
Type args[] = new Type[numArgs];
List<Type> args = new ArrayList<Type>(numArgs);
for (int i = 0; i < numArgs; i ++) {
args[i] = readType(stream);
args.add(readType(stream));
}
Type returnType = readType(stream);
short flags = stream.readShort();
Expand Down
Loading

0 comments on commit 7f65ab5

Please sign in to comment.