KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > compiler > CompilerProxy


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32 END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.compiler;
35
36 import java.io.File JavaDoc;
37 import java.util.List JavaDoc;
38
39 import edu.rice.cs.drjava.DrJava;
40 import edu.rice.cs.util.classloader.StickyClassLoader;
41 import edu.rice.cs.util.Log;
42 import edu.rice.cs.util.swing.Utilities;
43 import edu.rice.cs.drjava.config.OptionConstants;
44 import edu.rice.cs.drjava.config.FileOption;
45
46 /** A compiler interface to search a given
47  * @version $Id: CompilerProxy.java 4044 2006-11-23 13:40:43Z dlsmith $
48  */

49 public class CompilerProxy implements CompilerInterface {
50   
51   public static final String JavaDoc VERSION = System.getProperty("java.specification.version");
52   
53   private static final Log _log = new Log("CompilerTest.txt", false);
54   
55   /** The actual compiler interface. If it's null, we couldn't load it. */
56   private CompilerInterface _realCompiler = null;
57
58   private final String JavaDoc _className;
59   private final ClassLoader JavaDoc _newLoader;
60   
61   /** These classes will always be loaded using the previous classloader. This is important to make sure there is
62    * only one instance of them, so their values can be freely passed about the program.
63    */

64   private static final String JavaDoc[] _useOldLoader = {
65     "edu.rice.cs.drjava.model.Configuration",
66     "edu.rice.cs.drjava.model.compiler.CompilerInterface",
67     "edu.rice.cs.drjava.model.compiler.CompilerError"
68   };
69
70   /** A proxy compiler interface that tries to load the given class from one of the given locations. It uses its own
71    * classloader, which will even allow loading a second instance of the class!
72    * @param className Implementation of {@link CompilerInterface} to proxy for.
73    * @param newLoader Classloader to use
74    */

75
76   public CompilerProxy(String JavaDoc className, ClassLoader JavaDoc newLoader) {
77     _className = className;
78     _newLoader = newLoader;
79     _recreateCompiler();
80   }
81
82   private void _recreateCompiler() {
83     
84     _log.log(this + "._recreateCompiler() called");
85     
86     StickyClassLoader loader = new StickyClassLoader(_newLoader, getClass().getClassLoader(), _useOldLoader);
87     
88     try {
89       Class JavaDoc<?> c = loader.loadClass(_className);
90       _log.log("Class " + c + " loaded");
91       _realCompiler = CompilerRegistry.createCompiler(c);
92       
93       _log.log("_realCompiler set to " + _realCompiler);
94       
95       String JavaDoc compilerClass = _realCompiler.getClass().getName();
96       _log.log("Compiler created with name " + compilerClass);
97     }
98     catch (Throwable JavaDoc t) {
99       _log.log(this + "._recreateCompiler() threw exception " + t);
100     /* don't do anything. realCompiler stays null. */
101     }
102     
103   }
104
105
106 /** Compile the given files.
107   * @param files Source files to compile.
108   * @param classPath Support jars or directories that should be on the classpath. If @code{null}, the default is used.
109   * @param sourcePath Location of additional sources to be compiled on-demand. If @code{null}, the default is used.
110   * @param destination Location (directory) for compiled classes. If @code{null}, the default in-place location is used.
111   * @param bootClassPath The bootclasspath (contains Java API jars or directories); should be consistent with @code{sourceVersion}
112   * If @code{null}, the default is used.
113   * @param sourceVersion The language version of the sources. Should be consistent with @code{bootClassPath}. If @code{null},
114   * the default is used.
115   * @param showWarnings Whether compiler warnings should be shown or ignored.
116   * @return Errors that occurred. If no errors, should be zero length (not null).
117   */

118   public List JavaDoc<? extends CompilerError> compile(List JavaDoc<? extends File JavaDoc> files, List JavaDoc<? extends File JavaDoc> classPath,
119                                                List JavaDoc<? extends File JavaDoc> sourcePath, File JavaDoc destination,
120                                                List JavaDoc<? extends File JavaDoc> bootClassPath, String JavaDoc sourceVersion, boolean showWarnings) {
121     _recreateCompiler();
122     _log.log("realCompiler is " + _realCompiler.getClass());
123     return _realCompiler.compile(files, classPath, sourcePath, destination, bootClassPath, sourceVersion, showWarnings);
124   }
125
126   /** Indicates whether this compiler is actually available. As in: Is it installed and located? This method should
127    * load the compiler class, which should hopefully prove whether the class can load. If this method returns true,
128    * the {@link #compile} method should not fail due to class not being found.
129    */

130   public boolean isAvailable() {
131     _log.log("CompilerProxy.isAvailable() called for " + getClass() + " _realCompiler = " + _realCompiler);
132     if (_realCompiler == null) return false;
133     else return _realCompiler.isAvailable();
134   }
135
136   /** Returns the name of this compiler, appropriate to show to the user. */
137   public String JavaDoc getName() {
138     if (! isAvailable()) return "(unavailable)";
139     return _realCompiler.getName();
140   }
141
142   /** Should return info about compiler, at least including name. */
143   public String JavaDoc toString() { return getName(); }
144
145 }
146
147
148
149
Popular Tags