8003412: javac needs to understand java.lang.annotation.Native
authorjjg
Wed, 14 Nov 2012 10:07:38 -0800
changeset 14539 507556c4e622
parent 14538 384681be798f
child 14540 b7cbc1db01fe
8003412: javac needs to understand java.lang.annotation.Native Reviewed-by: mcimadamore
langtools/src/share/classes/com/sun/tools/javac/code/Symtab.java
langtools/src/share/classes/com/sun/tools/javac/jvm/JNIWriter.java
langtools/test/tools/javac/nativeHeaders/NativeHeaderTest.java
langtools/test/tools/javac/nativeHeaders/javahComparison/CompareTest.java
langtools/test/tools/javac/nativeHeaders/javahComparison/TestClass4.java
langtools/test/tools/javac/nativeHeaders/javahComparison/TestClass5.java
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Symtab.java	Tue Nov 13 15:09:15 2012 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Symtab.java	Wed Nov 14 10:07:38 2012 -0800
@@ -130,6 +130,7 @@
     public final Type methodHandleLookupType;
     public final Type methodTypeType;
     public final Type nativeHeaderType;
+    public final Type nativeHeaderType_old;
     public final Type throwableType;
     public final Type errorType;
     public final Type interruptedExceptionType;
@@ -505,7 +506,8 @@
                                             List.of(exceptionType), methodClass),
                              autoCloseableType.tsym);
         trustMeType = enterClass("java.lang.SafeVarargs");
-        nativeHeaderType = enterClass("javax.tools.annotation.GenerateNativeHeader");
+        nativeHeaderType = enterClass("java.lang.annotation.Native");
+        nativeHeaderType_old = enterClass("javax.tools.annotation.GenerateNativeHeader");
         lambdaMetafactory = enterClass("java.lang.invoke.LambdaMetafactory");
 
         synthesizeEmptyInterfaceIfMissing(autoCloseableType);
--- a/langtools/src/share/classes/com/sun/tools/javac/jvm/JNIWriter.java	Tue Nov 13 15:09:15 2012 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/jvm/JNIWriter.java	Wed Nov 14 10:07:38 2012 -0800
@@ -157,13 +157,20 @@
         if (c.isLocal() || (c.flags() & Flags.SYNTHETIC) != 0)
             return false;
 
+        /* temporary code for backwards compatibility */
         for (Attribute.Compound a: c.annotations.getAttributes()) {
-            if (a.type.tsym == syms.nativeHeaderType.tsym)
+            if (a.type.tsym == syms.nativeHeaderType_old.tsym)
                 return true;
         }
+        /* end of temporary code for backwards compatibility */
+
         for (Scope.Entry i = c.members_field.elems; i != null; i = i.sibling) {
             if (i.sym.kind == Kinds.MTH && (i.sym.flags() & Flags.NATIVE) != 0)
                 return true;
+            for (Attribute.Compound a: i.sym.annotations.getAttributes()) {
+                if (a.type.tsym == syms.nativeHeaderType.tsym)
+                    return true;
+            }
         }
         if (checkNestedClasses) {
             for (Scope.Entry i = c.members_field.elems; i != null; i = i.sibling) {
--- a/langtools/test/tools/javac/nativeHeaders/NativeHeaderTest.java	Tue Nov 13 15:09:15 2012 -0800
+++ b/langtools/test/tools/javac/nativeHeaders/NativeHeaderTest.java	Wed Nov 14 10:07:38 2012 -0800
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 7150368
+ * @bug 7150368 8003412
  * @summary javac should include basic ability to generate native headers
  */
 
@@ -125,7 +125,7 @@
     }
 
     @Test
-    void annoTest(RunKind rk, GenKind gk) throws Exception {
+    void oldAnnoTest(RunKind rk, GenKind gk) throws Exception {
         List<File> files = new ArrayList<File>();
         files.add(createFile("p/C.java",
                 "@javax.tools.annotation.GenerateNativeHeader class C { }"));
@@ -136,10 +136,33 @@
     }
 
     @Test
+    void annoTest(RunKind rk, GenKind gk) throws Exception {
+        List<File> files = new ArrayList<File>();
+        files.add(createFile("p/C.java",
+                "class C { @java.lang.annotation.Native public static final int i = 1907; }"));
+
+        Set<String> expect = createSet("C.h");
+
+        test(rk, gk, files, expect);
+    }
+
+    @Test
+    void oldAnnoNestedClassTest(RunKind rk, GenKind gk) throws Exception {
+        List<File> files = new ArrayList<File>();
+        files.add(createFile("p/C.java",
+                "class C { @javax.tools.annotation.GenerateNativeHeader class Inner { } }"));
+
+        Set<String> expect = createSet("C_Inner.h");
+        if (gk == GenKind.FULL) expect.add("C.h");
+
+        test(rk, gk, files, expect);
+    }
+
+    @Test
     void annoNestedClassTest(RunKind rk, GenKind gk) throws Exception {
         List<File> files = new ArrayList<File>();
         files.add(createFile("p/C.java",
-                "class C { @javax.tools.annotation.GenerateNativeHeader class Inner { } }"));
+                "class C { class Inner { @java.lang.annotation.Native public static final int i = 1907; } }"));
 
         Set<String> expect = createSet("C_Inner.h");
         if (gk == GenKind.FULL) expect.add("C.h");
--- a/langtools/test/tools/javac/nativeHeaders/javahComparison/CompareTest.java	Tue Nov 13 15:09:15 2012 -0800
+++ b/langtools/test/tools/javac/nativeHeaders/javahComparison/CompareTest.java	Wed Nov 14 10:07:38 2012 -0800
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 7150368
+ * @bug 7150368 8003412
  * @summary javac should include basic ability to generate native headers
  */
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/nativeHeaders/javahComparison/TestClass4.java	Wed Nov 14 10:07:38 2012 -0800
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code 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
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.lang.annotation.Native;
+
+public class TestClass4 {
+    @Native
+    public static final byte b = 1;
+
+    @Native
+    public static final short s = 2;
+
+    @Native
+    public static final int i = 3;
+
+    @Native
+    public static final long l = 4;
+
+    @Native
+    public static final float f = 5.0f;
+
+    @Native
+    public static final double d = 6.0;
+
+    @Native
+    public static final Object o = null;
+
+    @Native
+    public static final String t = "8";
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/nativeHeaders/javahComparison/TestClass5.java	Wed Nov 14 10:07:38 2012 -0800
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code 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
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.lang.annotation.Native;
+
+public class TestClass5 {
+    @Native
+    public static final int tc5 = 1;
+
+    public class Inner1 {
+        @Native
+        public static final int tc5i1 = 2;
+
+        public class Inner1A {
+            @Native
+            public static final int tc5i1i1a = 3;
+        }
+
+        public class Inner1B {
+            @Native
+            public static final int tc5i1i1b = 4;
+        }
+    }
+
+    public class Inner2 {
+        @Native
+        public static final int tc521 = 5;
+
+        public class Inner2A {
+            @Native
+            public static final int tc5i2i2a = 6;
+        }
+
+        public class Inner2B {
+            @Native
+            public static final int tc5i2i2b = 7;
+        }
+    }
+}
+