You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2018/01/04 11:04:27 UTC

[GitHub] JaroslavTulach closed pull request #4: Make @JavaScriptResource repeatable

JaroslavTulach closed pull request #4: Make @JavaScriptResource repeatable
URL: https://github.com/apache/incubator-netbeans-html4j/pull/4
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/boot/src/main/java/net/java/html/js/JavaScriptResource.java b/boot/src/main/java/net/java/html/js/JavaScriptResource.java
index 8af4cbd..4f94bc3 100644
--- a/boot/src/main/java/net/java/html/js/JavaScriptResource.java
+++ b/boot/src/main/java/net/java/html/js/JavaScriptResource.java
@@ -19,6 +19,7 @@
 package net.java.html.js;
 
 import java.lang.annotation.ElementType;
+import java.lang.annotation.Repeatable;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
@@ -38,9 +39,28 @@
  */
 @Retention(RetentionPolicy.CLASS)
 @Target(ElementType.TYPE)
+@Repeatable(JavaScriptResource.Group.class)
 public @interface JavaScriptResource {
     /** The JavaScript file to load in before associated class can execute.
      * @return relative path with respect to the annotated class
      */
     public String value();
+
+    /** Represents a group of resources to load. When initializing element
+     * annotated by {@code Group} annotation, load all resources, one by one, in the
+     * order they appear in the {@link Group#value() array}.
+     *
+     * @since 1.6
+     * @deprecated Don't use directly. Repeat the {@link JavaScriptResource} annotation.
+     */
+    @Retention(RetentionPolicy.CLASS)
+    @Target(ElementType.TYPE)
+    @Deprecated
+    public static @interface Group {
+        /** Multiple instances of {@link JavaScriptResource} to load.
+         *
+         * @return array of resources to load
+         */
+        JavaScriptResource[] value();
+    }
 }
