1. /*
  2. * @(#)Filer.java 1.1 04/01/26
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.mirror.apt;
  8. import java.io.*;
  9. /**
  10. * This interface supports the creation of new files by an
  11. * annotation processor.
  12. * Files created in this way will be known to the annotation processing
  13. * tool implementing this interface, better enabling the tool to manage them.
  14. * Four kinds of files are distinguished:
  15. * source files, class files, other text files, and other binary files.
  16. * The latter two are collectively referred to as <i>auxiliary</i> files.
  17. *
  18. * <p> There are two distinguished locations (subtrees within the
  19. * file system) where newly created files are placed:
  20. * one for new source files, and one for new class files.
  21. * (These might be specified on a tool's command line, for example,
  22. * using flags such as <tt>-s</tt> and <tt>-d</tt>.)
  23. * Auxiliary files may be created in either location.
  24. *
  25. * <p> During each run of an annotation processing tool, a file
  26. * with a given pathname may be created only once. If that file already
  27. * exists before the first attempt to create it, the old contents will
  28. * be deleted. Any subsequent attempt to create the same file during
  29. * a run will fail.
  30. *
  31. * @author Joseph D. Darcy
  32. * @author Scott Seligman
  33. * @version 1.1 04/01/26
  34. * @since 1.5
  35. */
  36. public interface Filer {
  37. /**
  38. * Creates a new source file and returns a writer for it.
  39. * The file's name and path (relative to the root of all newly created
  40. * source files) is based on the type to be declared in that file.
  41. * If more than one type is being declared, the name of the principal
  42. * top-level type (the public one, for example) should be used.
  43. *
  44. * <p> The {@linkplain java.nio.charset.Charset charset} used to
  45. * encode the file is determined by the implementation.
  46. * An annotation processing tool may have an <tt>-encoding</tt>
  47. * flag or the like for specifying this. It will typically use
  48. * the platform's default encoding if none is specified.
  49. *
  50. * @param name canonical (fully qualified) name of the principal type
  51. * being declared in this file
  52. * @return a writer for the new file
  53. * @throws IOException if the file cannot be created
  54. */
  55. PrintWriter createSourceFile(String name) throws IOException;
  56. /**
  57. * Creates a new class file, and returns a stream for writing to it.
  58. * The file's name and path (relative to the root of all newly created
  59. * class files) is based on the name of the type being written.
  60. *
  61. * @param name canonical (fully qualified) name of the type being written
  62. * @return a stream for writing to the new file
  63. * @throws IOException if the file cannot be created
  64. */
  65. OutputStream createClassFile(String name) throws IOException;
  66. /**
  67. * Creates a new text file, and returns a writer for it.
  68. * The file is located along with either the
  69. * newly created source or newly created binary files. It may be
  70. * named relative to some package (as are source and binary files),
  71. * and from there by an arbitrary pathname. In a loose sense, the
  72. * pathname of the new file will be the concatenation of
  73. * <tt>loc</tt>, <tt>pkg</tt>, and <tt>relPath</tt>.
  74. *
  75. * <p> A {@linkplain java.nio.charset.Charset charset} for
  76. * encoding the file may be provided. If none is given, the
  77. * charset used to encode source files
  78. * (see {@link #createSourceFile(String)}) will be used.
  79. *
  80. * @param loc location of the new file
  81. * @param pkg package relative to which the file should be named,
  82. * or the empty string if none
  83. * @param relPath final pathname components of the file
  84. * @param charsetName the name of the charset to use, or null if none
  85. * is being explicitly specified
  86. * @return a writer for the new file
  87. * @throws IOException if the file cannot be created
  88. */
  89. PrintWriter createTextFile(Location loc,
  90. String pkg,
  91. File relPath,
  92. String charsetName) throws IOException;
  93. /**
  94. * Creates a new binary file, and returns a stream for writing to it.
  95. * The file is located along with either the
  96. * newly created source or newly created binary files. It may be
  97. * named relative to some package (as are source and binary files),
  98. * and from there by an arbitrary pathname. In a loose sense, the
  99. * pathname of the new file will be the concatenation of
  100. * <tt>loc</tt>, <tt>pkg</tt>, and <tt>relPath</tt>.
  101. *
  102. * @param loc location of the new file
  103. * @param pkg package relative to which the file should be named,
  104. * or the empty string if none
  105. * @param relPath final pathname components of the file
  106. * @return a stream for writing to the new file
  107. * @throws IOException if the file cannot be created
  108. */
  109. OutputStream createBinaryFile(Location loc,
  110. String pkg,
  111. File relPath) throws IOException;
  112. /**
  113. * Locations (subtrees within the file system) where new files are created.
  114. */
  115. enum Location {
  116. /** The location of new source files. */
  117. SOURCE_TREE,
  118. /** The location of new class files. */
  119. CLASS_TREE
  120. }
  121. }