# HG changeset patch # User Jaroslav Tulach # Date 1354453277 -3600 # Node ID 84ffc347412d5e4e3061b56a422c19b532c17588 # Parent d97770281580355e65399a0f0d6adb03e2c681b7 Annotations with string attributes diff -r d97770281580 -r 84ffc347412d emul/src/main/java/java/lang/Class.java --- a/emul/src/main/java/java/lang/Class.java Sun Dec 02 12:39:51 2012 +0100 +++ b/emul/src/main/java/java/lang/Class.java Sun Dec 02 14:01:17 2012 +0100 @@ -770,10 +770,6 @@ throw new ClassCastException(this.toString()); } - /** - * @throws NullPointerException {@inheritDoc} - * @since 1.5 - */ @JavaScriptBody(args = { "self", "ac" }, body = "if (self.anno) {" @@ -783,6 +779,10 @@ private Object getAnnotationData(Class annotationClass) { throw new UnsupportedOperationException(); } + /** + * @throws NullPointerException {@inheritDoc} + * @since 1.5 + */ public A getAnnotation(Class annotationClass) { Object data = getAnnotationData(annotationClass); return data == null ? null : AnnotationImpl.create(annotationClass, data); diff -r d97770281580 -r 84ffc347412d javap/src/main/java/org/apidesign/javap/AnnotationParser.java --- a/javap/src/main/java/org/apidesign/javap/AnnotationParser.java Sun Dec 02 12:39:51 2012 +0100 +++ b/javap/src/main/java/org/apidesign/javap/AnnotationParser.java Sun Dec 02 14:01:17 2012 +0100 @@ -34,7 +34,10 @@ * @author Jaroslav Tulach */ public class AnnotationParser { - protected AnnotationParser() { + private final boolean textual; + + protected AnnotationParser(boolean textual) { + this.textual = textual; } protected void visitAnnotationStart(String type) throws IOException { @@ -90,13 +93,17 @@ readAnno(dis, cd); } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N int primitive = dis.readUnsignedShort(); + String val = cd.stringValue(primitive, textual); String attrType; if (type == 's') { attrType = "Ljava_lang_String"; + if (textual) { + val = '"' + val + '"'; + } } else { attrType = "" + type; } - visitAttr(typeName, attrName, attrType, cd.StringValue(primitive)); + visitAttr(typeName, attrName, attrType, val); } else if (type == 'c') { int cls = dis.readUnsignedShort(); } else if (type == '[') { diff -r d97770281580 -r 84ffc347412d vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Sun Dec 02 12:39:51 2012 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Sun Dec 02 14:01:17 2012 +0100 @@ -994,6 +994,10 @@ } final String jvmType = "Lorg/apidesign/bck2brwsr/core/JavaScriptBody;"; class P extends AnnotationParser { + public P() { + super(false); + } + int cnt; String[] args = new String[30]; String body; @@ -1055,7 +1059,7 @@ final String[] values = new String[attrNames.length]; final boolean[] found = { false }; final String jvmType = "L" + className.replace('.', '/') + ";"; - AnnotationParser ap = new AnnotationParser() { + AnnotationParser ap = new AnnotationParser(false) { @Override protected void visitAttr(String type, String attr, String at, String value) { if (type.equals(jvmType)) { @@ -1093,11 +1097,15 @@ } private static void generateAnno(ClassData cd, final Appendable out, byte[] data) throws IOException { - AnnotationParser ap = new AnnotationParser() { + AnnotationParser ap = new AnnotationParser(true) { + int anno; int cnt; @Override protected void visitAnnotationStart(String type) throws IOException { + if (anno++ > 0) { + out.append(","); + } out.append('"').append(type).append("\" : {\n"); cnt = 0; } diff -r d97770281580 -r 84ffc347412d vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java Sun Dec 02 12:39:51 2012 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java Sun Dec 02 14:01:17 2012 +0100 @@ -77,6 +77,9 @@ @Test public void jsAnnotation() throws Exception { assertExec("Check class annotation", Classes.class, "getMarkerI", Double.valueOf(10)); } + @Test public void jsStringAnnotation() throws Exception { + assertExec("Check class annotation", Classes.class, "getNamerLjava_lang_StringZ", "my text", true); + } private static CharSequence codeSeq; private static Invocable code; diff -r d97770281580 -r 84ffc347412d vm/src/test/java/org/apidesign/vm4brwsr/Classes.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/Classes.java Sun Dec 02 12:39:51 2012 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Classes.java Sun Dec 02 14:01:17 2012 +0100 @@ -18,6 +18,7 @@ package org.apidesign.vm4brwsr; import java.io.IOException; +import java.lang.annotation.Annotation; import java.net.MalformedURLException; /** @@ -25,6 +26,7 @@ * @author Jaroslav Tulach */ @ClassesMarker(number = 10) +@ClassesNamer(name = "my text") public class Classes { public static boolean equalsClassesOfExceptions() { return MalformedURLException.class.getSuperclass() == IOException.class; @@ -62,4 +64,16 @@ ClassesMarker cm = Classes.class.getAnnotation(ClassesMarker.class); return cm == null ? -1 : cm.number(); } + public static String getNamer(boolean direct) { + if (direct) { + ClassesNamer cm = Classes.class.getAnnotation(ClassesNamer.class); + return cm == null ? null : cm.name(); + } + for (Annotation a : Classes.class.getAnnotations()) { + if (a instanceof ClassesNamer) { + return ((ClassesNamer)a).name(); + } + } + return null; + } } diff -r d97770281580 -r 84ffc347412d vm/src/test/java/org/apidesign/vm4brwsr/ClassesNamer.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ClassesNamer.java Sun Dec 02 14:01:17 2012 +0100 @@ -0,0 +1,30 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * + * @author Jaroslav Tulach + */ +@Retention(RetentionPolicy.RUNTIME) +public @interface ClassesNamer { + String name(); +}