001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model;
038:
039: import java.util.List;
040: import java.io.File;
041:
042: import edu.rice.cs.plt.reflect.ReflectUtil;
043: import edu.rice.cs.plt.reflect.JavaVersion;
044: import edu.rice.cs.plt.reflect.JavaVersion.FullVersion;
045: import edu.rice.cs.plt.reflect.ReflectException;
046: import edu.rice.cs.plt.io.IOUtil;
047: import edu.rice.cs.plt.iter.IterUtil;
048:
049: import edu.rice.cs.drjava.model.compiler.CompilerInterface;
050: import edu.rice.cs.drjava.model.compiler.NoCompilerAvailable;
051: import edu.rice.cs.drjava.model.debug.Debugger;
052: import edu.rice.cs.drjava.model.debug.NoDebuggerAvailable;
053: import edu.rice.cs.drjava.model.javadoc.JavadocModel;
054: import edu.rice.cs.drjava.model.javadoc.NoJavadocAvailable;
055: import edu.rice.cs.drjava.model.javadoc.DefaultJavadocModel;
056:
057: /** The class that provides methods for interfacing to facilities in tools.jar that do not have published interfaces
058: * (or did not have published interfaces at the time DrJava was originally written).
059: */
060: public class JDKToolsLibrary {
061:
062: private final FullVersion _version;
063: private final CompilerInterface _compiler;
064: private final Debugger _debugger;
065: private final JavadocModel _javadoc;
066:
067: protected JDKToolsLibrary(FullVersion version,
068: CompilerInterface compiler, Debugger debugger,
069: JavadocModel javadoc) {
070: _version = version;
071: _compiler = compiler;
072: _debugger = debugger;
073: _javadoc = javadoc;
074: }
075:
076: public FullVersion version() {
077: return _version;
078: }
079:
080: public CompilerInterface compiler() {
081: return _compiler;
082: }
083:
084: public Debugger debugger() {
085: return _debugger;
086: }
087:
088: public JavadocModel javadoc() {
089: return _javadoc;
090: }
091:
092: public boolean isValid() {
093: return _compiler.isAvailable() || _debugger.isAvailable()
094: || _javadoc.isAvailable();
095: }
096:
097: public String toString() {
098: return "JDK library " + _version.versionString();
099: }
100:
101: protected static String adapterForCompiler(JavaVersion version) {
102: switch (version) {
103: case JAVA_6:
104: return "edu.rice.cs.drjava.model.compiler.Javac160Compiler";
105: case JAVA_5:
106: return "edu.rice.cs.drjava.model.compiler.Javac150Compiler";
107: case JAVA_1_4:
108: return "edu.rice.cs.drjava.model.compiler.Javac141Compiler";
109: default:
110: return null;
111: }
112: }
113:
114: protected static String adapterForDebugger(JavaVersion version) {
115: switch (version) {
116: case JAVA_6:
117: return "edu.rice.cs.drjava.model.debug.jpda.JPDADebugger";
118: case JAVA_5:
119: return "edu.rice.cs.drjava.model.debug.jpda.JPDADebugger";
120: case JAVA_1_4:
121: return "edu.rice.cs.drjava.model.debug.jpda.JPDADebugger";
122: default:
123: return null;
124: }
125: }
126:
127: public static JDKToolsLibrary makeFromRuntime(GlobalModel model) {
128: FullVersion version = JavaVersion.CURRENT_FULL;
129:
130: CompilerInterface compiler = NoCompilerAvailable.ONLY;
131: String compilerAdapter = adapterForCompiler(version
132: .majorVersion());
133: if (compilerAdapter != null) {
134: List<File> bootClassPath = null;
135: String bootProp = System.getProperty("sun.boot.class.path");
136: if (bootProp != null) {
137: bootClassPath = IterUtil.asList(IOUtil
138: .parsePath(bootProp));
139: }
140: try {
141: Class[] sig = new Class[] { FullVersion.class,
142: String.class, List.class };
143: Object[] args = new Object[] { version,
144: "the runtime class path", bootClassPath };
145: CompilerInterface attempt = (CompilerInterface) ReflectUtil
146: .loadObject(compilerAdapter, sig, args);
147: if (attempt.isAvailable()) {
148: compiler = attempt;
149: }
150: } catch (ReflectException e) { /* can't load */
151: } catch (LinkageError e) { /* can't load */
152: }
153: }
154:
155: Debugger debugger = NoDebuggerAvailable.ONLY;
156: String debuggerAdapter = adapterForDebugger(version
157: .majorVersion());
158: if (debuggerAdapter != null) {
159: try {
160: Debugger attempt = (Debugger) ReflectUtil.loadObject(
161: debuggerAdapter,
162: new Class[] { GlobalModel.class }, model);
163: if (attempt.isAvailable()) {
164: debugger = attempt;
165: }
166: } catch (ReflectException e) { /* can't load */
167: } catch (LinkageError e) { /* can't load */
168: }
169: }
170:
171: JavadocModel javadoc = new NoJavadocAvailable(model);
172: try {
173: Class.forName("com.sun.tools.javadoc.Main");
174: javadoc = new DefaultJavadocModel(model, null,
175: GlobalModel.RUNTIME_CLASS_PATH);
176: } catch (ClassNotFoundException e) { /* can't load */
177: } catch (LinkageError e) { /* can't load (probably not necessary, but might as well catch it) */
178: }
179:
180: return new JDKToolsLibrary(version, compiler, debugger, javadoc);
181: }
182:
183: }
|