test/tools/javac/annotations/typeAnnotations/classfile/NoTargetAnnotations.java

Tue, 24 Dec 2013 09:17:37 -0800

author
ksrini
date
Tue, 24 Dec 2013 09:17:37 -0800
changeset 2227
998b10c43157
parent 1534
bec996065c45
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com

jjg@1521 1 /*
ksrini@2227 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1521 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1521 4 *
jjg@1521 5 * This code is free software; you can redistribute it and/or modify it
jjg@1521 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1521 7 * published by the Free Software Foundation.
jjg@1521 8 *
jjg@1521 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1521 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1521 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1521 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1521 13 * accompanied this code).
jjg@1521 14 *
jjg@1521 15 * You should have received a copy of the GNU General Public License version
jjg@1521 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1521 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1521 18 *
jjg@1521 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1521 20 * or visit www.oracle.com if you need additional information or have any
jjg@1521 21 * questions.
jjg@1521 22 */
jjg@1521 23
jjg@1521 24 import java.io.*;
jjg@1521 25 import java.lang.annotation.Retention;
jjg@1521 26 import java.lang.annotation.RetentionPolicy;
jjg@1521 27 import java.net.URL;
jjg@1521 28 import java.util.List;
jjg@1521 29
jjg@1521 30 import com.sun.tools.classfile.*;
jjg@1521 31
jjg@1521 32 /*
jjg@1521 33 * @test NoTargetAnnotations
jjg@1521 34 * @summary test that annotations with no Target meta type is emitted
jjg@1521 35 * only once as declaration annotation
jjg@1521 36 */
jjg@1521 37 public class NoTargetAnnotations {
jjg@1521 38
jjg@1521 39 public static void main(String[] args) throws Exception {
jjg@1521 40 new NoTargetAnnotations().run();
jjg@1521 41 }
jjg@1521 42
jjg@1521 43 public void run() throws Exception {
jjg@1521 44 ClassFile cf = getClassFile("NoTargetAnnotations$Test.class");
jjg@1521 45 for (Field f : cf.fields) {
jjg@1521 46 test(cf, f);
jjg@1521 47 testDeclaration(cf, f);
jjg@1521 48 }
jjg@1521 49 for (Method m: cf.methods) {
jjg@1521 50 test(cf, m);
jjg@1521 51 testDeclaration(cf, m);
jjg@1521 52 }
jjg@1521 53
jjg@1521 54 countAnnotations();
jjg@1521 55
jjg@1521 56 if (errors > 0)
jjg@1521 57 throw new Exception(errors + " errors found");
jjg@1521 58 System.out.println("PASSED");
jjg@1521 59 }
jjg@1521 60
jjg@1521 61 ClassFile getClassFile(String name) throws IOException, ConstantPoolException {
jjg@1521 62 URL url = getClass().getResource(name);
jjg@1521 63 InputStream in = url.openStream();
jjg@1521 64 try {
jjg@1521 65 return ClassFile.read(in);
jjg@1521 66 } finally {
jjg@1521 67 in.close();
jjg@1521 68 }
jjg@1521 69 }
jjg@1521 70
jjg@1521 71 /************ Helper annotations counting methods ******************/
jjg@1521 72 void test(ClassFile cf, Method m) {
jjg@1521 73 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@1521 74 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@1521 75 }
jjg@1521 76
jjg@1521 77 void test(ClassFile cf, Field m) {
jjg@1521 78 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@1521 79 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@1521 80 }
jjg@1521 81
jjg@1521 82 void testDeclaration(ClassFile cf, Method m) {
jjg@1521 83 testDecl(cf, m, Attribute.RuntimeVisibleAnnotations, true);
jjg@1521 84 testDecl(cf, m, Attribute.RuntimeInvisibleAnnotations, false);
jjg@1521 85 }
jjg@1521 86
jjg@1521 87 void testDeclaration(ClassFile cf, Field m) {
jjg@1521 88 testDecl(cf, m, Attribute.RuntimeVisibleAnnotations, true);
jjg@1521 89 testDecl(cf, m, Attribute.RuntimeInvisibleAnnotations, false);
jjg@1521 90 }
jjg@1521 91
jjg@1521 92 // test the result of Attributes.getIndex according to expectations
jjg@1521 93 // encoded in the method's name
jjg@1521 94 void test(ClassFile cf, Method m, String name, boolean visible) {
jjg@1521 95 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@1521 96 if (index != -1) {
jjg@1521 97 Attribute attr = m.attributes.get(index);
jjg@1521 98 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@1521 99 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@1521 100 all += tAttr.annotations.length;
jjg@1521 101 if (visible)
jjg@1521 102 visibles += tAttr.annotations.length;
jjg@1521 103 else
jjg@1521 104 invisibles += tAttr.annotations.length;
jjg@1521 105 }
jjg@1521 106 }
jjg@1521 107
jjg@1521 108 // test the result of Attributes.getIndex according to expectations
jjg@1521 109 // encoded in the method's name
jjg@1521 110 void test(ClassFile cf, Field m, String name, boolean visible) {
jjg@1521 111 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@1521 112 if (index != -1) {
jjg@1521 113 Attribute attr = m.attributes.get(index);
jjg@1521 114 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@1521 115 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@1521 116 all += tAttr.annotations.length;
jjg@1521 117 if (visible)
jjg@1521 118 visibles += tAttr.annotations.length;
jjg@1521 119 else
jjg@1521 120 invisibles += tAttr.annotations.length;
jjg@1521 121 }
jjg@1521 122 }
jjg@1521 123
jjg@1521 124 // test the result of Attributes.getIndex according to expectations
jjg@1521 125 // encoded in the method's name
jjg@1521 126 void testDecl(ClassFile cf, Method m, String name, boolean visible) {
jjg@1521 127 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@1521 128 if (index != -1) {
jjg@1521 129 Attribute attr = m.attributes.get(index);
jjg@1521 130 assert attr instanceof RuntimeAnnotations_attribute;
jjg@1521 131 RuntimeAnnotations_attribute tAttr = (RuntimeAnnotations_attribute)attr;
jjg@1521 132 this.declAnnotations += tAttr.annotations.length;
jjg@1521 133 }
jjg@1521 134 }
jjg@1521 135
jjg@1521 136 // test the result of Attributes.getIndex according to expectations
jjg@1521 137 // encoded in the method's name
jjg@1521 138 void testDecl(ClassFile cf, Field m, String name, boolean visible) {
jjg@1521 139 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@1521 140 if (index != -1) {
jjg@1521 141 Attribute attr = m.attributes.get(index);
jjg@1521 142 assert attr instanceof RuntimeAnnotations_attribute;
jjg@1521 143 RuntimeAnnotations_attribute tAttr = (RuntimeAnnotations_attribute)attr;
jjg@1521 144 this.declAnnotations += tAttr.annotations.length;
jjg@1521 145 }
jjg@1521 146 }
jjg@1521 147
jjg@1521 148 File compileTestFile(File f) {
jjg@1521 149 int rc = com.sun.tools.javac.Main.compile(new String[] { "-XDTA:writer", "-source", "1.8", "-g", f.getPath() });
jjg@1521 150 if (rc != 0)
jjg@1521 151 throw new Error("compilation failed. rc=" + rc);
jjg@1521 152 String path = f.getPath();
jjg@1521 153 return new File(path.substring(0, path.length() - 5) + ".class");
jjg@1521 154 }
jjg@1521 155
jjg@1521 156 void countAnnotations() {
jjg@1521 157 int expected_all = expected_visibles + expected_invisibles;
jjg@1521 158
jjg@1521 159 if (expected_all != all) {
jjg@1521 160 errors++;
jjg@1521 161 System.err.println("expected " + expected_all
jjg@1521 162 + " annotations but found " + all);
jjg@1521 163 }
jjg@1521 164
jjg@1521 165 if (expected_visibles != visibles) {
jjg@1521 166 errors++;
jjg@1521 167 System.err.println("expected " + expected_visibles
jjg@1521 168 + " visibles annotations but found " + visibles);
jjg@1521 169 }
jjg@1521 170
jjg@1521 171 if (expected_invisibles != invisibles) {
jjg@1521 172 errors++;
jjg@1521 173 System.err.println("expected " + expected_invisibles
jjg@1521 174 + " invisibles annotations but found " + invisibles);
jjg@1521 175 }
jjg@1521 176
jjg@1521 177 if (expected_decl != declAnnotations) {
jjg@1521 178 errors++;
jjg@1521 179 System.err.println("expected " + expected_decl
jjg@1521 180 + " declaration annotations but found " + declAnnotations);
jjg@1521 181 }
jjg@1521 182 }
jjg@1521 183
jjg@1521 184 int errors;
jjg@1521 185 int all;
jjg@1521 186 int visibles;
jjg@1521 187 int invisibles;
jjg@1521 188
jjg@1521 189 int declAnnotations;
jjg@1521 190
jjg@1521 191 /*********************** Test class *************************/
jjg@1521 192 static int expected_invisibles = 0;
jjg@1521 193 static int expected_visibles = 0;
jjg@1521 194 static int expected_decl = 1;
jjg@1521 195
jjg@1521 196 static class Test {
jjg@1521 197 @Retention(RetentionPolicy.RUNTIME)
jjg@1521 198 @interface A {}
jjg@1521 199
jjg@1521 200 @A String method() {
jjg@1521 201 return null;
jjg@1521 202 }
jjg@1521 203 }
jjg@1521 204 }

mercurial