1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/apt/Discovery/Touch.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,116 @@ 1.4 +/* 1.5 + * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 + 1.28 +import com.sun.mirror.apt.*; 1.29 +import com.sun.mirror.declaration.*; 1.30 +import com.sun.mirror.type.*; 1.31 +import com.sun.mirror.util.*; 1.32 + 1.33 +import java.util.Collection; 1.34 +import java.util.Set; 1.35 +import java.util.Arrays; 1.36 +import java.util.Collections; 1.37 +import java.io.*; 1.38 + 1.39 +public class Touch implements AnnotationProcessorFactory { 1.40 + static class TouchProc implements AnnotationProcessor { 1.41 + AnnotationProcessorEnvironment ape; 1.42 + TouchProc(AnnotationProcessorEnvironment ape) { 1.43 + this.ape = ape; 1.44 + } 1.45 + 1.46 + public void process() { 1.47 + boolean result; 1.48 + // Only run the processor on the initial apt invocation 1.49 + Collection<TypeDeclaration> tdecls = ape.getSpecifiedTypeDeclarations(); 1.50 + 1.51 + if (tdecls.size() == 1) { 1.52 + for(TypeDeclaration decl: tdecls) { 1.53 + if (! decl.getSimpleName().equals("Touch") ) 1.54 + return; 1.55 + } 1.56 + 1.57 + try { 1.58 + // Create temporary file 1.59 + java.io.File f = new java.io.File("touched"); 1.60 + result = f.createNewFile(); 1.61 + 1.62 + 1.63 + Filer filer = ape.getFiler(); 1.64 + 1.65 + // Create new source file 1.66 + PrintWriter pw = filer.createSourceFile("HelloWorld"); 1.67 + pw.println("public class HelloWorld {"); 1.68 + pw.println(" public static void main(String argv[]) {"); 1.69 + pw.println(" System.out.println(\"Hello World\");"); 1.70 + pw.println(" }"); 1.71 + pw.println("}"); 1.72 + 1.73 + // Create new class file and copy Empty.class 1.74 + OutputStream os = filer.createClassFile("Empty"); 1.75 + FileInputStream fis = new FileInputStream("Empty.clazz"); 1.76 + int datum; 1.77 + while((datum = fis.read()) != -1) 1.78 + os.write(datum); 1.79 + 1.80 + } catch (java.io.IOException e) { 1.81 + result = false; 1.82 + } 1.83 + if (!result) 1.84 + throw new RuntimeException("touched file already exists or other error"); 1.85 + } 1.86 + 1.87 + } 1.88 + 1.89 + } 1.90 + 1.91 + static Collection<String> supportedTypes; 1.92 + static { 1.93 + String types[] = {"*"}; 1.94 + supportedTypes = Collections.unmodifiableCollection(Arrays.asList(types)); 1.95 + } 1.96 + 1.97 + static Collection<String> supportedOptions; 1.98 + static { 1.99 + String options[] = {""}; 1.100 + supportedOptions = Collections.unmodifiableCollection(Arrays.asList(options)); 1.101 + } 1.102 + 1.103 + public Collection<String> supportedOptions() { 1.104 + return supportedOptions; 1.105 + } 1.106 + 1.107 + public Collection<String> supportedAnnotationTypes() { 1.108 + return supportedTypes; 1.109 + } 1.110 + 1.111 + /* 1.112 + * Return the same processor independent of what annotations are 1.113 + * present, if any. 1.114 + */ 1.115 + public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds, 1.116 + AnnotationProcessorEnvironment env) { 1.117 + return new TouchProc(env); 1.118 + } 1.119 +}