diff options
author | Walter Harley | 2007-03-12 23:12:13 +0000 |
---|---|---|
committer | Walter Harley | 2007-03-12 23:12:13 +0000 |
commit | 4726a832ea714ad024e9712755d1579c273238b4 (patch) | |
tree | d7e9be78abd5acd7dffe95b5a96b0f023366d9a4 /org.eclipse.jdt.apt.pluggable.core/src | |
parent | 33a80cfa667cf790cdaef97941a4f7aeb25c24a5 (diff) | |
download | eclipse.jdt.core-4726a832ea714ad024e9712755d1579c273238b4.tar.gz eclipse.jdt.core-4726a832ea714ad024e9712755d1579c273238b4.tar.xz eclipse.jdt.core-4726a832ea714ad024e9712755d1579c273238b4.zip |
Initial check-in of IDE component of Java 6 annotation processing. Not yet operational.v20070314-1215
Diffstat (limited to 'org.eclipse.jdt.apt.pluggable.core/src')
8 files changed, 789 insertions, 0 deletions
diff --git a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/Apt6Plugin.java b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/Apt6Plugin.java new file mode 100644 index 0000000000..7391b627e4 --- /dev/null +++ b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/Apt6Plugin.java @@ -0,0 +1,131 @@ +/******************************************************************************* + * Copyright (c) 2007 BEA Systems, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wharley - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.internal.apt.pluggable.core; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.Plugin; +import org.eclipse.core.runtime.Status; +import org.osgi.framework.BundleContext; + +public class Apt6Plugin extends Plugin { + + private static final SimpleDateFormat TRACE_DATE_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS"); //$NON-NLS-1$ + + public static final String PLUGIN_ID = "org.eclipse.jdt.apt.pluggable.core"; //$NON-NLS-1$ + + /** + * Status IDs for system log entries. Must be unique per plugin. + */ + public static final int STATUS_EXCEPTION = 1; + + // Tracing options + public static boolean DEBUG = false; + public final static String APT_DEBUG_OPTION = Apt6Plugin.PLUGIN_ID + "/debug"; //$NON-NLS-1$ + + private static Apt6Plugin thePlugin = null; // singleton object + + public Apt6Plugin() { + } + + @Override + public void start(BundleContext context) throws Exception { + super.start(context); + thePlugin = this; + initDebugTracing(); + } + + private void initDebugTracing() { + String option = Platform.getDebugOption(APT_DEBUG_OPTION); + if (option != null) { + DEBUG = option.equalsIgnoreCase("true"); //$NON-NLS-1$ + } + } + + public static Apt6Plugin getPlugin() { + return thePlugin; + } + + /** + * Log a status message to the platform log. Use this for reporting exceptions. + * @param status + */ + public static void log(IStatus status) { + thePlugin.getLog().log(status); + } + + /** + * Convenience wrapper around log(IStatus), to log an exception + * with severity of ERROR. + */ + public static void log(Throwable e, String message) { + log(new Status(IStatus.ERROR, PLUGIN_ID, STATUS_EXCEPTION, message, e)); + } + + /** + * Convenience wrapper around log(IStatus), to log an exception + * with severity of WARNING. + */ + public static void logWarning(Throwable e, String message) { + log(createWarningStatus(e, message)); + } + + /** + * Convenience wrapper for rethrowing exceptions as CoreExceptions, + * with severity of ERROR. + */ + public static Status createStatus(Throwable e, String message) { + return new Status(IStatus.ERROR, PLUGIN_ID, STATUS_EXCEPTION, message, e); + } + + /** + * Convenience wrapper for rethrowing exceptions as CoreExceptions, + * with severity of WARNING. + */ + public static Status createWarningStatus(Throwable e, String message) { + return new Status(IStatus.WARNING, PLUGIN_ID, STATUS_EXCEPTION, message, e); + } + + /** + * Convenience wrapper for rethrowing exceptions as CoreExceptions, + * with severity of INFO. + */ + public static Status createInfoStatus(Throwable e, String message) { + return new Status(IStatus.INFO, PLUGIN_ID, STATUS_EXCEPTION, message, e); + } + public static void trace(final String msg){ + if (DEBUG) { + StringBuffer sb = new StringBuffer(); + sb.append('['); + // SimpleDateFormat is not thread-safe, according to javadoc + synchronized(TRACE_DATE_FORMAT) { + sb.append(TRACE_DATE_FORMAT.format(new Date())); + } + sb.append('-'); + // Some threads have qualified type names; too long. + String threadName = Thread.currentThread().getName(); + int dot = threadName.lastIndexOf('.'); + if (dot < 0) { + sb.append(threadName); + } + else { + sb.append(threadName.substring(dot+1)); + } + sb.append(']'); + sb.append(msg); + System.out.println(sb); + } + } + +} diff --git a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeAnnotationProcessorManager.java b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeAnnotationProcessorManager.java new file mode 100644 index 0000000000..6ed9d68c89 --- /dev/null +++ b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeAnnotationProcessorManager.java @@ -0,0 +1,136 @@ +/******************************************************************************* + * Copyright (c) 2007 BEA Systems, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wharley@bea.com - initial API and implementation + * + *******************************************************************************/ + +package org.eclipse.jdt.internal.apt.pluggable.core.dispatch; + +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import javax.annotation.processing.Processor; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.apt.core.internal.AnnotationProcessorFactoryLoader; +import org.eclipse.jdt.apt.core.internal.IServiceFactory; +import org.eclipse.jdt.apt.core.internal.util.FactoryPath; +import org.eclipse.jdt.apt.core.internal.util.FactoryPath.Attributes; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.internal.apt.pluggable.core.Apt6Plugin; +import org.eclipse.jdt.internal.compiler.Compiler; +import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseAnnotationProcessorManager; +import org.eclipse.jdt.internal.compiler.apt.dispatch.ProcessorInfo; +import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; +import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; +import org.eclipse.jdt.internal.core.CompilationUnitProblemFinder; +import org.eclipse.jdt.internal.core.builder.ICompilationUnitLocator; + +/** + * Java 6 annotation processor manager used when compiling within the IDE. + * @see org.eclipse.jdt.internal.compiler.apt.dispatch.BatchAnnotationProcessorManager + */ +public class IdeAnnotationProcessorManager extends BaseAnnotationProcessorManager { + + private IJavaProject _javaProject; + private ICompilationUnitLocator _cuLocator; + private Map<IServiceFactory, FactoryPath.Attributes> _processors; + private Iterator<Entry<IServiceFactory, Attributes>> _processorIter; + + /** + * Initialize the processor manager for a particular project. It is an error + * to initialize a manager more than once. + * + * @param abstractImageBuilder must be an instanceof AbstractImageBuilder. + * (But it can't be prototyped that way because the abstract base class must + * compile without Eclipse platform code.) + * + * @param javaProject must be an instanceof IJavaProject. (But it can't be + * prototyped that way because the abstract base class must compile without + * Eclipse platform code.) + */ + @Override + public void configureFromPlatform(Compiler compiler, Object compilationUnitLocator, Object javaProject) { + _javaProject = (IJavaProject) javaProject; + _cuLocator = (ICompilationUnitLocator) compilationUnitLocator; + if (null != _processingEnv) { + throw new IllegalStateException( + "Calling configure() more than once on an AnnotationProcessorManager is not supported"); //$NON-NLS-1$ + } + // If it's a CompilationUnitProblemFinder, we're in reconcile phase. Else it's build. + if (compiler instanceof CompilationUnitProblemFinder) { + _processingEnv = new IdeReconcileProcessingEnvImpl(this, _javaProject, compiler); + } else { + _processingEnv = new IdeBuildProcessingEnvImpl(this, _javaProject, compiler); + } + if (Apt6Plugin.DEBUG) { + Apt6Plugin.trace("Java 6 annotation processor manager initialized for compiler " + + compiler.toString() + " on project " + _javaProject.getElementName()); + } + } + + /** + * If this project has a ProcessorPath defined, use it. Else, construct + * one from the classpath. + */ + @Override + public ProcessorInfo discoverNextProcessor() { + // _processorIter gets initialized the first time through processAnnotations() + if (_processorIter.hasNext()) { + Entry<IServiceFactory, Attributes> entry = _processorIter.next(); + Processor p; + try { + p = (Processor)entry.getKey().newInstance(); + p.init(_processingEnv); + ProcessorInfo pi = new ProcessorInfo(p); + if (Apt6Plugin.DEBUG) { + Apt6Plugin.trace("Discovered processor " + p.toString()); + } + return pi; + } catch (CoreException e) { + Apt6Plugin.log(e, "Unable to create instance of annotation processor " + entry.getKey()); //$NON-NLS-1$ + } + } + return null; + } + + @Override + public void reportProcessorException(Processor p, Exception e) { + Apt6Plugin.log(e, "Exception thrown by Java annotation processor " + p); //$NON-NLS-1$ + } + + /** + * @return an ICompilationUnit corresponding to the specified file. In IDE mode this + * will be backed by an org.eclipse.jdt.internal.core.builder.SourceFile. + */ + public ICompilationUnit findCompilationUnit(IFile file) { + return _cuLocator.fromIFile(file); + } + + /** + * In IDE mode, we are able to determine whether there are no processors. If that's the case, + * then we can avoid doing the work of walking the ASTs to search for annotations. We still + * need to clean up no-longer-generated files when the factory path is changed, but the best + * way to do that is to force a clean build. + * @see BaseAnnotationProcessorManager#processAnnotations(CompilationUnitDeclaration[], boolean) + */ + @Override + public void processAnnotations(CompilationUnitDeclaration[] units, boolean isLastRound) { + if (null == _processors ) { + _processors = AnnotationProcessorFactoryLoader.getLoader().getJava6FactoriesAndAttributesForProject(_javaProject); + _processorIter = _processors.entrySet().iterator(); + } + if (!_processors.isEmpty()) { + super.processAnnotations(units, isLastRound); + } + } + +} diff --git a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeBuildProcessingEnvImpl.java b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeBuildProcessingEnvImpl.java new file mode 100644 index 0000000000..94e1437e92 --- /dev/null +++ b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeBuildProcessingEnvImpl.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2007 BEA Systems, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wharley@bea.com - initial API and implementation + * + *******************************************************************************/ + +package org.eclipse.jdt.internal.apt.pluggable.core.dispatch; + +import org.eclipse.jdt.apt.core.env.Phase; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.internal.compiler.Compiler; + +/** + * ProcessingEnvironment for build phase in IDE. + * @since 3.3 + */ +public class IdeBuildProcessingEnvImpl extends IdeProcessingEnvImpl { + + public IdeBuildProcessingEnvImpl(IdeAnnotationProcessorManager dispatchManager, + IJavaProject jproject, Compiler compiler) { + super(dispatchManager, jproject, compiler); + } + + @Override + public Phase getPhase() { + return Phase.BUILD; + } + +} diff --git a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeProcessingEnvImpl.java b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeProcessingEnvImpl.java new file mode 100644 index 0000000000..53e69ef35b --- /dev/null +++ b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeProcessingEnvImpl.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * Copyright (c) 2007 BEA Systems, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wharley@bea.com - initial API and implementation + * + *******************************************************************************/ + +package org.eclipse.jdt.internal.apt.pluggable.core.dispatch; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import javax.lang.model.element.Element; + +import org.eclipse.core.resources.IFile; +import org.eclipse.jdt.apt.core.env.Phase; +import org.eclipse.jdt.apt.core.internal.AptPlugin; +import org.eclipse.jdt.apt.core.internal.AptProject; +import org.eclipse.jdt.apt.core.internal.generatedfile.FileGenerationResult; +import org.eclipse.jdt.apt.core.util.AptConfig; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.internal.apt.pluggable.core.filer.IdeFilerImpl; +import org.eclipse.jdt.internal.compiler.Compiler; +import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl; +import org.eclipse.jdt.internal.compiler.apt.model.IElementInfo; + +/** + * Implementation of ProcessingEnvironment when running inside IDE. + * The lifetime of this object corresponds to the lifetime of the + * {@link IdeAnnotationProcessorManager} that owns it. + * @see org.eclipse.jdt.internal.compiler.apt.dispatch.BatchProcessingEnvImpl + */ +public abstract class IdeProcessingEnvImpl extends BaseProcessingEnvImpl { + + private final IdeAnnotationProcessorManager _dispatchManager; + private final IJavaProject _javaProject; + protected final AptProject _aptProject; + + public IdeProcessingEnvImpl(IdeAnnotationProcessorManager dispatchManager, + IJavaProject jproject, Compiler compiler) + { + _dispatchManager = dispatchManager; + _javaProject = jproject; + _compiler = compiler; + _aptProject = AptPlugin.getAptProject(jproject); + _filer = new IdeFilerImpl(_dispatchManager, this); + //TODO: _messager + } + + /* (non-Javadoc) + * @see javax.annotation.processing.ProcessingEnvironment#getLocale() + */ + @Override + public Locale getLocale() { + return Locale.getDefault(); + } + + @Override + public Map<String, String> getOptions() { + if (null == _processorOptions) { + // Java 5 processor options include items on the command line such as -s, + // -classpath, etc., but Java 6 options only include the options specified + // with -A, which will have been parsed into key/value pairs with no dash. + Map<String, String> allOptions = AptConfig.getProcessorOptions(_javaProject); + Map<String, String> procOptions = new HashMap<String, String>(); + for (Map.Entry<String, String> entry : allOptions.entrySet()) { + if (!entry.getKey().startsWith("-")) { //$NON-NLS-1$ + procOptions.put(entry.getKey(), entry.getValue()); + } + } + procOptions.put("phase", getPhase().toString()); //$NON-NLS-1$ + _processorOptions = Collections.unmodifiableMap(procOptions); + } + return _processorOptions; + } + + public AptProject getAptProject() { + return _aptProject; + } + + /** + * @return whether this environment supports building or reconciling. + */ + public abstract Phase getPhase(); + + /** + * Get the IFile that contains or represents the specified source element. + * If the element is a package, get the IFile corresponding to its + * package-info.java file. If the element is a top-level type, get the + * IFile corresponding to its type. If the element is a nested element + * of some sort (nested type, method, etc.) then get the IFile corresponding + * to the containing top-level type. + * If the element is not a source type at all, then return null. + * @param elem + * @return + */ + public IFile getEnclosingIFile(Element elem) { + // if this cast fails it could be that a non-Eclipse element got passed in somehow. + IElementInfo impl = (IElementInfo)elem; + String name = impl.getFileName(); + if (name == null) { + return null; + } + return _javaProject.getProject().getFile(name); + } + + public void addNewUnit(FileGenerationResult result) { + addNewUnit(_dispatchManager.findCompilationUnit(result.getFile())); + } + +} diff --git a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeReconcileProcessingEnvImpl.java b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeReconcileProcessingEnvImpl.java new file mode 100644 index 0000000000..128c8b0a3a --- /dev/null +++ b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/dispatch/IdeReconcileProcessingEnvImpl.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2007 BEA Systems, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wharley@bea.com - initial API and implementation + * + *******************************************************************************/ + +package org.eclipse.jdt.internal.apt.pluggable.core.dispatch; + +import org.eclipse.jdt.apt.core.env.Phase; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.internal.compiler.Compiler; + +/** + * ProcessingEnvironment for reconciles in IDE. + * @since 3.3 + */ +public class IdeReconcileProcessingEnvImpl extends IdeProcessingEnvImpl { + + public IdeReconcileProcessingEnvImpl(IdeAnnotationProcessorManager dispatchManager, + IJavaProject jproject, Compiler compiler) { + super(dispatchManager, jproject, compiler); + } + + @Override + public Phase getPhase() { + return Phase.RECONCILE; + } + +} diff --git a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeFilerImpl.java b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeFilerImpl.java new file mode 100644 index 0000000000..13c38a3c62 --- /dev/null +++ b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeFilerImpl.java @@ -0,0 +1,94 @@ +/******************************************************************************* + * Copyright (c) 2007 BEA Systems, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wharley@bea.com - initial API and implementation + * + *******************************************************************************/ + +package org.eclipse.jdt.internal.apt.pluggable.core.filer; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.processing.Filer; +import javax.lang.model.element.Element; +import javax.tools.FileObject; +import javax.tools.JavaFileObject; +import javax.tools.JavaFileManager.Location; + +import org.eclipse.core.resources.IFile; +import org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeAnnotationProcessorManager; +import org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeProcessingEnvImpl; + +/** + * Implementation of the Filer interface that is used in IDE mode. + * @see org.eclipse.jdt.internal.compiler.apt.dispatch.BatchFilerImpl + * @since 3.3 + */ +public class IdeFilerImpl implements Filer { + + //private final IdeAnnotationProcessorManager _dispatchManager; + private final IdeProcessingEnvImpl _env; + + public IdeFilerImpl(IdeAnnotationProcessorManager dispatchManager, + IdeProcessingEnvImpl env) { + //_dispatchManager = dispatchManager; + _env = env; + } + + /* (non-Javadoc) + * @see javax.annotation.processing.Filer#createClassFile(java.lang.CharSequence, javax.lang.model.element.Element[]) + */ + @Override + public JavaFileObject createClassFile(CharSequence name, Element... originatingElements) + throws IOException { + //TODO + throw new UnsupportedOperationException("Creating class files is not yet implemented"); //$NON-NLS-1$ + } + + /* (non-Javadoc) + * @see javax.annotation.processing.Filer#createResource(javax.tools.JavaFileManager.Location, java.lang.CharSequence, java.lang.CharSequence, javax.lang.model.element.Element[]) + */ + @Override + public FileObject createResource(Location location, CharSequence pkg, + CharSequence relativeName, Element... originatingElements) throws IOException { + // TODO Auto-generated method stub + return null; + } + + /** + * @param originatingElements should all be source types; binary types (ie elements in jar files) + * will be ignored. + * @see javax.annotation.processing.Filer#createSourceFile(java.lang.CharSequence, javax.lang.model.element.Element[]) + */ + @Override + public JavaFileObject createSourceFile(CharSequence name, Element... originatingElements) + throws IOException + { + //TODO: check whether file has already been generated in this run + List<IFile> parentFiles = new ArrayList<IFile>(originatingElements.length); + for (Element elem : originatingElements) { + parentFiles.add(_env.getEnclosingIFile(elem)); + } + // Convert originatingElements to List<IFile>. The originatingElements should all + // be source types, else they would not be getting + return new IdeJavaFileObject(_env, name, parentFiles); + } + + /* (non-Javadoc) + * @see javax.annotation.processing.Filer#getResource(javax.tools.JavaFileManager.Location, java.lang.CharSequence, java.lang.CharSequence) + */ + @Override + public FileObject getResource(Location location, CharSequence pkg, CharSequence relativeName) + throws IOException { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeJavaFileObject.java b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeJavaFileObject.java new file mode 100644 index 0000000000..d3ea5dba31 --- /dev/null +++ b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeJavaFileObject.java @@ -0,0 +1,161 @@ +/******************************************************************************* + * Copyright (c) 2007 BEA Systems, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wharley@bea.com - initial API and implementation + * + *******************************************************************************/ + +package org.eclipse.jdt.internal.apt.pluggable.core.filer; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.net.URI; +import java.util.List; + +import javax.lang.model.element.Modifier; +import javax.lang.model.element.NestingKind; +import javax.tools.JavaFileObject; + +import org.eclipse.core.resources.IFile; +import org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeProcessingEnvImpl; + +/** + * Implementation of JavaFileObject used for Java 6 annotation processing within the IDE. + * This object is used only for writing source and class files. + * + * @since 3.3 + */ +public class IdeJavaFileObject implements JavaFileObject { + + private final IdeProcessingEnvImpl _env; + private final CharSequence _name; + private final List<IFile> _parentFiles; + + public IdeJavaFileObject(IdeProcessingEnvImpl env, CharSequence name, List<IFile> parentFiles) { + _env = env; + _parentFiles = parentFiles; + _name = name; + } + + /* (non-Javadoc) + * @see javax.tools.JavaFileObject#getAccessLevel() + */ + @Override + public Modifier getAccessLevel() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see javax.tools.JavaFileObject#getKind() + */ + @Override + public Kind getKind() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see javax.tools.JavaFileObject#getNestingKind() + */ + @Override + public NestingKind getNestingKind() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see javax.tools.JavaFileObject#isNameCompatible(java.lang.String, javax.tools.JavaFileObject.Kind) + */ + @Override + public boolean isNameCompatible(String simpleName, Kind kind) { + // TODO Auto-generated method stub + return false; + } + + /* (non-Javadoc) + * @see javax.tools.FileObject#delete() + */ + @Override + public boolean delete() { + throw new UnsupportedOperationException(); + } + + /* (non-Javadoc) + * @see javax.tools.FileObject#getCharContent(boolean) + */ + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see javax.tools.FileObject#getLastModified() + */ + @Override + public long getLastModified() { + // TODO Auto-generated method stub + return 0; + } + + /* (non-Javadoc) + * @see javax.tools.FileObject#getName() + */ + @Override + public String getName() { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see javax.tools.FileObject#openInputStream() + */ + @Override + public InputStream openInputStream() throws IOException { + throw new UnsupportedOperationException(); + } + + /* (non-Javadoc) + * @see javax.tools.FileObject#openOutputStream() + */ + @Override + public OutputStream openOutputStream() throws IOException { + // TODO Auto-generated method stub + return null; + } + + /* (non-Javadoc) + * @see javax.tools.FileObject#openReader(boolean) + */ + @Override + public Reader openReader(boolean ignoreEncodingErrors) throws IOException { + throw new UnsupportedOperationException(); + } + + /* (non-Javadoc) + * @see javax.tools.FileObject#openWriter() + */ + @Override + public Writer openWriter() throws IOException { + return new IdeJavaSourceFileWriter(_env, _name, _parentFiles); + } + + /* (non-Javadoc) + * @see javax.tools.FileObject#toUri() + */ + @Override + public URI toUri() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeJavaSourceFileWriter.java b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeJavaSourceFileWriter.java new file mode 100644 index 0000000000..b9034d7a7e --- /dev/null +++ b/org.eclipse.jdt.apt.pluggable.core/src/org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeJavaSourceFileWriter.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2007 BEA Systems, Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * wharley@bea.com - initial API and implementation + * + *******************************************************************************/ + +package org.eclipse.jdt.internal.apt.pluggable.core.filer; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.List; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.apt.core.env.Phase; +import org.eclipse.jdt.apt.core.internal.generatedfile.FileGenerationResult; +import org.eclipse.jdt.apt.core.internal.generatedfile.GeneratedFileManager; +import org.eclipse.jdt.internal.apt.pluggable.core.Apt6Plugin; +import org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeProcessingEnvImpl; + +/** + * + * @since 3.3 + */ +public class IdeJavaSourceFileWriter extends StringWriter { + + private final IdeProcessingEnvImpl _env; + private final CharSequence _name; + private final List<IFile> _parentFiles; + private boolean _closed = false; + + public IdeJavaSourceFileWriter(IdeProcessingEnvImpl env, CharSequence name, List<IFile> parentFiles) { + _env = env; + _parentFiles = parentFiles; + _name = name; + _env.getAptProject().getGeneratedSourceFolderManager().getFolder(); + } + + /** + * + */ + @Override + public void close() throws IOException { + synchronized(this) { + if (_closed) { + return; + } + _closed = true; + } + try { + GeneratedFileManager gfm = _env.getAptProject().getGeneratedFileManager(); + Phase phase = _env.getPhase(); + + FileGenerationResult result = null; + if ( phase == Phase.RECONCILE ) + { + //TODO - implement reconcile + } + else if ( phase == Phase.BUILD) { + // TODO: actually we need to be more sophisticated about dependencies, because they can be specified. + // Remember empty-parent situation. + result = gfm.generateFileDuringBuild( + _parentFiles, _name.toString(), this.toString(), null /* progress monitor */ ); + } + if (result != null) { + _env.addNewUnit(result); + } + } + catch (CoreException ce) { + Apt6Plugin.log(ce, "Unable to generate type when JavaSourceFilePrintWriter was closed"); //$NON-NLS-1$ + } + } +} |