src/share/classes/com/sun/tools/apt/main/Main.java

Print this page




  39 
  40 import java.net.URLClassLoader;
  41 import java.net.URL;
  42 import java.net.MalformedURLException;
  43 
  44 import javax.tools.JavaFileManager;
  45 import javax.tools.StandardLocation;
  46 
  47 import com.sun.tools.javac.file.JavacFileManager;
  48 import com.sun.tools.javac.code.Source;
  49 import com.sun.tools.javac.code.Symbol;
  50 import com.sun.tools.javac.code.Type;
  51 import com.sun.tools.javac.jvm.Target;
  52 import com.sun.tools.javac.util.*;
  53 
  54 import com.sun.tools.apt.comp.AnnotationProcessingError;
  55 import com.sun.tools.apt.comp.UsageMessageNeededException;
  56 import com.sun.tools.apt.util.Bark;
  57 import com.sun.mirror.apt.AnnotationProcessorFactory;
  58 


  59 /** This class provides a commandline interface to the apt build-time
  60  *  tool.
  61  *
  62  *  <p><b>This is NOT part of any API supported by Sun Microsystems.
  63  *  If you write code that depends on this, you do so at your own
  64  *  risk.  This code and its internal interfaces are subject to change
  65  *  or deletion without notice.</b>
  66  */
  67 @SuppressWarnings("deprecation")
  68 public class Main {
  69 
  70     /** For testing: enter any options you want to be set implicitly
  71      *  here.
  72      */
  73     static String[] forcedOpts = {
  74         // Preserve parameter names from class files if the class was
  75         // compiled with debug enabled
  76         "-XDsave-parameter-names"
  77     };
  78 


1259         String[] args = new String[_args.length];
1260         for (int i=0; i<_args.length; i++) {
1261             args[i] = "" + _args[i];
1262         }
1263         if (messageRBapt == null || messageRBjavac == null )
1264             initResource();
1265         try {
1266             return MessageFormat.format(messageRBapt.getString("apt." + key),
1267                                         (Object[]) args);
1268         } catch (MissingResourceException e) {
1269             try {
1270                 return MessageFormat.format(messageRBjavac.getString("javac." + key),
1271                                             (Object[]) args);
1272             } catch (MissingResourceException f) {
1273                 String msg = "apt or javac message file broken: key={0} "
1274                     + "arguments={1}, {2}";
1275                 return MessageFormat.format(msg, (Object[]) args);
1276             }
1277         }
1278     }
1279 
1280     // Borrowed from DocletInvoker
1281     /**
1282      * Utility method for converting a search path string to an array
1283      * of directory and JAR file URLs.
1284      *
1285      * @param path the search path string
1286      * @return the resulting array of directory and JAR file URLs
1287      */
1288     static URL[] pathToURLs(String path) {
1289         StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
1290         URL[] urls = new URL[st.countTokens()];
1291         int count = 0;
1292         while (st.hasMoreTokens()) {
1293             URL url = fileToURL(new File(st.nextToken()));
1294             if (url != null) {
1295                 urls[count++] = url;
1296             }
1297         }
1298         if (urls.length != count) {
1299             URL[] tmp = new URL[count];
1300             System.arraycopy(urls, 0, tmp, 0, count);
1301             urls = tmp;
1302         }
1303         return urls;
1304     }
1305 
1306     /**
1307      * Returns the directory or JAR file URL corresponding to the specified
1308      * local file name.
1309      *
1310      * @param file the File object
1311      * @return the resulting directory or JAR file URL, or null if unknown
1312      */
1313     static URL fileToURL(File file) {
1314         String name;
1315         try {
1316             name = file.getCanonicalPath();
1317         } catch (IOException e) {
1318             name = file.getAbsolutePath();
1319         }
1320         name = name.replace(File.separatorChar, '/');
1321         if (!name.startsWith("/")) {
1322             name = "/" + name;
1323         }
1324         // If the file does not exist, then assume that it's a directory
1325         if (!file.isFile()) {
1326             name = name + "/";
1327         }
1328         try {
1329             return new URL("file", "", name);
1330         } catch (MalformedURLException e) {
1331             throw new IllegalArgumentException("file");
1332         }
1333     }
1334 }


  39 
  40 import java.net.URLClassLoader;
  41 import java.net.URL;
  42 import java.net.MalformedURLException;
  43 
  44 import javax.tools.JavaFileManager;
  45 import javax.tools.StandardLocation;
  46 
  47 import com.sun.tools.javac.file.JavacFileManager;
  48 import com.sun.tools.javac.code.Source;
  49 import com.sun.tools.javac.code.Symbol;
  50 import com.sun.tools.javac.code.Type;
  51 import com.sun.tools.javac.jvm.Target;
  52 import com.sun.tools.javac.util.*;
  53 
  54 import com.sun.tools.apt.comp.AnnotationProcessingError;
  55 import com.sun.tools.apt.comp.UsageMessageNeededException;
  56 import com.sun.tools.apt.util.Bark;
  57 import com.sun.mirror.apt.AnnotationProcessorFactory;
  58 
  59 import static com.sun.tools.javac.file.Paths.pathToURLs;
  60 
  61 /** This class provides a commandline interface to the apt build-time
  62  *  tool.
  63  *
  64  *  <p><b>This is NOT part of any API supported by Sun Microsystems.
  65  *  If you write code that depends on this, you do so at your own
  66  *  risk.  This code and its internal interfaces are subject to change
  67  *  or deletion without notice.</b>
  68  */
  69 @SuppressWarnings("deprecation")
  70 public class Main {
  71 
  72     /** For testing: enter any options you want to be set implicitly
  73      *  here.
  74      */
  75     static String[] forcedOpts = {
  76         // Preserve parameter names from class files if the class was
  77         // compiled with debug enabled
  78         "-XDsave-parameter-names"
  79     };
  80 


1261         String[] args = new String[_args.length];
1262         for (int i=0; i<_args.length; i++) {
1263             args[i] = "" + _args[i];
1264         }
1265         if (messageRBapt == null || messageRBjavac == null )
1266             initResource();
1267         try {
1268             return MessageFormat.format(messageRBapt.getString("apt." + key),
1269                                         (Object[]) args);
1270         } catch (MissingResourceException e) {
1271             try {
1272                 return MessageFormat.format(messageRBjavac.getString("javac." + key),
1273                                             (Object[]) args);
1274             } catch (MissingResourceException f) {
1275                 String msg = "apt or javac message file broken: key={0} "
1276                     + "arguments={1}, {2}";
1277                 return MessageFormat.format(msg, (Object[]) args);
1278             }
1279         }
1280     }























































1281 }