test/tools/apt/Discovery/Touch.java

changeset 1
9a66ca7c79fa
child 554
9d9f26857129
equal deleted inserted replaced
-1:000000000000 1:9a66ca7c79fa
1 /*
2 * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24
25 import com.sun.mirror.apt.*;
26 import com.sun.mirror.declaration.*;
27 import com.sun.mirror.type.*;
28 import com.sun.mirror.util.*;
29
30 import java.util.Collection;
31 import java.util.Set;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.io.*;
35
36 public class Touch implements AnnotationProcessorFactory {
37 static class TouchProc implements AnnotationProcessor {
38 AnnotationProcessorEnvironment ape;
39 TouchProc(AnnotationProcessorEnvironment ape) {
40 this.ape = ape;
41 }
42
43 public void process() {
44 boolean result;
45 // Only run the processor on the initial apt invocation
46 Collection<TypeDeclaration> tdecls = ape.getSpecifiedTypeDeclarations();
47
48 if (tdecls.size() == 1) {
49 for(TypeDeclaration decl: tdecls) {
50 if (! decl.getSimpleName().equals("Touch") )
51 return;
52 }
53
54 try {
55 // Create temporary file
56 java.io.File f = new java.io.File("touched");
57 result = f.createNewFile();
58
59
60 Filer filer = ape.getFiler();
61
62 // Create new source file
63 PrintWriter pw = filer.createSourceFile("HelloWorld");
64 pw.println("public class HelloWorld {");
65 pw.println(" public static void main(String argv[]) {");
66 pw.println(" System.out.println(\"Hello World\");");
67 pw.println(" }");
68 pw.println("}");
69
70 // Create new class file and copy Empty.class
71 OutputStream os = filer.createClassFile("Empty");
72 FileInputStream fis = new FileInputStream("Empty.clazz");
73 int datum;
74 while((datum = fis.read()) != -1)
75 os.write(datum);
76
77 } catch (java.io.IOException e) {
78 result = false;
79 }
80 if (!result)
81 throw new RuntimeException("touched file already exists or other error");
82 }
83
84 }
85
86 }
87
88 static Collection<String> supportedTypes;
89 static {
90 String types[] = {"*"};
91 supportedTypes = Collections.unmodifiableCollection(Arrays.asList(types));
92 }
93
94 static Collection<String> supportedOptions;
95 static {
96 String options[] = {""};
97 supportedOptions = Collections.unmodifiableCollection(Arrays.asList(options));
98 }
99
100 public Collection<String> supportedOptions() {
101 return supportedOptions;
102 }
103
104 public Collection<String> supportedAnnotationTypes() {
105 return supportedTypes;
106 }
107
108 /*
109 * Return the same processor independent of what annotations are
110 * present, if any.
111 */
112 public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds,
113 AnnotationProcessorEnvironment env) {
114 return new TouchProc(env);
115 }
116 }

mercurial