test/tools/apt/Discovery/Touch.java

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 554
9d9f26857129
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

     1 /*
     2  * Copyright (c) 2004, 2007, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    25 import com.sun.mirror.apt.*;
    26 import com.sun.mirror.declaration.*;
    27 import com.sun.mirror.type.*;
    28 import com.sun.mirror.util.*;
    30 import java.util.Collection;
    31 import java.util.Set;
    32 import java.util.Arrays;
    33 import java.util.Collections;
    34 import java.io.*;
    36 public class Touch implements AnnotationProcessorFactory {
    37     static class TouchProc implements AnnotationProcessor {
    38         AnnotationProcessorEnvironment ape;
    39         TouchProc(AnnotationProcessorEnvironment ape) {
    40             this.ape = ape;
    41         }
    43         public void process() {
    44             boolean result;
    45             // Only run the processor on the initial apt invocation
    46             Collection<TypeDeclaration> tdecls = ape.getSpecifiedTypeDeclarations();
    48             if (tdecls.size() == 1) {
    49                 for(TypeDeclaration decl: tdecls) {
    50                     if (! decl.getSimpleName().equals("Touch") )
    51                         return;
    52                 }
    54                 try {
    55                     // Create temporary file
    56                     java.io.File f = new java.io.File("touched");
    57                     result = f.createNewFile();
    60                     Filer filer = ape.getFiler();
    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("}");
    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);
    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             }
    84         }
    86     }
    88     static Collection<String> supportedTypes;
    89     static {
    90         String types[] = {"*"};
    91         supportedTypes = Collections.unmodifiableCollection(Arrays.asList(types));
    92     }
    94     static Collection<String> supportedOptions;
    95     static {
    96         String options[] = {""};
    97         supportedOptions = Collections.unmodifiableCollection(Arrays.asList(options));
    98     }
   100     public Collection<String> supportedOptions() {
   101         return supportedOptions;
   102     }
   104     public Collection<String> supportedAnnotationTypes() {
   105         return supportedTypes;
   106     }
   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