Annotations with string attributes reflection
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 02 Dec 2012 14:01:17 +0100
branchreflection
changeset 23784ffc347412d
parent 236 d97770281580
child 238 5ab1f0890a42
Annotations with string attributes
emul/src/main/java/java/lang/Class.java
javap/src/main/java/org/apidesign/javap/AnnotationParser.java
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java
vm/src/test/java/org/apidesign/vm4brwsr/Classes.java
vm/src/test/java/org/apidesign/vm4brwsr/ClassesNamer.java
     1.1 --- a/emul/src/main/java/java/lang/Class.java	Sun Dec 02 12:39:51 2012 +0100
     1.2 +++ b/emul/src/main/java/java/lang/Class.java	Sun Dec 02 14:01:17 2012 +0100
     1.3 @@ -770,10 +770,6 @@
     1.4              throw new ClassCastException(this.toString());
     1.5      }
     1.6  
     1.7 -    /**
     1.8 -     * @throws NullPointerException {@inheritDoc}
     1.9 -     * @since 1.5
    1.10 -     */
    1.11      @JavaScriptBody(args = { "self", "ac" }, 
    1.12          body = 
    1.13            "if (self.anno) {"
    1.14 @@ -783,6 +779,10 @@
    1.15      private Object getAnnotationData(Class<?> annotationClass) {
    1.16          throw new UnsupportedOperationException();
    1.17      }
    1.18 +    /**
    1.19 +     * @throws NullPointerException {@inheritDoc}
    1.20 +     * @since 1.5
    1.21 +     */
    1.22      public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
    1.23          Object data = getAnnotationData(annotationClass);
    1.24          return data == null ? null : AnnotationImpl.create(annotationClass, data);
     2.1 --- a/javap/src/main/java/org/apidesign/javap/AnnotationParser.java	Sun Dec 02 12:39:51 2012 +0100
     2.2 +++ b/javap/src/main/java/org/apidesign/javap/AnnotationParser.java	Sun Dec 02 14:01:17 2012 +0100
     2.3 @@ -34,7 +34,10 @@
     2.4   * @author Jaroslav Tulach <jtulach@netbeans.org>
     2.5   */
     2.6  public class AnnotationParser {
     2.7 -    protected AnnotationParser() {
     2.8 +    private final boolean textual;
     2.9 +    
    2.10 +    protected AnnotationParser(boolean textual) {
    2.11 +        this.textual = textual;
    2.12      }
    2.13  
    2.14      protected void visitAnnotationStart(String type) throws IOException {
    2.15 @@ -90,13 +93,17 @@
    2.16              readAnno(dis, cd);
    2.17          } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N
    2.18              int primitive = dis.readUnsignedShort();
    2.19 +            String val = cd.stringValue(primitive, textual);
    2.20              String attrType;
    2.21              if (type == 's') {
    2.22                  attrType = "Ljava_lang_String";
    2.23 +                if (textual) {
    2.24 +                    val = '"' + val + '"';
    2.25 +                }
    2.26              } else {
    2.27                  attrType = "" + type;
    2.28              }
    2.29 -            visitAttr(typeName, attrName, attrType, cd.StringValue(primitive));
    2.30 +            visitAttr(typeName, attrName, attrType, val);
    2.31          } else if (type == 'c') {
    2.32              int cls = dis.readUnsignedShort();
    2.33          } else if (type == '[') {
     3.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Dec 02 12:39:51 2012 +0100
     3.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Dec 02 14:01:17 2012 +0100
     3.3 @@ -994,6 +994,10 @@
     3.4          }
     3.5          final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;";
     3.6          class P extends AnnotationParser {
     3.7 +            public P() {
     3.8 +                super(false);
     3.9 +            }
    3.10 +            
    3.11              int cnt;
    3.12              String[] args = new String[30];
    3.13              String body;
    3.14 @@ -1055,7 +1059,7 @@
    3.15          final String[] values = new String[attrNames.length];
    3.16          final boolean[] found = { false };
    3.17          final String jvmType = "L" + className.replace('.', '/') + ";";
    3.18 -        AnnotationParser ap = new AnnotationParser() {
    3.19 +        AnnotationParser ap = new AnnotationParser(false) {
    3.20              @Override
    3.21              protected void visitAttr(String type, String attr, String at, String value) {
    3.22                  if (type.equals(jvmType)) {
    3.23 @@ -1093,11 +1097,15 @@
    3.24      }
    3.25  
    3.26      private static void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException {
    3.27 -        AnnotationParser ap = new AnnotationParser() {
    3.28 +        AnnotationParser ap = new AnnotationParser(true) {
    3.29 +            int anno;
    3.30              int cnt;
    3.31              
    3.32              @Override
    3.33              protected void visitAnnotationStart(String type) throws IOException {
    3.34 +                if (anno++ > 0) {
    3.35 +                    out.append(",");
    3.36 +                }
    3.37                  out.append('"').append(type).append("\" : {\n");
    3.38                  cnt = 0;
    3.39              }
     4.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java	Sun Dec 02 12:39:51 2012 +0100
     4.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java	Sun Dec 02 14:01:17 2012 +0100
     4.3 @@ -77,6 +77,9 @@
     4.4      @Test public void jsAnnotation() throws Exception {
     4.5          assertExec("Check class annotation", Classes.class, "getMarkerI", Double.valueOf(10));
     4.6      }
     4.7 +    @Test public void jsStringAnnotation() throws Exception {
     4.8 +        assertExec("Check class annotation", Classes.class, "getNamerLjava_lang_StringZ", "my text", true);
     4.9 +    }
    4.10      
    4.11      private static CharSequence codeSeq;
    4.12      private static Invocable code;
     5.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/Classes.java	Sun Dec 02 12:39:51 2012 +0100
     5.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Classes.java	Sun Dec 02 14:01:17 2012 +0100
     5.3 @@ -18,6 +18,7 @@
     5.4  package org.apidesign.vm4brwsr;
     5.5  
     5.6  import java.io.IOException;
     5.7 +import java.lang.annotation.Annotation;
     5.8  import java.net.MalformedURLException;
     5.9  
    5.10  /**
    5.11 @@ -25,6 +26,7 @@
    5.12   * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.13   */
    5.14  @ClassesMarker(number = 10)
    5.15 +@ClassesNamer(name = "my text")
    5.16  public class Classes {
    5.17      public static boolean equalsClassesOfExceptions() {
    5.18          return MalformedURLException.class.getSuperclass() == IOException.class;
    5.19 @@ -62,4 +64,16 @@
    5.20          ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class);
    5.21          return cm == null ? -1 : cm.number();
    5.22      }
    5.23 +    public static String getNamer(boolean direct) {
    5.24 +        if (direct) {
    5.25 +            ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class);
    5.26 +            return cm == null ? null : cm.name();
    5.27 +        }
    5.28 +        for (Annotation a : Classes.class.getAnnotations()) {
    5.29 +            if (a instanceof ClassesNamer) {
    5.30 +                return ((ClassesNamer)a).name();
    5.31 +            }
    5.32 +        }
    5.33 +        return null;
    5.34 +    }
    5.35  }
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ClassesNamer.java	Sun Dec 02 14:01:17 2012 +0100
     6.3 @@ -0,0 +1,30 @@
     6.4 +/**
     6.5 + * Back 2 Browser Bytecode Translator
     6.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     6.7 + *
     6.8 + * This program is free software: you can redistribute it and/or modify
     6.9 + * it under the terms of the GNU General Public License as published by
    6.10 + * the Free Software Foundation, version 2 of the License.
    6.11 + *
    6.12 + * This program is distributed in the hope that it will be useful,
    6.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    6.15 + * GNU General Public License for more details.
    6.16 + *
    6.17 + * You should have received a copy of the GNU General Public License
    6.18 + * along with this program. Look for COPYING file in the top folder.
    6.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    6.20 + */
    6.21 +package org.apidesign.vm4brwsr;
    6.22 +
    6.23 +import java.lang.annotation.Retention;
    6.24 +import java.lang.annotation.RetentionPolicy;
    6.25 +
    6.26 +/**
    6.27 + *
    6.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    6.29 + */
    6.30 +@Retention(RetentionPolicy.RUNTIME)
    6.31 +public @interface ClassesNamer {
    6.32 +    String name();
    6.33 +}