From e46f204b4e68c423d273bdbb36cf53e725e464be Mon Sep 17 00:00:00 2001 From: Artem Godin Date: Thu, 10 Oct 2019 03:11:06 +0300 Subject: [PATCH] #431: Add support for char annotation methods Fixes #431 --- .../lang/reflect/AnnotationDependencyListener.java | 2 ++ .../src/main/java/org/teavm/cache/AnnotationIO.java | 5 +++++ .../main/java/org/teavm/model/AnnotationValue.java | 13 +++++++++++++ core/src/main/java/org/teavm/parsing/Parser.java | 2 ++ .../impl/reflect/AnnotationProxy.java | 2 +- .../metaprogramming/test/MetaprogrammingTest.java | 9 +++++---- .../teavm/metaprogramming/test/TestAnnotation.java | 2 ++ 7 files changed, 30 insertions(+), 5 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/AnnotationDependencyListener.java b/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/AnnotationDependencyListener.java index b4b1b971e..a57bfc133 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/AnnotationDependencyListener.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/AnnotationDependencyListener.java @@ -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: diff --git a/core/src/main/java/org/teavm/cache/AnnotationIO.java b/core/src/main/java/org/teavm/cache/AnnotationIO.java index 0ebdef589..194ea4af4 100644 --- a/core/src/main/java/org/teavm/cache/AnnotationIO.java +++ b/core/src/main/java/org/teavm/cache/AnnotationIO.java @@ -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: diff --git a/core/src/main/java/org/teavm/model/AnnotationValue.java b/core/src/main/java/org/teavm/model/AnnotationValue.java index 48de05dd0..00d4429bd 100644 --- a/core/src/main/java/org/teavm/model/AnnotationValue.java +++ b/core/src/main/java/org/teavm/model/AnnotationValue.java @@ -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"); diff --git a/core/src/main/java/org/teavm/parsing/Parser.java b/core/src/main/java/org/teavm/parsing/Parser.java index 3907f549f..1a2e85b76 100644 --- a/core/src/main/java/org/teavm/parsing/Parser.java +++ b/core/src/main/java/org/teavm/parsing/Parser.java @@ -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) { diff --git a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/reflect/AnnotationProxy.java b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/reflect/AnnotationProxy.java index 5169c878b..789fd54ff 100644 --- a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/reflect/AnnotationProxy.java +++ b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/reflect/AnnotationProxy.java @@ -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(); diff --git a/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java b/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java index 886e0a3d2..4c94c6eb2 100644 --- a/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java +++ b/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java @@ -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 }) diff --git a/tests/src/test/java/org/teavm/metaprogramming/test/TestAnnotation.java b/tests/src/test/java/org/teavm/metaprogramming/test/TestAnnotation.java index 94a1ed803..e6a364595 100644 --- a/tests/src/test/java/org/teavm/metaprogramming/test/TestAnnotation.java +++ b/tests/src/test/java/org/teavm/metaprogramming/test/TestAnnotation.java @@ -25,4 +25,6 @@ public @interface TestAnnotation { int b() default 23; Class[] c(); + + char d() default '?'; }