From 13ddd0559fab8d8df41a4d643356adf66dab119a Mon Sep 17 00:00:00 2001 From: "Jason T. Greene" Date: Wed, 13 Apr 2011 16:30:47 -0500 Subject: [PATCH] Allow creation of mocks (JANDEX-1) --- .../org/jboss/jandex/AnnotationInstance.java | 18 +++++++ .../org/jboss/jandex/AnnotationValue.java | 53 +++++++++++++++++++ src/main/java/org/jboss/jandex/ClassInfo.java | 17 +++++- src/main/java/org/jboss/jandex/FieldInfo.java | 19 +++++++ .../java/org/jboss/jandex/MethodInfo.java | 27 ++++++++++ .../org/jboss/jandex/MethodParameterInfo.java | 14 ++++- src/main/java/org/jboss/jandex/Type.java | 10 ++++ 7 files changed, 156 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jboss/jandex/AnnotationInstance.java b/src/main/java/org/jboss/jandex/AnnotationInstance.java index b694c117..404fe836 100644 --- a/src/main/java/org/jboss/jandex/AnnotationInstance.java +++ b/src/main/java/org/jboss/jandex/AnnotationInstance.java @@ -51,6 +51,24 @@ public final class AnnotationInstance { 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. * diff --git a/src/main/java/org/jboss/jandex/AnnotationValue.java b/src/main/java/org/jboss/jandex/AnnotationValue.java index a80a8f28..c1115a88 100644 --- a/src/main/java/org/jboss/jandex/AnnotationValue.java +++ b/src/main/java/org/jboss/jandex/AnnotationValue.java @@ -74,6 +74,59 @@ public abstract class AnnotationValue { 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 * declaration. The value may not represent a parameter (e.g an array element member), in diff --git a/src/main/java/org/jboss/jandex/ClassInfo.java b/src/main/java/org/jboss/jandex/ClassInfo.java index 71399446..af638eaf 100644 --- a/src/main/java/org/jboss/jandex/ClassInfo.java +++ b/src/main/java/org/jboss/jandex/ClassInfo.java @@ -53,13 +53,28 @@ public final class ClassInfo implements AnnotationTarget { private final Map> annotations; ClassInfo(DotName name, DotName superName, short flags, DotName[] interfaces, Map> annotations) { - this.name = name;; + this.name = name; this.superName = superName; this.flags = flags; this.interfaces = interfaces; 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> annotations) { + return new ClassInfo(name, superName, flags, interfaces, annotations); + } + public String toString() { return name.toString(); } diff --git a/src/main/java/org/jboss/jandex/FieldInfo.java b/src/main/java/org/jboss/jandex/FieldInfo.java index 36d5ec00..e92d3e70 100644 --- a/src/main/java/org/jboss/jandex/FieldInfo.java +++ b/src/main/java/org/jboss/jandex/FieldInfo.java @@ -45,6 +45,25 @@ public final class FieldInfo implements AnnotationTarget { 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 diff --git a/src/main/java/org/jboss/jandex/MethodInfo.java b/src/main/java/org/jboss/jandex/MethodInfo.java index 2c920d6c..1aeee45f 100644 --- a/src/main/java/org/jboss/jandex/MethodInfo.java +++ b/src/main/java/org/jboss/jandex/MethodInfo.java @@ -46,6 +46,33 @@ public final class MethodInfo implements AnnotationTarget { 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 * diff --git a/src/main/java/org/jboss/jandex/MethodParameterInfo.java b/src/main/java/org/jboss/jandex/MethodParameterInfo.java index 710a4436..bdcfb211 100644 --- a/src/main/java/org/jboss/jandex/MethodParameterInfo.java +++ b/src/main/java/org/jboss/jandex/MethodParameterInfo.java @@ -33,12 +33,24 @@ public final class MethodParameterInfo implements AnnotationTarget { private final MethodInfo method; private final short parameter; - MethodParameterInfo(MethodInfo method,short parameter) + MethodParameterInfo(MethodInfo method, short parameter) { this.method = method; 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. * diff --git a/src/main/java/org/jboss/jandex/Type.java b/src/main/java/org/jboss/jandex/Type.java index 3ec36170..c7a46a4f 100644 --- a/src/main/java/org/jboss/jandex/Type.java +++ b/src/main/java/org/jboss/jandex/Type.java @@ -82,6 +82,16 @@ public static Kind fromOrdinal(int ordinal) { 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 * Java reserved word (void, boolean, byte, short, char, int, long, float,