Permalink
Browse files

Allow creation of mocks (JANDEX-1)

  • Loading branch information...
1 parent 8027228 commit 13ddd0559fab8d8df41a4d643356adf66dab119a @n1hility n1hility committed Apr 13, 2011
@@ -52,6 +52,24 @@
}
/**
+ * 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.
*
* @return the name of this annotation
@@ -74,6 +74,59 @@
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
@@ -53,13 +53,28 @@
private final 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.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<DotName, List<AnnotationInstance>> annotations) {
+ return new ClassInfo(name, superName, flags, interfaces, annotations);
+ }
+
public String toString() {
return name.toString();
}
@@ -45,6 +45,25 @@
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
@@ -47,6 +47,33 @@
}
/**
+ * 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
*
* @return the name of the method
@@ -33,13 +33,25 @@
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.
*
* @return the declaring Java method
@@ -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,

0 comments on commit 13ddd05

Please sign in to comment.