Skip to content

Commit

Permalink
Allow creation of mocks (JANDEX-1)
Browse files Browse the repository at this point in the history
  • Loading branch information
n1hility committed Apr 13, 2011
1 parent 8027228 commit 13ddd05
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/jboss/jandex/AnnotationInstance.java
Expand Up @@ -51,6 +51,24 @@ public final class AnnotationInstance {
this.values = values.length > 0 ? values : AnnotationValue.EMPTY_VALUE_ARRAY; this.values = values.length > 0 ? values : AnnotationValue.EMPTY_VALUE_ARRAY;
} }


/**
* Construct a new mock annotation instance.The passed values array must be immutable.
*
* @param name the name of the annotation instance
* @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, AnnotationTarget target, 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 new AnnotationInstance(name, target, values);
}

/** /**
* The name of this annotation in DotName form. * The name of this annotation in DotName form.
* *
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/jboss/jandex/AnnotationValue.java
Expand Up @@ -74,6 +74,59 @@ public abstract class AnnotationValue {
this.name = name; this.name = name;
} }


public static final AnnotationValue createByteValue(String name, byte b) {
return new ByteValue(name, b);
}

public static final AnnotationValue createShortValue(String name, short s) {
return new ShortValue(name, s);
}

public static final AnnotationValue createIntegerValue(String name, int i) {
return new IntegerValue(name, i);
}

public static final AnnotationValue createCharacterValue(String name, char c) {
return new CharacterValue(name, c);
}

public static final AnnotationValue createFloatValue(String name, float f) {
return new FloatValue(name, f);
}

public static final AnnotationValue createDouleValue(String name, double d) {
return new DoubleValue(name, d);
}

public static final AnnotationValue createLongalue(String name, long l) {
return new LongValue(name, l);
}

public static final AnnotationValue createBooleanValue(String name, boolean bool) {
return new BooleanValue(name, bool);
}

public static final AnnotationValue createStringValue(String name, String string) {
return new StringValue(name, string);
}

public static final AnnotationValue createClassValue(String name, Type type) {
return new ClassValue(name, type);
}

public static final AnnotationValue createEnumValue(String name, DotName typeName, String value) {
return new EnumValue(name, typeName, value);
}

public static final AnnotationValue createArrayValue(String name, AnnotationValue[] values) {
return new ArrayValue(name, values);
}

public static final AnnotationValue createNestedAnnotationValue(String name, AnnotationInstance instance)
{
return new NestedAnnotation(name, instance);
}

/** /**
* Returns the name of this value, which is typically the parameter name in the annotation * Returns the name of this value, which is typically the parameter name in the annotation
* declaration. The value may not represent a parameter (e.g an array element member), in * declaration. The value may not represent a parameter (e.g an array element member), in
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/org/jboss/jandex/ClassInfo.java
Expand Up @@ -53,13 +53,28 @@ public final class ClassInfo implements AnnotationTarget {
private final Map<DotName, List<AnnotationInstance>> annotations; private final Map<DotName, List<AnnotationInstance>> annotations;


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


/**
* Constructs a "mock" ClassInfo using the passed values. All passed values MUST NOT BE MODIFIED AFTER THIS CALL.
* Otherwise the resulting object would not conform to the contract outlined above.
*
* @param name the name of this class
* @param superName the name of the parent class
* @param flags the class attributes
* @param interfaces the interfaces this class implements
* @param annotations the annotations on this class
* @return a new mock class representation
*/
public static final ClassInfo create(DotName name, DotName superName, short flags, DotName[] interfaces, Map<DotName, List<AnnotationInstance>> annotations) {
return new ClassInfo(name, superName, flags, interfaces, annotations);
}

public String toString() { public String toString() {
return name.toString(); return name.toString();
} }
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/jboss/jandex/FieldInfo.java
Expand Up @@ -45,6 +45,25 @@ public final class FieldInfo implements AnnotationTarget {
this.flags = flags; this.flags = flags;
} }


/**
* Construct a new mock Field instance.
*
* @param clazz the class declaring the field
* @param name the name of the field
* @param type the Java field type
* @param flags the field attributes
* @return a mock field
*/
public static final FieldInfo create(ClassInfo clazz, String name, Type type, short flags) {
if (clazz == null)
throw new IllegalArgumentException("Clazz can't be null");

if (name == null)
throw new IllegalArgumentException("Name can't be null");

return new FieldInfo(clazz, name, type, flags);
}



/** /**
* Returns the local name of the field * Returns the local name of the field
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/jboss/jandex/MethodInfo.java
Expand Up @@ -46,6 +46,33 @@ public final class MethodInfo implements AnnotationTarget {
this.flags = flags; this.flags = flags;
} }


/**
* Construct a new mock Method instance.
*
* @param clazz the class declaring the field
* @param name the name of the field
* @param args a read only array containing the types of each parameter in parameter order
* @param returnType the return value type
* @param flags the method attributes
* @return a mock field
*/
public static final MethodInfo create(ClassInfo clazz, String name, Type[] args, Type returnType, short flags) {
if (clazz == null)
throw new IllegalArgumentException("Clazz can't be null");

if (name == null)
throw new IllegalArgumentException("Name can't be null");

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

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

return new MethodInfo(clazz, name, args, returnType, flags);
}


/** /**
* Returns the name of this method * Returns the name of this method
* *
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/jboss/jandex/MethodParameterInfo.java
Expand Up @@ -33,12 +33,24 @@ public final class MethodParameterInfo implements AnnotationTarget {
private final MethodInfo method; private final MethodInfo method;
private final short parameter; private final short parameter;


MethodParameterInfo(MethodInfo method,short parameter) MethodParameterInfo(MethodInfo method, short parameter)
{ {
this.method = method; this.method = method;
this.parameter = parameter; this.parameter = parameter;
} }


/**
* Constructs a new mock method parameter info
*
* @param method the method containing this parameter.
* @param parameter the zero based index of this parameter
* @return the new mock parameter info
*/
public static final MethodParameterInfo create(MethodInfo method, short parameter)
{
return new MethodParameterInfo(method, parameter);
}

/** /**
* Returns the method this parameter belongs to. * Returns the method this parameter belongs to.
* *
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/jboss/jandex/Type.java
Expand Up @@ -82,6 +82,16 @@ public static Kind fromOrdinal(int ordinal) {
this.kind = kind; this.kind = kind;
} }


public static final Type create(DotName name, Kind kind) {
if (name == null)
throw new IllegalArgumentException("name can not be null!");

if (kind == null)
throw new IllegalArgumentException("kind can not be null!");

return new Type(name, kind);
}

/** /**
* Returns the name of this type. Primitives and void are returned as the * Returns the name of this type. Primitives and void are returned as the
* Java reserved word (void, boolean, byte, short, char, int, long, float, * Java reserved word (void, boolean, byte, short, char, int, long, float,
Expand Down

0 comments on commit 13ddd05

Please sign in to comment.