|
1 | | -/*BEGIN_COPYRIGHT_BLOCK |
2 | | - * |
3 | | - * Copyright (c) 2001-2009, JavaPLT group at Rice University (drjava@rice.edu) |
4 | | - * All rights reserved. |
5 | | - * |
6 | | - * Redistribution and use in source and binary forms, with or without |
7 | | - * modification, are permitted provided that the following conditions are met: |
8 | | - * * Redistributions of source code must retain the above copyright |
9 | | - * notice, this list of conditions and the following disclaimer. |
10 | | - * * Redistributions in binary form must reproduce the above copyright |
11 | | - * notice, this list of conditions and the following disclaimer in the |
12 | | - * documentation and/or other materials provided with the distribution. |
13 | | - * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the |
14 | | - * names of its contributors may be used to endorse or promote products |
15 | | - * derived from this software without specific prior written permission. |
16 | | - * |
17 | | - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | - * |
29 | | - * This software is Open Source Initiative approved Open Source Software. |
30 | | - * Open Source Initative Approved is a trademark of the Open Source Initiative. |
31 | | - * |
32 | | - * This file is part of DrJava. Download the current version of this project |
33 | | - * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ |
34 | | - * |
35 | | - * END_COPYRIGHT_BLOCK*/ |
36 | | - |
37 | | -package edu.rice.cs.drjava.model; |
38 | | - |
39 | | -import java.util.List; |
40 | | -import java.io.File; |
41 | | - |
42 | | -import edu.rice.cs.plt.reflect.ReflectUtil; |
43 | | -import edu.rice.cs.plt.reflect.JavaVersion; |
44 | | -import edu.rice.cs.plt.reflect.JavaVersion.FullVersion; |
45 | | -import edu.rice.cs.plt.reflect.ReflectException; |
46 | | -import edu.rice.cs.plt.io.IOUtil; |
47 | | -import edu.rice.cs.plt.collect.CollectUtil; |
48 | | - |
49 | | -import edu.rice.cs.drjava.model.compiler.CompilerInterface; |
50 | | -import edu.rice.cs.drjava.model.compiler.NoCompilerAvailable; |
51 | | -import edu.rice.cs.drjava.model.debug.Debugger; |
52 | | -import edu.rice.cs.drjava.model.debug.NoDebuggerAvailable; |
53 | | -import edu.rice.cs.drjava.model.javadoc.JavadocModel; |
54 | | -import edu.rice.cs.drjava.model.javadoc.NoJavadocAvailable; |
55 | | -import edu.rice.cs.drjava.model.javadoc.DefaultJavadocModel; |
56 | | - |
57 | | -/** |
58 | | - * Provides dynamic access to the interface of a JDK's tools.jar classes. This level of indirection |
59 | | - * eliminates the need to have specific tools.jar classes available statically (and the resulting need |
60 | | - * to reset the JVM if they are not), and makes it possible to interface with multiple tools.jar |
61 | | - * libraries simultaneously. |
62 | | - */ |
63 | | -public class JDKToolsLibrary { |
64 | | - |
65 | | - private final FullVersion _version; |
66 | | - private final CompilerInterface _compiler; |
67 | | - private final Debugger _debugger; |
68 | | - private final JavadocModel _javadoc; |
69 | | - |
70 | | - protected JDKToolsLibrary(FullVersion version, CompilerInterface compiler, Debugger debugger, |
71 | | - JavadocModel javadoc) { |
72 | | - _version = version; |
73 | | - _compiler = compiler; |
74 | | - _debugger = debugger; |
75 | | - _javadoc = javadoc; |
76 | | - } |
77 | | - |
78 | | - public FullVersion version() { return _version; } |
79 | | - |
80 | | - public CompilerInterface compiler() { return _compiler; } |
81 | | - |
82 | | - public Debugger debugger() { return _debugger; } |
83 | | - |
84 | | - public JavadocModel javadoc() { return _javadoc; } |
85 | | - |
86 | | - public boolean isValid() { |
87 | | - return _compiler.isAvailable() || _debugger.isAvailable() || _javadoc.isAvailable(); |
88 | | - } |
89 | | - |
90 | | - public String toString() { return "JDK library " + _version.versionString(); } |
91 | | - |
92 | | - protected static String adapterForCompiler(JavaVersion.FullVersion version) { |
93 | | - switch (version.majorVersion()) { |
94 | | - case JAVA_6: { |
95 | | - switch (version.vendor()) { |
96 | | - case OPENJDK: return "edu.rice.cs.drjava.model.compiler.Javac160OpenJDKCompiler"; |
97 | | - case MINT: return "edu.rice.cs.drjava.model.compiler.MintCompiler"; |
98 | | - default: return "edu.rice.cs.drjava.model.compiler.Javac160Compiler"; |
99 | | - } |
100 | | - } |
101 | | - case JAVA_5: return "edu.rice.cs.drjava.model.compiler.Javac150Compiler"; |
102 | | - default: return null; |
103 | | - } |
104 | | - } |
105 | | - |
106 | | - protected static String adapterForDebugger(JavaVersion.FullVersion version) { |
107 | | - switch (version.majorVersion()) { |
108 | | - case JAVA_6: return "edu.rice.cs.drjava.model.debug.jpda.JPDADebugger"; |
109 | | - case JAVA_5: return "edu.rice.cs.drjava.model.debug.jpda.JPDADebugger"; |
110 | | - default: return null; |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - /** Create a JDKToolsLibrary from the runtime class path (or, more accurately, from the class |
115 | | - * loader that loaded this class. |
116 | | - */ |
117 | | - public static JDKToolsLibrary makeFromRuntime(GlobalModel model) { |
118 | | - FullVersion version = JavaVersion.CURRENT_FULL; |
119 | | - |
120 | | - CompilerInterface compiler = NoCompilerAvailable.ONLY; |
121 | | - String compilerAdapter = adapterForCompiler(version); |
122 | | - if (compilerAdapter != null) { |
123 | | - List<File> bootClassPath = null; |
124 | | - String bootProp = System.getProperty("sun.boot.class.path"); |
125 | | - if (bootProp != null) { bootClassPath = CollectUtil.makeList(IOUtil.parsePath(bootProp)); } |
126 | | - try { |
127 | | - Class<?>[] sig = { FullVersion.class, String.class, List.class }; |
128 | | - Object[] args = { version, "the runtime class path", bootClassPath }; |
129 | | - CompilerInterface attempt = (CompilerInterface) ReflectUtil.loadObject(compilerAdapter, sig, args); |
130 | | - if (attempt.isAvailable()) { compiler = attempt; } |
131 | | - } |
132 | | - catch (ReflectException e) { /* can't load */ } |
133 | | - catch (LinkageError e) { /* can't load */ } |
134 | | - } |
135 | | - |
136 | | - Debugger debugger = NoDebuggerAvailable.ONLY; |
137 | | - String debuggerAdapter = adapterForDebugger(version); |
138 | | - if (debuggerAdapter != null) { |
139 | | - try { |
140 | | - Debugger attempt = (Debugger) ReflectUtil.loadObject(debuggerAdapter, new Class<?>[]{GlobalModel.class}, model); |
141 | | - if (attempt.isAvailable()) { debugger = attempt; } |
142 | | - } |
143 | | - catch (ReflectException e) { /* can't load */ } |
144 | | - catch (LinkageError e) { /* can't load */ } |
145 | | - } |
146 | | - |
147 | | - JavadocModel javadoc = new NoJavadocAvailable(model); |
148 | | - try { |
149 | | - Class.forName("com.sun.tools.javadoc.Main"); |
150 | | - javadoc = new DefaultJavadocModel(model, null, ReflectUtil.SYSTEM_CLASS_PATH); |
151 | | - } |
152 | | - catch (ClassNotFoundException e) { /* can't load */ } |
153 | | - catch (LinkageError e) { /* can't load (probably not necessary, but might as well catch it) */ } |
154 | | - |
155 | | - return new JDKToolsLibrary(version, compiler, debugger, javadoc); |
156 | | - } |
157 | | - |
158 | | -} |
| 1 | +/*BEGIN_COPYRIGHT_BLOCK |
| 2 | + * |
| 3 | + * Copyright (c) 2001-2009, JavaPLT group at Rice University (drjava@rice.edu) |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the |
| 14 | + * names of its contributors may be used to endorse or promote products |
| 15 | + * derived from this software without specific prior written permission. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 21 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | + * This software is Open Source Initiative approved Open Source Software. |
| 30 | + * Open Source Initative Approved is a trademark of the Open Source Initiative. |
| 31 | + * |
| 32 | + * This file is part of DrJava. Download the current version of this project |
| 33 | + * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ |
| 34 | + * |
| 35 | + * END_COPYRIGHT_BLOCK*/ |
| 36 | + |
| 37 | +package edu.rice.cs.drjava.model; |
| 38 | + |
| 39 | +import java.util.List; |
| 40 | +import java.io.File; |
| 41 | + |
| 42 | +import edu.rice.cs.plt.reflect.ReflectUtil; |
| 43 | +import edu.rice.cs.plt.reflect.JavaVersion; |
| 44 | +import edu.rice.cs.plt.reflect.JavaVersion.FullVersion; |
| 45 | +import edu.rice.cs.plt.reflect.ReflectException; |
| 46 | +import edu.rice.cs.plt.io.IOUtil; |
| 47 | +import edu.rice.cs.plt.collect.CollectUtil; |
| 48 | + |
| 49 | +import edu.rice.cs.drjava.model.compiler.CompilerInterface; |
| 50 | +import edu.rice.cs.drjava.model.compiler.NoCompilerAvailable; |
| 51 | +import edu.rice.cs.drjava.model.debug.Debugger; |
| 52 | +import edu.rice.cs.drjava.model.debug.NoDebuggerAvailable; |
| 53 | +import edu.rice.cs.drjava.model.javadoc.JavadocModel; |
| 54 | +import edu.rice.cs.drjava.model.javadoc.NoJavadocAvailable; |
| 55 | +import edu.rice.cs.drjava.model.javadoc.DefaultJavadocModel; |
| 56 | + |
| 57 | +/** |
| 58 | + * Provides dynamic access to the interface of a JDK's tools.jar classes. This level of indirection |
| 59 | + * eliminates the need to have specific tools.jar classes available statically (and the resulting need |
| 60 | + * to reset the JVM if they are not), and makes it possible to interface with multiple tools.jar |
| 61 | + * libraries simultaneously. |
| 62 | + */ |
| 63 | +public class JDKToolsLibrary { |
| 64 | + |
| 65 | + private final FullVersion _version; |
| 66 | + private final CompilerInterface _compiler; |
| 67 | + private final Debugger _debugger; |
| 68 | + private final JavadocModel _javadoc; |
| 69 | + |
| 70 | + protected JDKToolsLibrary(FullVersion version, CompilerInterface compiler, Debugger debugger, |
| 71 | + JavadocModel javadoc) { |
| 72 | + _version = version; |
| 73 | + _compiler = compiler; |
| 74 | + _debugger = debugger; |
| 75 | + _javadoc = javadoc; |
| 76 | + } |
| 77 | + |
| 78 | + public FullVersion version() { return _version; } |
| 79 | + |
| 80 | + public CompilerInterface compiler() { return _compiler; } |
| 81 | + |
| 82 | + public Debugger debugger() { return _debugger; } |
| 83 | + |
| 84 | + public JavadocModel javadoc() { return _javadoc; } |
| 85 | + |
| 86 | + public boolean isValid() { |
| 87 | + return _compiler.isAvailable() || _debugger.isAvailable() || _javadoc.isAvailable(); |
| 88 | + } |
| 89 | + |
| 90 | + public String toString() { return "JDK library " + _version.versionString(); } |
| 91 | + |
| 92 | + protected static String adapterForCompiler(JavaVersion.FullVersion version) { |
| 93 | + switch (version.majorVersion()) { |
| 94 | + case JAVA_6: { |
| 95 | + switch (version.vendor()) { |
| 96 | + case OPENJDK: return "edu.rice.cs.drjava.model.compiler.Javac160OpenJDKCompiler"; |
| 97 | + case MINT: return "edu.rice.cs.drjava.model.compiler.MintCompiler"; |
| 98 | + default: return "edu.rice.cs.drjava.model.compiler.Javac160Compiler"; |
| 99 | + } |
| 100 | + } |
| 101 | + case JAVA_5: return "edu.rice.cs.drjava.model.compiler.Javac150Compiler"; |
| 102 | + default: return null; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + protected static String adapterForDebugger(JavaVersion.FullVersion version) { |
| 107 | + switch (version.majorVersion()) { |
| 108 | + case JAVA_6: return "edu.rice.cs.drjava.model.debug.jpda.JPDADebugger"; |
| 109 | + case JAVA_5: return "edu.rice.cs.drjava.model.debug.jpda.JPDADebugger"; |
| 110 | + default: return null; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** Create a JDKToolsLibrary from the runtime class path (or, more accurately, from the class |
| 115 | + * loader that loaded this class. |
| 116 | + */ |
| 117 | + public static JDKToolsLibrary makeFromRuntime(GlobalModel model) { |
| 118 | + FullVersion version = JavaVersion.CURRENT_FULL; |
| 119 | + |
| 120 | + CompilerInterface compiler = NoCompilerAvailable.ONLY; |
| 121 | + String compilerAdapter = adapterForCompiler(version); |
| 122 | + if (compilerAdapter != null) { |
| 123 | + List<File> bootClassPath = null; |
| 124 | + String bootProp = System.getProperty("sun.boot.class.path"); |
| 125 | + if (bootProp != null) { bootClassPath = CollectUtil.makeList(IOUtil.parsePath(bootProp)); } |
| 126 | + try { |
| 127 | + Class<?>[] sig = { FullVersion.class, String.class, List.class }; |
| 128 | + Object[] args = { version, "the runtime class path", bootClassPath }; |
| 129 | + CompilerInterface attempt = (CompilerInterface) ReflectUtil.loadObject(compilerAdapter, sig, args); |
| 130 | + if (attempt.isAvailable()) { compiler = attempt; } |
| 131 | + } |
| 132 | + catch (ReflectException e) { /* can't load */ } |
| 133 | + catch (LinkageError e) { /* can't load */ } |
| 134 | + } |
| 135 | + |
| 136 | + Debugger debugger = NoDebuggerAvailable.ONLY; |
| 137 | + String debuggerAdapter = adapterForDebugger(version); |
| 138 | + if (debuggerAdapter != null) { |
| 139 | + try { |
| 140 | + Debugger attempt = (Debugger) ReflectUtil.loadObject(debuggerAdapter, new Class<?>[]{GlobalModel.class}, model); |
| 141 | + if (attempt.isAvailable()) { debugger = attempt; } |
| 142 | + } |
| 143 | + catch (ReflectException e) { /* can't load */ } |
| 144 | + catch (LinkageError e) { /* can't load */ } |
| 145 | + } |
| 146 | + |
| 147 | + JavadocModel javadoc = new NoJavadocAvailable(model); |
| 148 | + try { |
| 149 | + Class.forName("com.sun.tools.javadoc.Main"); |
| 150 | + javadoc = new DefaultJavadocModel(model, null, ReflectUtil.SYSTEM_CLASS_PATH); |
| 151 | + } |
| 152 | + catch (ClassNotFoundException e) { /* can't load */ } |
| 153 | + catch (LinkageError e) { /* can't load (probably not necessary, but might as well catch it) */ } |
| 154 | + |
| 155 | + return new JDKToolsLibrary(version, compiler, debugger, javadoc); |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments