diff --git a/java.source/apichanges.xml b/java.source/apichanges.xml --- a/java.source/apichanges.xml +++ b/java.source/apichanges.xml @@ -105,6 +105,19 @@ + + + Added ImportProcessor interface + + + + + + Added ImportProcessor interface, + + + + Added GeneratorUtilities.copyComments method. diff --git a/java.source/nbproject/project.properties b/java.source/nbproject/project.properties --- a/java.source/nbproject/project.properties +++ b/java.source/nbproject/project.properties @@ -43,7 +43,7 @@ javadoc.title=Java Source javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml -spec.version.base=0.53.0 +spec.version.base=0.54.0 test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/javac-api-nb-7.0-b07.jar test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\ ${o.n.core.dir}/lib/boot.jar:\ diff --git a/java.source/src/org/netbeans/api/java/source/SourceUtils.java b/java.source/src/org/netbeans/api/java/source/SourceUtils.java --- a/java.source/src/org/netbeans/api/java/source/SourceUtils.java +++ b/java.source/src/org/netbeans/api/java/source/SourceUtils.java @@ -41,6 +41,7 @@ package org.netbeans.api.java.source; +import org.netbeans.spi.java.loaders.ImportProcessor; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; @@ -80,6 +81,8 @@ import javax.swing.SwingUtilities; import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.api.editor.mimelookup.MimeLookup; +import org.netbeans.api.editor.mimelookup.MimePath; import org.netbeans.api.java.classpath.ClassPath; import org.netbeans.api.java.lexer.JavaTokenId; import org.netbeans.api.java.classpath.GlobalPathRegistry; @@ -112,6 +115,7 @@ import org.openide.filesystems.FileUtil; import org.openide.filesystems.URLMapper; import org.openide.util.Exceptions; +import org.openide.util.Lookup; import org.openide.util.Parameters; import org.openide.util.Utilities; @@ -271,27 +275,39 @@ return fqn; //not imported/visible so far by any means: - if (info instanceof WorkingCopy) { - CompilationUnitTree nue = (CompilationUnitTree) ((WorkingCopy)info).getChangeSet().get(cut); - cut = nue != null ? nue : cut; - ((WorkingCopy)info).rewrite(info.getCompilationUnit(), addImports(cut, Collections.singletonList(fqn), ((WorkingCopy)info).getTreeMaker())); - } else { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - try { - ModificationResult.runModificationTask(Collections.singletonList(info.getSnapshot().getSource()), new UserTask() { - @Override - public void run(ResultIterator resultIterator) throws Exception { - WorkingCopy copy = WorkingCopy.get(resultIterator.getParserResult()); - copy.toPhase(Phase.ELEMENTS_RESOLVED); - copy.rewrite(copy.getCompilationUnit(), addImports(copy.getCompilationUnit(), Collections.singletonList(fqn), copy.getTreeMaker())); - } - }).commit(); - } catch (Exception e) { - Exceptions.printStackTrace(e); + String topLevelLanguageMIMEType = info.getFileObject().getMIMEType(); + if ("text/x-java".equals(topLevelLanguageMIMEType)){ + if (info instanceof WorkingCopy) { + CompilationUnitTree nue = (CompilationUnitTree) ((WorkingCopy)info).getChangeSet().get(cut); + cut = nue != null ? nue : cut; + ((WorkingCopy)info).rewrite(info.getCompilationUnit(), addImports(cut, Collections.singletonList(fqn), ((WorkingCopy)info).getTreeMaker())); + } else { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + try { + ModificationResult.runModificationTask(Collections.singletonList(info.getSnapshot().getSource()), new UserTask() { + @Override + public void run(ResultIterator resultIterator) throws Exception { + WorkingCopy copy = WorkingCopy.get(resultIterator.getParserResult()); + copy.toPhase(Phase.ELEMENTS_RESOLVED); + copy.rewrite(copy.getCompilationUnit(), addImports(copy.getCompilationUnit(), Collections.singletonList(fqn), copy.getTreeMaker())); + } + }).commit(); + } catch (Exception e) { + Exceptions.printStackTrace(e); + } } - } - }); + }); + } + } else { // embedded java, look up the handler for the top level language + Lookup lookup = MimeLookup.getLookup(MimePath.get(topLevelLanguageMIMEType)); + Lookup.Result result = lookup.lookup(new Lookup.Template(ImportProcessor.class)); + Collection instances = result.allInstances(); + + for (ImportProcessor importsProcesor : instances) { + importsProcesor.addImport(info.getDocument(), fqn); + } + } TypeElement te = info.getElements().getTypeElement(fqn); if (te != null) { diff --git a/java.source/src/org/netbeans/spi/java/loaders/ImportProcessor.java b/java.source/src/org/netbeans/spi/java/loaders/ImportProcessor.java new file mode 100644 --- /dev/null +++ b/java.source/src/org/netbeans/spi/java/loaders/ImportProcessor.java @@ -0,0 +1,60 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2010 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2010 Sun Microsystems, Inc. + */ + +package org.netbeans.spi.java.loaders; + +import javax.swing.text.Document; + +/** + * Interface that allows to hook custom code to the action of inserting import + * when code completion is called within embedded Java code + * + * @since 0.54 + * @author Tomasz.Slota@Sun.COM + */ +public interface ImportProcessor { + /** + * Handle request to add unresolved import + * (top-level-language specific way) + * + * @param doc + * @param fullyQualifiedClassName + */ + void addImport(Document doc, String fullyQualifiedClassName); +}