Source Code Cross Referenced for Compiler.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » tools » javac » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.tools.javac 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.harmony.tools.javac;
019:
020:        import java.io.File;
021:        import java.io.PrintWriter;
022:        import java.lang.reflect.Constructor;
023:        import java.lang.reflect.InvocationTargetException;
024:        import java.lang.reflect.Method;
025:        import java.net.MalformedURLException;
026:        import java.net.URL;
027:        import java.net.URLClassLoader;
028:        import org.apache.harmony.tools.toolutils.Util;
029:
030:        /**
031:         * A proxy to the Java source code compiler itself.
032:         */
033:        class Compiler {
034:
035:            /* FIXME: Hard-coded for now, the name of the ECJ JAR file */
036:            static final String ECJ_JAR_FILE = "ecj_3.3.jar"; //$NON-NLS-1$
037:
038:            static final String TOOLS_JAR_FILE = "tools.jar"; //$NON-NLS-1$
039:
040:            /* The name of the ECJ compiler class */
041:            static final String MAIN_CLASS_NAME = "org.eclipse.jdt.internal.compiler.batch.Main"; //$NON-NLS-1$
042:
043:            /*
044:             * Invokes the compiler with the given command-line arguments. The supported
045:             * arguments can be determined form the usage message.
046:             * 
047:             * Answers the result of the compilation from ECJ; i.e. true if the compile
048:             * succeeded, and false otherwise.
049:             */
050:            public static boolean main(String[] args) {
051:                return main(args, Util.getDefaultWriter(System.out), Util
052:                        .getDefaultWriter(System.err));
053:            }
054:
055:            public static boolean main(String[] args, PrintWriter out,
056:                    PrintWriter err) {
057:                Compiler myself = new Compiler(out, err);
058:
059:                // If there is a problem invoking the method, simply dump the trace for
060:                // now
061:                try {
062:                    Object result = myself.staticCompileMth.invoke(
063:                            myself.mainInst, new Object[] { args });
064:                    return (Boolean) result;
065:                } catch (IllegalArgumentException e) {
066:                    e.printStackTrace();
067:                } catch (IllegalAccessException e) {
068:                    e.printStackTrace();
069:                } catch (InvocationTargetException e) {
070:                    e.printStackTrace();
071:                }
072:                return false;
073:            }
074:
075:            // Reference to ECJ 'Main' compiler class.
076:            Class<?> ecjCompilerClass;
077:
078:            // An instance of the ECJ compiler
079:            Object mainInst;
080:
081:            // The Main#printUsage() method.
082:            Method printUsageMth;
083:
084:            // The static Main#compile(string[]) method on the ECJ compiler
085:            Method staticCompileMth;
086:
087:            /**
088:             * Default constructor. Returns a new initialized instance of the Java
089:             * compiler.
090:             */
091:            public Compiler(PrintWriter out, PrintWriter err) {
092:                super ();
093:                initialize(out, err);
094:            }
095:
096:            /*
097:             * Initialize our local variables. Called during type construction.
098:             */
099:            protected void initialize(PrintWriter out, PrintWriter err) {
100:                try {
101:                    initializeMainClass();
102:                    initializeInstance(out, err);
103:                    initializeMethods();
104:                } catch (Exception e) {
105:                    // If there is a problem we log it to the console
106:                    e.printStackTrace();
107:                }
108:            }
109:
110:            /*
111:             * Defines the local instance of the ECJ compiler
112:             */
113:            protected void initializeInstance(PrintWriter out, PrintWriter err)
114:                    throws SecurityException, NoSuchMethodException,
115:                    IllegalArgumentException, InstantiationException,
116:                    IllegalAccessException, InvocationTargetException {
117:
118:                // Create a new instance of the compiler
119:                Constructor<?> ctor = ecjCompilerClass
120:                        .getConstructor(new Class[] { PrintWriter.class,
121:                                PrintWriter.class, Boolean.TYPE });
122:
123:                mainInst = ctor.newInstance(new Object[] { out, err,
124:                        Boolean.FALSE });
125:            }
126:
127:            /*
128:             * Defines the compiler class from the ECJ jar file
129:             */
130:            protected void initializeMainClass() throws ClassNotFoundException,
131:                    SecurityException, NoSuchMethodException,
132:                    MalformedURLException, IllegalArgumentException,
133:                    InstantiationException, IllegalAccessException,
134:                    InvocationTargetException {
135:
136:                // Find the ECJ JAR file, prefer those found near loaders
137:                URL ecjURL = searchLoaders();
138:                if (ecjURL == null) {
139:                    ecjURL = searchPaths();
140:                }
141:                if (ecjURL == null) {
142:                    throw new RuntimeException("Cannot find file "
143:                            + ECJ_JAR_FILE);
144:                }
145:
146:                // Load the ECJ main class
147:                URLClassLoader loader = new URLClassLoader(new URL[] { ecjURL });
148:                ecjCompilerClass = loader.loadClass(MAIN_CLASS_NAME);
149:            }
150:
151:            /*
152:             * Looks for the ECJ JAR file in the current working directory, and in the
153:             * jdk/lib of the current runtime. Answers with a URL of the JAR file if
154:             * found, or null if not found.
155:             */
156:            private URL searchPaths() throws MalformedURLException {
157:                // Search in current working directory
158:                File cwdFile = new File(ECJ_JAR_FILE);
159:                if (cwdFile.exists()) {
160:                    return cwdFile.toURL();
161:                }
162:
163:                // Look for it via the java.home
164:                File javaHomeFile = new File(System.getProperty("java.home")); //$NON-NLS-1$
165:                String pathFromJDK = "lib" + File.separator + ECJ_JAR_FILE; //$NON-NLS-1$
166:
167:                // Is java.home pointing at a JDK?
168:                File jdkBasedFile = new File(javaHomeFile, pathFromJDK);
169:                if (jdkBasedFile.exists()) {
170:                    return jdkBasedFile.toURL();
171:                }
172:                // Maybe it is pointing at a JRE.
173:                File jdkHomeFile = javaHomeFile.getParentFile();
174:                if (jdkHomeFile == null) {
175:                    return null;
176:                }
177:                File jreBasedFile = new File(jdkHomeFile, pathFromJDK);
178:                if (jreBasedFile.exists()) {
179:                    return jreBasedFile.toURL();
180:                }
181:
182:                // We didn't find it
183:                return null;
184:            }
185:
186:            /*
187:             * Find the ECJ jar by searching for the tools.jar location and figuring
188:             * that it is alongside that.
189:             */
190:            private URL searchLoaders() throws MalformedURLException {
191:                URLClassLoader bogusLoader = new URLClassLoader(new URL[] {});
192:                ClassLoader parentLoader = bogusLoader.getParent();
193:                while (parentLoader instanceof  URLClassLoader) {
194:                    URLClassLoader parentURLLoader = (URLClassLoader) parentLoader;
195:                    URL[] uls = parentURLLoader.getURLs();
196:                    for (int i = 0; i < uls.length; i++) {
197:                        URL l = uls[i];
198:                        String filename = new File(l.getFile()).getName();
199:                        if (filename.equals(TOOLS_JAR_FILE)) {
200:                            return new URL(l, ECJ_JAR_FILE);
201:                        }
202:                    }
203:                    // Not found here, move up a level
204:                    parentLoader = parentLoader.getParent();
205:                }
206:                // We didn't find it
207:                return null;
208:            }
209:
210:            /*
211:             * Initialize our local references to compiler methods we may wish to
212:             * invoke.
213:             */
214:            protected void initializeMethods() throws SecurityException,
215:                    NoSuchMethodException {
216:                staticCompileMth = ecjCompilerClass.getMethod("compile", //$NON-NLS-1$
217:                        new Class[] { String[].class });
218:                printUsageMth = ecjCompilerClass.getMethod(
219:                        "printUsage", (Class[]) null); //$NON-NLS-1$
220:            }
221:
222:            /**
223:             * Prints the compiler usage message out on the console.
224:             */
225:            public void printUsage() {
226:                // If there is a problem invoking the method, simply dump the trace for
227:                // now
228:                try {
229:                    printUsageMth.invoke(mainInst);
230:                } catch (IllegalArgumentException e) {
231:                    e.printStackTrace();
232:                } catch (IllegalAccessException e) {
233:                    e.printStackTrace();
234:                } catch (InvocationTargetException e) {
235:                    e.printStackTrace();
236:                }
237:            }
238:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.