test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java

changeset 232
1fbc1cc6e260
parent 1
9a66ca7c79fa
child 395
5a72ba18c471
equal deleted inserted replaced
231:435d5d9bb87d 232:1fbc1cc6e260
1 /* 1 /*
2 * Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved. 2 * Copyright 2006-2009 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
21 * have any questions. 21 * have any questions.
22 */ 22 */
23 23
24 /* 24 /*
25 * @test 25 * @test
26 * @bug 6397298 6400986 6425592 6449798 6453386 6508401 26 * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938
27 * @summary Tests that getElementsAnnotatedWith works properly. 27 * @summary Tests that getElementsAnnotatedWith works properly.
28 * @author Joseph D. Darcy 28 * @author Joseph D. Darcy
29 * @compile TestElementsAnnotatedWith.java 29 * @compile TestElementsAnnotatedWith.java
30 * @compile InheritedAnnotation.java 30 * @compile InheritedAnnotation.java
31 * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java 31 * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java
32 * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java 32 * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java
33 * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java 33 * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java
34 * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
35 * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
36 * @compile -XD-d=. Foo.java
34 * @compile -processor TestElementsAnnotatedWith -proc:only TestElementsAnnotatedWith.java 37 * @compile -processor TestElementsAnnotatedWith -proc:only TestElementsAnnotatedWith.java
35 * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
36 */ 38 */
37 39
38 import java.lang.annotation.Annotation; 40 import java.lang.annotation.Annotation;
41 import java.io.*;
39 import java.util.Collections; 42 import java.util.Collections;
40 import java.util.Set; 43 import java.util.Set;
41 import java.util.HashSet; 44 import java.util.HashSet;
45 import java.util.List;
46 import java.util.ArrayList;
42 import java.util.Arrays; 47 import java.util.Arrays;
43 import javax.annotation.processing.*; 48 import javax.annotation.processing.*;
49 import javax.tools.*;
44 import javax.lang.model.SourceVersion; 50 import javax.lang.model.SourceVersion;
45 import javax.lang.model.element.*; 51 import javax.lang.model.element.*;
46 import javax.lang.model.util.*; 52 import javax.lang.model.util.*;
47 import static javax.lang.model.util.ElementFilter.*; 53 import static javax.lang.model.util.ElementFilter.*;
48 54
118 124
119 if (failed) { 125 if (failed) {
120 System.err.println("AnnotatedElementInfo: " + annotatedElementInfo); 126 System.err.println("AnnotatedElementInfo: " + annotatedElementInfo);
121 throw new RuntimeException(); 127 throw new RuntimeException();
122 } 128 }
129
130 if("TestElementsAnnotatedWith".equals(firstType.getSimpleName().toString()))
131 writeClassFile(); // Start another round to test class file input
123 } else { 132 } else {
124 // If processing is over without an error, the specified 133 // If processing is over without an error, the specified
125 // elements should be empty so an empty set should be returned. 134 // elements should be empty so an empty set should be returned.
126 resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement); 135 resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement);
127 resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class); 136 resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class);
159 getTypeElement("java.lang.Object") ); 168 getTypeElement("java.lang.Object") );
160 throw new RuntimeException("Illegal argument exception not thrown"); 169 throw new RuntimeException("Illegal argument exception not thrown");
161 } catch(IllegalArgumentException iae) {} 170 } catch(IllegalArgumentException iae) {}
162 } 171 }
163 172
173 /*
174 * Hack alert! The class file read below is generated by the
175 * "@compile -XD-d=. Foo.java" directive above. This sneakily
176 * overrides the output location to the current directory where a
177 * subsequent @compile can read the file. This could be improved
178 * if either a new directive like @process accepted class file
179 * arguments (the javac command accepts such arguments but
180 * @compile does not) or the test.src and test.classes properties
181 * were set to be read with @compile jobs.
182 */
183 private void writeClassFile() {
184 try {
185 Filer filer = processingEnv.getFiler();
186 JavaFileObject jfo = filer.createClassFile("Foo");
187 OutputStream os = jfo.openOutputStream();
188 // Copy the bytes over
189 System.out.println((new File(".")).getAbsolutePath());
190 InputStream io = new BufferedInputStream(new FileInputStream(new File(".", "Foo.class")));
191 int datum = io.read();
192 while(datum != -1) {
193 os.write(datum);
194 datum = io.read();
195 }
196 os.close();
197 } catch (IOException io) {
198 throw new RuntimeException(io);
199 }
200
201
202 }
203
164 @Override 204 @Override
165 public SourceVersion getSupportedSourceVersion() { 205 public SourceVersion getSupportedSourceVersion() {
166 return SourceVersion.latest(); 206 return SourceVersion.latest();
167 } 207 }
168 } 208 }

mercurial