/langtools/test/tools/apt/Discovery/PhantomTouch.java

https://github.com/Morriar/ProxyJDK · Java · 93 lines · 52 code · 12 blank · 29 comment · 3 complexity · 603fcaf4ec8c86a732930b99ddd130e8 MD5 · raw file

  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. */
  23. import com.sun.mirror.apt.*;
  24. import com.sun.mirror.declaration.*;
  25. import com.sun.mirror.type.*;
  26. import com.sun.mirror.util.*;
  27. import java.util.Collection;
  28. import java.util.Set;
  29. import java.util.Arrays;
  30. import java.util.Collections;
  31. import java.io.*;
  32. public class PhantomTouch implements AnnotationProcessorFactory {
  33. static class PhantomTouchProc implements AnnotationProcessor {
  34. AnnotationProcessorEnvironment ape;
  35. PhantomTouchProc(AnnotationProcessorEnvironment ape) {
  36. this.ape = ape;
  37. }
  38. public void process() {
  39. // Only run the processor on the initial apt invocation
  40. if (ape.getSpecifiedTypeDeclarations().size() == 0) {
  41. boolean result;
  42. try {
  43. // Create temporary file
  44. java.io.File f = new java.io.File("touched");
  45. result = f.createNewFile();
  46. if (result) {
  47. // Create new source file
  48. PrintWriter pw = ape.getFiler().createSourceFile("HelloWorld");
  49. pw.println("public class HelloWorld {");
  50. pw.println(" // Phantom hello world");
  51. pw.println(" public static void main(String argv[]) {");
  52. pw.println(" System.out.println(\"Hello World\");");
  53. pw.println(" }");
  54. pw.println("}");
  55. } else
  56. throw new RuntimeException("touched file already exists!");
  57. } catch (java.io.IOException e) {
  58. result = false;
  59. }
  60. }
  61. }
  62. }
  63. static final Collection<String> supportedOptions;
  64. static final Collection<String> supportedTypes;
  65. static {
  66. String options[] = {""};
  67. supportedOptions = Collections.unmodifiableCollection(Arrays.asList(options));
  68. String types[] = {"*"};
  69. supportedTypes = Collections.unmodifiableCollection(Arrays.asList(types));
  70. }
  71. public Collection<String> supportedAnnotationTypes() {return supportedTypes;}
  72. public Collection<String> supportedOptions() {return supportedOptions;}
  73. /*
  74. * Return the same processor independent of what annotations are
  75. * present, if any.
  76. */
  77. public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds,
  78. AnnotationProcessorEnvironment env) {
  79. return new PhantomTouchProc(env);
  80. }
  81. }