diff --git a/boot/src/main/java/org/netbeans/html/boot/impl/FnUtils.java b/boot/src/main/java/org/netbeans/html/boot/impl/FnUtils.java
index e41519e..3a42327 100644
--- a/boot/src/main/java/org/netbeans/html/boot/impl/FnUtils.java
+++ b/boot/src/main/java/org/netbeans/html/boot/impl/FnUtils.java
@@ -117,7 +117,8 @@ protected CharSequence callMethod(
     private static final class FindInClass extends ClassVisitor {
         private String name;
         private int found;
-        private String resource;
+        private int resourcesCnt = 0;
+        private final String[] resources = new String[256];
 
         public FindInClass(ClassLoader l, ClassVisitor cv) {
             super(Opcodes.ASM4, cv);
@@ -135,6 +136,9 @@ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
             if ("Lnet/java/html/js/JavaScriptResource;".equals(desc)) {
                 return new LoadResource(del);
             }
+            if ("Lnet/java/html/js/JavaScriptResource$Group;".equals(desc)) {
+                return new LoadResource(del);
+            }
             return del;
         }
 
@@ -259,7 +263,12 @@ private boolean generateBody(boolean hasCode) {
                 Label noPresenter = new Label();
                 super.visitInsn(Opcodes.DUP);
                 super.visitJumpInsn(Opcodes.IFNULL, noPresenter);
-                if (resource != null) {
+                int cnt = resourcesCnt;
+                while (cnt > 0) {
+                    String resource = resources[--cnt];
+                    if (resource == null) {
+                        continue;
+                    }
                     super.visitLdcInsn(Type.getObjectType(FindInClass.this.name));
                     super.visitLdcInsn(resource);
                     super.visitMethodInsn(Opcodes.INVOKESTATIC,
@@ -533,13 +542,23 @@ public void visit(String attrName, Object value) {
                 super.visit(attrName, value);
                 String relPath = (String) value;
                 if (relPath.startsWith("/")) {
-                    resource = relPath;
+                    resources[resourcesCnt++] = relPath;
                 } else {
                     int last = name.lastIndexOf('/');
                     String fullPath = name.substring(0, last + 1) + relPath;
-                    resource = fullPath;
+                    resources[resourcesCnt++] = fullPath;
                 }
             }
+
+            @Override
+            public AnnotationVisitor visitArray(String name) {
+                return this;
+            }
+
+            @Override
+            public AnnotationVisitor visitAnnotation(String name, String desc) {
+                return this;
+            }
         }
     }
 
diff --git a/boot/src/main/java/org/netbeans/html/boot/impl/JavaScriptProcesor.java b/boot/src/main/java/org/netbeans/html/boot/impl/JavaScriptProcesor.java
index 42b6136..cac5a5c 100644
--- a/boot/src/main/java/org/netbeans/html/boot/impl/JavaScriptProcesor.java
+++ b/boot/src/main/java/org/netbeans/html/boot/impl/JavaScriptProcesor.java
@@ -75,6 +75,7 @@
         Set<String> set = new HashSet<String>();
         set.add(JavaScriptBody.class.getName());
         set.add(JavaScriptResource.class.getName());
+        set.add(JavaScriptResource.Group.class.getName());
         return set;
     }
 
@@ -140,44 +141,16 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
             if (r == null) {
                 continue;
             }
-            final String res;
-            if (r.value().startsWith("/")) {
-                res = r.value().substring(1);
-            } else {
-                res = findPkg(e).replace('.', '/') + "/" + r.value();
-            }
-
-            try {
-                FileObject os = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", res);
-                os.openInputStream().close();
-            } catch (IOException ex1) {
-                try {
-                    FileObject os2 = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", res);
-                    os2.openInputStream().close();
-                } catch (IOException ex2) {
-                    try {
-                        FileObject os3 = processingEnv.getFiler().getResource(StandardLocation.CLASS_PATH, "", res);
-                        os3.openInputStream().close();
-                    } catch (IOException ex3) {
-                        msg.printMessage(Diagnostic.Kind.ERROR, "Cannot find resource " + res, e);
-                    }
-                }
-            }
+            checkJavaScriptBody(r, e, msg);
+        }
 
-            boolean found = false;
-            for (Element mthod : e.getEnclosedElements()) {
-                if (mthod.getKind() != ElementKind.METHOD) {
-                    continue;
-                }
-                if (mthod.getAnnotation(JavaScriptBody.class) != null) {
-                    found = true;
-                    break;
-                }
+        for (Element e : roundEnv.getElementsAnnotatedWith(JavaScriptResource.Group.class)) {
+            JavaScriptResource.Group g = e.getAnnotation(JavaScriptResource.Group.class);
+            if (g == null) {
+                continue;
             }
-            if (!found) {
-                msg.printMessage(Diagnostic.Kind.ERROR, "At least one method needs @JavaScriptBody annotation. "
-                    + "Otherwise it is not guaranteed the resource will ever be loaded,", e
-                );
+            for (JavaScriptResource r : g.value()) {
+                checkJavaScriptBody(r, e, msg);
             }
         }
 
@@ -189,6 +162,48 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
         return true;
     }
 
+    private void checkJavaScriptBody(JavaScriptResource r, Element e, final Messager msg) {
+        final String res;
+        if (r.value().startsWith("/")) {
+            res = r.value().substring(1);
+        } else {
+            res = findPkg(e).replace('.', '/') + "/" + r.value();
+        }
+
+        try {
+            FileObject os = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", res);
+            os.openInputStream().close();
+        } catch (IOException ex1) {
+            try {
+                FileObject os2 = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", res);
+                os2.openInputStream().close();
+            } catch (IOException ex2) {
+                try {
+                    FileObject os3 = processingEnv.getFiler().getResource(StandardLocation.CLASS_PATH, "", res);
+                    os3.openInputStream().close();
+                } catch (IOException ex3) {
+                    msg.printMessage(Diagnostic.Kind.ERROR, "Cannot find resource " + res, e);
+                }
+            }
+        }
+
+        boolean found = false;
+        for (Element mthod : e.getEnclosedElements()) {
+            if (mthod.getKind() != ElementKind.METHOD) {
+                continue;
+            }
+            if (mthod.getAnnotation(JavaScriptBody.class) != null) {
+                found = true;
+                break;
+            }
+        }
+        if (!found) {
+            msg.printMessage(Diagnostic.Kind.ERROR, "At least one method needs @JavaScriptBody annotation. "
+                    + "Otherwise it is not guaranteed the resource will ever be loaded,", e
+            );
+        }
+    }
+
     @Override
     public Iterable<? extends Completion> getCompletions(Element e,
         AnnotationMirror annotation, ExecutableElement member, String userText
diff --git a/json-tck/pom.xml b/json-tck/pom.xml
index fbb5870..108a556 100644
--- a/json-tck/pom.xml
+++ b/json-tck/pom.xml
@@ -55,6 +55,15 @@
                   <includeDependencySources>true</includeDependencySources>
               </configuration>
           </plugin>
+         <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-compiler-plugin</artifactId>
+            <version>2.3.2</version>
+            <configuration>
+               <source>1.8</source>
+               <target>1.8</target>
+            </configuration>
+         </plugin>
       </plugins>
   </build>
   <dependencies>
diff --git a/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java b/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java
index e79d47b..338a9e8 100644
--- a/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java
+++ b/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java
@@ -433,6 +433,11 @@ public void globalStringAvailable() throws Exception {
         assertEquals("HTML/Java", Bodies.readGlobalString());
     }
 
+    @KOTest
+    public void orderOfJavaScriptResources() throws Exception {
+        assertEquals("Hello World!", ResourceOrder.helloWorld());
+    }
+
     @KOTest
     public void globalValueInCallbackAvailable() throws Exception {
         final String[] value = { null, null };
diff --git a/json-tck/src/main/java/net/java/html/js/tests/ResourceOrder.java b/json-tck/src/main/java/net/java/html/js/tests/ResourceOrder.java
new file mode 100644
index 0000000..d9244c9
--- /dev/null
+++ b/json-tck/src/main/java/net/java/html/js/tests/ResourceOrder.java
@@ -0,0 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package net.java.html.js.tests;
+
+import net.java.html.js.JavaScriptBody;
+import net.java.html.js.JavaScriptResource;
+
+@JavaScriptResource("initArray.js")
+@JavaScriptResource("addHello.js")
+@JavaScriptResource("addWorld.js")
+public class ResourceOrder {
+    @JavaScriptBody(args = {  }, body = "return testArray.join(' ');")
+    public static native String helloWorld();
+}
diff --git a/json-tck/src/main/resources/net/java/html/js/tests/addHello.js b/json-tck/src/main/resources/net/java/html/js/tests/addHello.js
new file mode 100644
index 0000000..d6f70e7
--- /dev/null
+++ b/json-tck/src/main/resources/net/java/html/js/tests/addHello.js
@@ -0,0 +1,20 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+testArray.push("Hello");
+
diff --git a/json-tck/src/main/resources/net/java/html/js/tests/addWorld.js b/json-tck/src/main/resources/net/java/html/js/tests/addWorld.js
new file mode 100644
index 0000000..78938de
--- /dev/null
+++ b/json-tck/src/main/resources/net/java/html/js/tests/addWorld.js
@@ -0,0 +1,20 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+testArray.push("World!");
+
diff --git a/json-tck/src/main/resources/net/java/html/js/tests/initArray.js b/json-tck/src/main/resources/net/java/html/js/tests/initArray.js
new file mode 100644
index 0000000..96dffff
--- /dev/null
+++ b/json-tck/src/main/resources/net/java/html/js/tests/initArray.js
@@ -0,0 +1,20 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+testArray = [];
+
diff --git a/pom.xml b/pom.xml
index f31aa76..a99b94b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -151,7 +151,7 @@ org.netbeans.html.boot.impl:org.netbeans.html.boot.fx:org.netbeans.html.context.
                     <artifactId>codesnippet-doclet</artifactId>
                     <version>0.22</version>
                 </docletArtifact>
-                <additionalparam>-snippetpath "${basedir}" ${javadoc.allowjs}</additionalparam>
+                <additionalparam>-snippetpath "${basedir}" ${javadoc.allowjs} -hiddingannotation java.lang.Deprecated</additionalparam>
               </configuration>
             </plugin>
             <plugin>
diff --git a/src/main/javadoc/overview.html b/src/main/javadoc/overview.html
index 49a44e4..b2d145f 100644
--- a/src/main/javadoc/overview.html
+++ b/src/main/javadoc/overview.html
@@ -126,6 +126,9 @@ <h3>New version will include</h3>
             {@link net.java.html.json.ComputedProperty Computed properties} can
             depend on other computed properties -
             <a target="_blank" href="https://github.com/apache/incubator-netbeans-html4j/pull/3">PR #3</a>.
+            {@link net.java.html.js.JavaScriptResource} annotation has been
+            made repeatable -
+            <a target="_blank" href="https://github.com/apache/incubator-netbeans-html4j/pull/4">PR #4</a>.
         </p>
 
         <h3>New in version 1.5.1</h3>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists