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.commons.jci.compilers;
019:
020: import groovy.lang.GroovyClassLoader;
021:
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.apache.commons.jci.problems.CompilationProblem;
028: import org.apache.commons.jci.readers.ResourceReader;
029: import org.apache.commons.jci.stores.ResourceStore;
030: import org.apache.commons.jci.utils.ConversionUtils;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.codehaus.groovy.control.CompilationFailedException;
034: import org.codehaus.groovy.control.CompilationUnit;
035: import org.codehaus.groovy.control.CompilerConfiguration;
036: import org.codehaus.groovy.control.ErrorCollector;
037: import org.codehaus.groovy.control.MultipleCompilationErrorsException;
038: import org.codehaus.groovy.control.Phases;
039: import org.codehaus.groovy.control.SourceUnit;
040: import org.codehaus.groovy.control.messages.Message;
041: import org.codehaus.groovy.control.messages.WarningMessage;
042: import org.codehaus.groovy.tools.GroovyClass;
043:
044: /**
045: * Groovy implementation of the JavaCompiler interface
046: *
047: * @author tcurdt
048: */
049: public final class GroovyJavaCompiler extends AbstractJavaCompiler {
050:
051: private final Log log = LogFactory.getLog(GroovyJavaCompiler.class);
052: private final GroovyJavaCompilerSettings defaultSettings;
053:
054: public GroovyJavaCompiler() {
055: defaultSettings = new GroovyJavaCompilerSettings(
056: new CompilerConfiguration());
057: }
058:
059: public CompilationResult compile(final String[] pResourceNames,
060: final ResourceReader pReader, final ResourceStore pStore,
061: final ClassLoader pClassLoader,
062: final JavaCompilerSettings pSettings) {
063:
064: final CompilerConfiguration configuration = ((GroovyJavaCompilerSettings) pSettings)
065: .getCompilerConfiguration();
066: final ErrorCollector collector = new ErrorCollector(
067: configuration);
068: final GroovyClassLoader groovyClassLoader = new GroovyClassLoader(
069: pClassLoader);
070: final CompilationUnit unit = new CompilationUnit(configuration,
071: null, groovyClassLoader);
072: final SourceUnit[] source = new SourceUnit[pResourceNames.length];
073: for (int i = 0; i < source.length; i++) {
074: final String resourceName = pResourceNames[i];
075: source[i] = new SourceUnit(ConversionUtils
076: .convertResourceToClassName(resourceName),
077: new String(pReader.getBytes(resourceName)), // FIXME delay the read
078: configuration, groovyClassLoader, collector);
079: unit.addSource(source[i]);
080: }
081:
082: final Collection problems = new ArrayList();
083:
084: try {
085: log.debug("compiling");
086: unit.compile(Phases.CLASS_GENERATION);
087:
088: final List classes = unit.getClasses();
089: for (final Iterator it = classes.iterator(); it.hasNext();) {
090: final GroovyClass clazz = (GroovyClass) it.next();
091: final byte[] bytes = clazz.getBytes();
092: pStore.write(ConversionUtils
093: .convertClassToResourcePath(clazz.getName()),
094: bytes);
095: }
096: } catch (final MultipleCompilationErrorsException e) {
097: final ErrorCollector col = e.getErrorCollector();
098: final Collection warnings = col.getWarnings();
099: if (warnings != null) {
100: for (final Iterator it = warnings.iterator(); it
101: .hasNext();) {
102: final WarningMessage warning = (WarningMessage) it
103: .next();
104: final CompilationProblem problem = new GroovyCompilationProblem(
105: warning);
106: if (problemHandler != null) {
107: problemHandler.handle(problem);
108: }
109: problems.add(problem);
110: }
111: }
112:
113: final Collection errors = col.getErrors();
114: if (errors != null) {
115: for (final Iterator it = errors.iterator(); it
116: .hasNext();) {
117: final Message message = (Message) it.next();
118: final CompilationProblem problem = new GroovyCompilationProblem(
119: message);
120: if (problemHandler != null) {
121: problemHandler.handle(problem);
122: }
123: problems.add(problem);
124: }
125: }
126: } catch (CompilationFailedException e) {
127: throw new RuntimeException("no expected");
128: }
129:
130: final CompilationProblem[] result = new CompilationProblem[problems
131: .size()];
132: problems.toArray(result);
133: return new CompilationResult(result);
134: }
135:
136: public JavaCompilerSettings createDefaultSettings() {
137: return defaultSettings;
138: }
139: }
|