001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.jxc;
038:
039: import java.lang.reflect.Method;
040:
041: import com.sun.mirror.apt.AnnotationProcessorFactory;
042:
043: import org.apache.tools.ant.BuildException;
044: import org.apache.tools.ant.Project;
045: import org.apache.tools.ant.taskdefs.Javac;
046: import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter;
047: import org.apache.tools.ant.types.Commandline;
048:
049: /**
050: * Base class for tasks that eventually invoke APT.
051: *
052: * @author Kohsuke Kawaguchi
053: */
054: public abstract class AptBasedTask extends Javac {
055:
056: public AptBasedTask() {
057: setExecutable("apt");
058: }
059:
060: /**
061: * Implemented by the derived class to set up command line switches passed to Apt.
062: */
063: protected abstract void setupCommandlineSwitches(Commandline cmd);
064:
065: /**
066: * Common parts between {@link InternalAptAdapter} and {@link ExternalAptAdapter}.
067: */
068: private abstract class AptAdapter extends DefaultCompilerAdapter {
069: protected AptAdapter() {
070: setJavac(AptBasedTask.this );
071: }
072:
073: protected Commandline setupModernJavacCommandlineSwitches(
074: Commandline cmd) {
075: super .setupModernJavacCommandlineSwitches(cmd);
076: setupCommandlineSwitches(cmd);
077: return cmd;
078: }
079: }
080:
081: /**
082: * Adapter to invoke Apt internally.
083: */
084: private final class InternalAptAdapter extends AptAdapter {
085: public boolean execute() throws BuildException {
086: Commandline cmd = setupModernJavacCommand();
087:
088: try {
089: Class apt = Class.forName("com.sun.tools.apt.Main");
090: Method process;
091: try {
092: process = apt.getMethod("process", new Class[] {
093: AnnotationProcessorFactory.class,
094: String[].class });
095: } catch (NoSuchMethodException e) {
096: throw new BuildException(
097: "JDK 1.5.0_01 or later is necessary", e,
098: location);
099: }
100:
101: int result = ((Integer) process.invoke(null,
102: new Object[] { createFactory(),
103: cmd.getArguments() })).intValue();
104: return result == 0;
105: } catch (BuildException e) {
106: throw e;
107: } catch (Exception ex) {
108: throw new BuildException("Error starting apt", ex,
109: location);
110: }
111: }
112: }
113:
114: /**
115: * Creates a facotry that does the actual job.
116: */
117: protected abstract AnnotationProcessorFactory createFactory();
118:
119: // /**
120: // * Adapter to invoke Apt externally.
121: // */
122: // private final class ExternalAptAdapter extends AptAdapter {
123: // public boolean execute() throws BuildException {
124: // Commandline cmd = setupModernJavacCommand();
125: // return executeExternalCompile(cmd.getArguments(),-1)==0;
126: // }
127: // }
128:
129: protected void compile() {
130: if (compileList.length == 0)
131: return;
132:
133: log(getCompilationMessage() + compileList.length
134: + " source file" + (compileList.length == 1 ? "" : "s"));
135:
136: if (listFiles) {
137: for (int i = 0; i < compileList.length; i++) {
138: String filename = compileList[i].getAbsolutePath();
139: log(filename);
140: }
141: }
142:
143: AptAdapter apt;
144: // if(isForkedJavac())
145: // apt = new ExternalAptAdapter();
146: // else
147: apt = new InternalAptAdapter();
148:
149: // compile
150: if (!apt.execute()) {
151: if (failOnError) {
152: throw new BuildException(getFailedMessage(),
153: getLocation());
154: } else {
155: log(getFailedMessage(), Project.MSG_ERR);
156: }
157: }
158: }
159:
160: protected abstract String getCompilationMessage();
161:
162: protected abstract String getFailedMessage();
163: }
|