KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > JavaCWrapper


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base;
26
27 import java.io.File JavaDoc;
28
29 import java.util.*;
30 import java.io.OutputStream JavaDoc;
31 import java.lang.reflect.*;
32
33 import org.aspectj.compiler.base.*;
34 import org.aspectj.compiler.base.ast.World;
35
36 import org.aspectj.compiler.base.JavaCompiler;
37 import org.aspectj.compiler.base.CompilerObject;
38
39 public class JavaCWrapper extends CompilerObject {
40
41     public JavaCWrapper(JavaCompiler compiler) {
42         super(compiler);
43     }
44
45     private String JavaDoc addClassPath(File JavaDoc pathdir) {
46         String JavaDoc classpath = getOptions().classpath; //System.getProperty("java.class.path");
47
if (classpath != null) return pathdir.getPath()+File.pathSeparator+classpath;
48         classpath = System.getProperty("java.class.path");
49         if (classpath != null) return pathdir.getPath()+File.pathSeparator+classpath;
50         return pathdir.getPath();
51     }
52
53     public boolean compile(File JavaDoc inputdir, File JavaDoc outputdir, File JavaDoc[] files) {
54         List args = new ArrayList(files.length+6);
55
56         args.add("-d");
57         args.add(inputdir.getPath());
58         args.add("-classpath");
59         args.add(addClassPath(outputdir));
60         if (getOptions().deprecation) {
61             args.add("-deprecation");
62         }
63         if (getOptions().O) {
64             args.add("-O");
65         }
66         if (getOptions().g) {
67             args.add("-g");
68         }
69         if (getOptions().source.equals("1.4")) {
70             args.add("-source");
71             args.add("1.4");
72         }
73         if (getOptions().target != null) {
74             args.add("-target");
75             args.add(getOptions().target);
76         }
77
78         if (getOptions().bootclasspath != null) {
79             args.add("-bootclasspath");
80             args.add(getOptions().bootclasspath);
81         }
82
83         if (getOptions().extdirs != null) {
84             args.add("-extdirs");
85             args.add(getOptions().extdirs);
86         }
87
88         //add javafiles to args
89
for(int i=0; i<files.length; i++) {
90             args.add(files[i].getPath());
91         }
92
93         /*
94         StringBuffer cmd = new StringBuffer();
95         cmd.append("javac");
96         for(int i=0; i<args.size(); i++) {
97             cmd.append(" ");
98             cmd.append(args.get(i));
99         }
100         */

101
102
103         String JavaDoc[] argsArray = (String JavaDoc[])args.toArray(new String JavaDoc[0]);
104         //System.out.println("javac "+args);
105

106         Class JavaDoc compilerClass;
107
108         try {
109             compilerClass = Class.forName("com.sun.tools.javac.Main");
110             return doModernCompile(compilerClass, argsArray);
111         } catch (ClassNotFoundException JavaDoc ce1) {
112             try {
113                 compilerClass = Class.forName("sun.tools.javac.Main");
114                 return doClassicCompile(compilerClass, argsArray);
115             } catch (ClassNotFoundException JavaDoc ce2) {
116                 showNoJavacError();
117                 return false;
118             }
119         }
120     }
121
122
123     void showNoJavacError() {
124         System.err.println("The -usejavac flag requires that com.sun.tools.javac.Main");
125         System.err.println("is on your classpath. Generally, this will involve editing");
126         System.err.println("your launch script (ajc.bat or ajc) to include tools.jar");
127         System.err.println("on the classpath that is passed to the jvm which runs ajc.");
128         System.err.println("See README-TOOLS.html for suggestions on how to fix this.");
129         throw new ExitRequestException(-1);
130     }
131
132
133
134     boolean doModernCompile(Class JavaDoc compiler, String JavaDoc[] args) {
135         getCompiler().showMessage(" modern compile");
136         // use reflection to work with both 1.3 and 1.4
137
try {
138             Method m = compiler.getMethod("compile", new Class JavaDoc[] {String JavaDoc[].class});
139             Object JavaDoc inst = compiler.newInstance();
140             Integer JavaDoc i = (Integer JavaDoc)m.invoke(inst, new Object JavaDoc[] {args});
141             return i.intValue() == 0;
142         } catch (Exception JavaDoc e) {
143             System.out.println(e);
144             return false;
145         }
146     }
147
148     boolean doClassicCompile(Class JavaDoc compiler, String JavaDoc[] args) {
149         getCompiler().showMessage(" classic compile");
150         // use reflection so that we don't depend on the compiler sources to build
151
//sun.tools.javac.Main main = new sun.tools.javac.Main(System.err, "ajc");
152
//return main.compile(args);
153
try {
154             Constructor c = compiler.getConstructor(new Class JavaDoc[] {OutputStream JavaDoc.class, String JavaDoc.class});
155             Object JavaDoc inst = c.newInstance(new Object JavaDoc[] {System.err, "ajc"});
156             Method m = compiler.getMethod("compile", new Class JavaDoc[] {String JavaDoc[].class});
157             Boolean JavaDoc b = (Boolean JavaDoc)m.invoke(inst, new Object JavaDoc[] {args});
158             return b.booleanValue();
159         } catch (Exception JavaDoc e) {
160             System.out.println(e);
161             return false;
162         }
163     }
164 }
165
Popular Tags