#431: Add support for char annotation methods

Fixes #431
This commit is contained in:
Artem Godin 2019-10-10 03:11:06 +03:00 committed by Alexey Andreev
parent 4ef231c7fa
commit e46f204b4e
7 changed files with 30 additions and 5 deletions

View File

@ -257,6 +257,8 @@ public class AnnotationDependencyListener extends AbstractDependencyListener {
switch (value.getType()) {
case AnnotationValue.BOOLEAN:
return pe.constant(value.getBoolean() ? 1 : 0);
case AnnotationValue.CHAR:
return pe.constant(value.getChar());
case AnnotationValue.BYTE:
return pe.constant(value.getByte());
case AnnotationValue.SHORT:

View File

@ -93,6 +93,9 @@ public class AnnotationIO {
case AnnotationValue.BOOLEAN:
output.writeUnsigned(value.getBoolean() ? 1 : 0);
break;
case AnnotationValue.CHAR:
output.writeSigned(value.getChar());
break;
case AnnotationValue.BYTE:
output.writeSigned(value.getByte());
break;
@ -139,6 +142,8 @@ public class AnnotationIO {
return new AnnotationValue(readAnnotation(input));
case AnnotationValue.BOOLEAN:
return new AnnotationValue(input.readUnsigned() != 0);
case AnnotationValue.CHAR:
return new AnnotationValue((char) input.readUnsigned());
case AnnotationValue.BYTE:
return new AnnotationValue((byte) input.readSigned());
case AnnotationValue.CLASS:

View File

@ -31,6 +31,7 @@ public class AnnotationValue {
public static final byte LIST = 9;
public static final byte ENUM = 10;
public static final byte ANNOTATION = 11;
public static final byte CHAR = 12;
private byte type;
private Object value;
@ -44,6 +45,11 @@ public class AnnotationValue {
this.value = value;
}
public AnnotationValue(char value) {
this.type = CHAR;
this.value = value;
}
public AnnotationValue(short value) {
this.type = SHORT;
this.value = value;
@ -101,6 +107,13 @@ public class AnnotationValue {
return (Boolean) value;
}
public char getChar() {
if (type != CHAR) {
throw new IllegalStateException("There is no char value");
}
return (Character) value;
}
public byte getByte() {
if (type != BYTE) {
throw new IllegalStateException("There is no byte value");

View File

@ -538,6 +538,8 @@ public class Parser {
return new AnnotationValue((String) value);
} else if (value instanceof Boolean) {
return new AnnotationValue((Boolean) value);
} else if (value instanceof Character) {
return new AnnotationValue((Character) value);
} else if (value instanceof Byte) {
return new AnnotationValue((Byte) value);
} else if (value instanceof Short) {

View File

@ -87,7 +87,7 @@ class AnnotationProxy implements InvocationHandler {
case DOUBLE:
return value.getDouble();
case CHARACTER:
break;
return value.getChar();
}
} else if (type.isObject(String.class)) {
return value.getString();

View File

@ -224,9 +224,9 @@ public class MetaprogrammingTest {
@Test
public void annotationsWork() {
assertEquals(""
+ "foo:23:Object\n"
+ "foo=!:42:String:int\n"
+ "f=!:23\n",
+ "foo:23:Object:?\n"
+ "foo=!:42:String:int:?\n"
+ "f=!:23:^\n",
readAnnotations(WithAnnotations.class, new WithAnnotations()));
}
@ -264,6 +264,7 @@ public class MetaprogrammingTest {
for (Class<?> cls : annot.c()) {
sb.append(':').append(cls.getSimpleName());
}
sb.append(':').append(annot.d());
return sb.toString();
}
@ -534,7 +535,7 @@ public class MetaprogrammingTest {
@TestAnnotation(a = "foo", c = Object.class)
@MetaprogrammingClass
static class WithAnnotations {
@TestAnnotation(c = {})
@TestAnnotation(c = {}, d = '^')
int f;
@TestAnnotation(b = 42, c = { String.class, int.class })

View File

@ -25,4 +25,6 @@ public @interface TestAnnotation {
int b() default 23;
Class<?>[] c();
char d() default '?';
}