# HG changeset patch # User Milos Kleint # Date 1233844128 -3600 # Node ID 261b1a0ae4a8ce5638f619d77a449d22eaba286e # Parent 89c6a10111962ddad778e8f2742c8b416b21aede #157985 add ProjectClassPathModifier.addProjects() and same method in PCPMImplementation to allow adding project dependencies to ant and maven projects. Get rid of AntArtifact from signature. add default (ant) and maven implementation, use in ejb core module when adding an ejb reference. diff --git a/j2ee.ejbcore/nbproject/project.xml b/j2ee.ejbcore/nbproject/project.xml --- a/j2ee.ejbcore/nbproject/project.xml +++ b/j2ee.ejbcore/nbproject/project.xml @@ -263,6 +263,7 @@ 1 + 1.24 diff --git a/j2ee.ejbcore/src/org/netbeans/modules/j2ee/ejbcore/Utils.java b/j2ee.ejbcore/src/org/netbeans/modules/j2ee/ejbcore/Utils.java --- a/j2ee.ejbcore/src/org/netbeans/modules/j2ee/ejbcore/Utils.java +++ b/j2ee.ejbcore/src/org/netbeans/modules/j2ee/ejbcore/Utils.java @@ -380,6 +380,19 @@ public static AntArtifact getAntArtifact(final EjbReference ejbReference) throws IOException { + Project project = getProject(ejbReference); + if (project == null) { + return null; + } + AntArtifact[] antArtifacts = AntArtifactQuery.findArtifactsByType(project, JavaProjectConstants.ARTIFACT_TYPE_JAR); + boolean hasArtifact = (antArtifacts != null && antArtifacts.length > 0); + + return hasArtifact ? antArtifacts[0] : null; + + } + + public static Project getProject(final EjbReference ejbReference) throws IOException { + MetadataModel ejbReferenceMetadataModel = ejbReference.getEjbModule().getMetadataModel(); FileObject ejbReferenceEjbClassFO = ejbReferenceMetadataModel.runReadAction(new MetadataModelAction() { public FileObject run(EjbJarMetadata metadata) throws Exception { @@ -390,12 +403,7 @@ if (ejbReferenceEjbClassFO == null) { return null; } - Project project = FileOwnerQuery.getOwner(ejbReferenceEjbClassFO); - AntArtifact[] antArtifacts = AntArtifactQuery.findArtifactsByType(project, JavaProjectConstants.ARTIFACT_TYPE_JAR); - boolean hasArtifact = (antArtifacts != null && antArtifacts.length > 0); - - return hasArtifact ? antArtifacts[0] : null; - + return FileOwnerQuery.getOwner(ejbReferenceEjbClassFO); } /** diff --git a/j2ee.ejbcore/src/org/netbeans/modules/j2ee/ejbcore/action/CallEjbGenerator.java b/j2ee.ejbcore/src/org/netbeans/modules/j2ee/ejbcore/action/CallEjbGenerator.java --- a/j2ee.ejbcore/src/org/netbeans/modules/j2ee/ejbcore/action/CallEjbGenerator.java +++ b/j2ee.ejbcore/src/org/netbeans/modules/j2ee/ejbcore/action/CallEjbGenerator.java @@ -51,6 +51,8 @@ import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeKind; import javax.naming.NamingException; +import org.netbeans.api.java.classpath.ClassPath; +import org.netbeans.api.java.project.classpath.ProjectClassPathModifier; import org.netbeans.api.java.source.ElementHandle; import org.netbeans.api.java.source.GeneratorUtilities; import org.netbeans.api.java.source.JavaSource; @@ -59,7 +61,6 @@ import org.netbeans.api.java.source.WorkingCopy; import org.netbeans.api.project.FileOwnerQuery; import org.netbeans.api.project.Project; -import org.netbeans.api.project.ant.AntArtifact; import org.netbeans.modules.j2ee.api.ejbjar.EjbReference; import org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer; import org.netbeans.modules.j2ee.common.method.MethodModel; @@ -81,7 +82,6 @@ import org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres.ServiceLocatorStrategy; import org.netbeans.modules.j2ee.metadata.model.api.MetadataModel; import org.netbeans.modules.j2ee.metadata.model.api.MetadataModelAction; -import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; import org.openide.filesystems.FileObject; import org.openide.util.Exceptions; @@ -150,7 +150,7 @@ if (remote) { if (enterpriseProjectIsJavaEE5 && InjectionTargetQuery.isInjectionTarget(referencingFO, referencingClassName)) { - addProjectToClassPath(enterpriseProject, ejbReference); + addProjectToClassPath(enterpriseProject, ejbReference, referencingFO); } else if (nodeProjectIsJavaEE5 == enterpriseProjectIsJavaEE5){ // see #75876 erc.addEjbReference(ejbReference, ejbReferenceName, referencingFO, referencingClassName); } @@ -161,7 +161,7 @@ } } else { if (enterpriseProjectIsJavaEE5 && InjectionTargetQuery.isInjectionTarget(referencingFO, referencingClassName)) { - addProjectToClassPath(enterpriseProject, ejbReference); + addProjectToClassPath(enterpriseProject, ejbReference, referencingFO); } else if (nodeProjectIsJavaEE5 == enterpriseProjectIsJavaEE5){ // see #75876 erc.addEjbLocalReference(ejbReference, ejbReferenceName, referencingFO, referencingClassName); } @@ -537,15 +537,15 @@ return caps.toString(); } - private static void addProjectToClassPath(final Project enterpriseProject, final EjbReference ref) throws IOException { + private static void addProjectToClassPath(final Project enterpriseProject, final EjbReference ref, FileObject refFO) throws IOException { - AntArtifact target = Utils.getAntArtifact(ref); + Project target = Utils.getProject(ref); - boolean differentProject = target != null && !enterpriseProject.equals(target.getProject()); + boolean differentProject = target != null && !enterpriseProject.equals(target); if (differentProject) { - ProjectClassPathExtender pcpe = enterpriseProject.getLookup().lookup(ProjectClassPathExtender.class); - assert pcpe != null; - pcpe.addAntArtifact(target, target.getArtifactLocations()[0]); +// Sources sg = ProjectUtils.getSources(target); +// SourceGroup[] grp = sg.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); + ProjectClassPathModifier.addProjects(new Project[] {target} , refFO, ClassPath.COMPILE); } } diff --git a/java.project/apichanges.xml b/java.project/apichanges.xml --- a/java.project/apichanges.xml +++ b/java.project/apichanges.xml @@ -106,6 +106,25 @@ + + + Addition of ProjectClassPathModifier.addProject() method + + + + + +

+ ProjectClassPathModifier.addProject(Project[], FileObject, String) is a partial replacement for + ProjectClassPathModifier.addAntArtifact() that is capable of working with non-ant based project types as well. + It's not guaranteed that the source and target project will connect in cases when each is of different class of project. Eg. + Ant-based vs Maven project types. A way to check is to attempt to retrieve AntArtifact from the source and target projects.. +

+
+ + + +
diff --git a/java.project/manifest.mf b/java.project/manifest.mf --- a/java.project/manifest.mf +++ b/java.project/manifest.mf @@ -3,7 +3,7 @@ OpenIDE-Module-Layer: org/netbeans/modules/java/project/layer.xml OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/java/project/Bundle.properties OpenIDE-Module-Needs: javax.script.ScriptEngine.freemarker -OpenIDE-Module-Specification-Version: 1.23 +OpenIDE-Module-Specification-Version: 1.24 OpenIDE-Module-Recommends: org.netbeans.spi.java.project.runner.JavaRunnerImplementation AutoUpdate-Show-In-Client: false diff --git a/java.project/src/org/netbeans/api/java/project/classpath/ProjectClassPathModifier.java b/java.project/src/org/netbeans/api/java/project/classpath/ProjectClassPathModifier.java --- a/java.project/src/org/netbeans/api/java/project/classpath/ProjectClassPathModifier.java +++ b/java.project/src/org/netbeans/api/java/project/classpath/ProjectClassPathModifier.java @@ -279,6 +279,36 @@ return result; } } + + /** + * Adds projects into project's classpath if the + * artifacts are not already on it. + *

+ * It's not guaranteed that the source and target project will connect in cases when each is of different class of project. Eg. + * Ant-based vs Maven project types. A way to check is to attempt to retrieve AntArtifact from the source and target projects.. + * + * @param projects to be added + * @param projectArtifact a file whose classpath should be extended + * @param classPathType the type of classpath to be extended, @see ClassPath + * @return true in case the classpath was changed, (at least one artifact was added to the classpath), + * the value false is returned when all the artifacts are already included on the classpath. + * @exception IOException in case the project metadata cannot be changed + * @exception UnsupportedOperationException is thrown when the project does not support + * adding of an artifact to the classpath of the given type. + * @since org.netbeans.modules.java.project/1 1.24 + */ + @SuppressWarnings("deprecation") //NOI18N + public static boolean addProjects (final Project[] projects, + final FileObject projectArtifact, final String classPathType) throws IOException, UnsupportedOperationException { + final Extensible extensible = findExtensible (projectArtifact, classPathType); + if (extensible.pcmi != null) { + assert extensible.sg != null; + assert extensible.classPathType != null; + return ProjectClassPathModifierAccessor.INSTANCE.addProjects (projects, extensible.pcmi, extensible.sg, extensible.classPathType); + } else { + throw new UnsupportedOperationException("Cannot add project as dependency. Missing ProjectClassPathModifierImplementation service in project type."); + } + } /** * Removes artifacts (e.g. subprojects) from project's classpath if the diff --git a/java.project/src/org/netbeans/modules/java/project/classpath/ProjectClassPathModifierAccessor.java b/java.project/src/org/netbeans/modules/java/project/classpath/ProjectClassPathModifierAccessor.java --- a/java.project/src/org/netbeans/modules/java/project/classpath/ProjectClassPathModifierAccessor.java +++ b/java.project/src/org/netbeans/modules/java/project/classpath/ProjectClassPathModifierAccessor.java @@ -44,6 +44,7 @@ import java.io.IOException; import java.net.URI; import java.net.URL; +import org.netbeans.api.project.Project; import org.netbeans.api.project.SourceGroup; import org.netbeans.api.project.ant.AntArtifact; import org.netbeans.api.project.libraries.Library; @@ -70,7 +71,7 @@ /** Creates a new instance of ProjectClassPathModifierAccessor */ public ProjectClassPathModifierAccessor() { } - + public abstract SourceGroup[] getExtensibleSourceGroups (ProjectClassPathModifierImplementation m); public abstract String[] getExtensibleClassPathTypes (ProjectClassPathModifierImplementation m, SourceGroup sg); @@ -90,5 +91,7 @@ public abstract boolean addAntArtifacts (AntArtifact[] artifacts, URI[] artifactElements, ProjectClassPathModifierImplementation m, SourceGroup sourceGroup, String type) throws IOException, UnsupportedOperationException; public abstract boolean removeAntArtifacts (AntArtifact[] artifacts, URI[] artifactElements, ProjectClassPathModifierImplementation m, SourceGroup sourceGroup, String type) throws IOException, UnsupportedOperationException; - + + public abstract boolean addProjects(Project[] projects, ProjectClassPathModifierImplementation pcmi, SourceGroup sg, String classPathType) throws IOException, UnsupportedOperationException; + } diff --git a/java.project/src/org/netbeans/spi/java/project/classpath/ProjectClassPathModifierImplementation.java b/java.project/src/org/netbeans/spi/java/project/classpath/ProjectClassPathModifierImplementation.java --- a/java.project/src/org/netbeans/spi/java/project/classpath/ProjectClassPathModifierImplementation.java +++ b/java.project/src/org/netbeans/spi/java/project/classpath/ProjectClassPathModifierImplementation.java @@ -49,8 +49,11 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; +import org.netbeans.api.java.project.JavaProjectConstants; +import org.netbeans.api.project.Project; import org.netbeans.api.project.SourceGroup; import org.netbeans.api.project.ant.AntArtifact; +import org.netbeans.api.project.ant.AntArtifactQuery; import org.netbeans.api.project.libraries.Library; import org.netbeans.api.queries.CollocationQuery; import org.netbeans.modules.java.project.classpath.ProjectClassPathModifierAccessor; @@ -271,7 +274,38 @@ * removing of an artifact from the classpath of the given type. */ protected abstract boolean removeAntArtifacts (AntArtifact[] artifacts, URI[] artifactElements, SourceGroup sourceGroup, String type) throws IOException, UnsupportedOperationException; - + + + /** + * Adds projects as dependencies into project's classpath if the + * artifacts are not already on it. The default behaviour will behave as {@link #addAntArtifacts(org.netbeans.api.project.ant.AntArtifact[], java.net.URI[], org.netbeans.api.project.SourceGroup, java.lang.String)} + * Other project types can override the behaviour. + * @param projects to be added + * (must be owned by the artifact and be relative to it) + * @param sourceGroup of type {@link org.netbeans.api.java.project.JavaProjectConstants#SOURCES_TYPE_JAVA} + * identifying the compilation unit to change + * @param type the type of the classpath the artifact should be added to, + * eg {@link org.netbeans.api.java.classpath.ClassPath.COMPILE} + * @return true in case the classpath was changed, (at least one artifact was added to the classpath), + * the value false is returned when all the artifacts are already included on the classpath. + * @exception IOException in case the project metadata cannot be changed + * @exception UnsupportedOperationException is thrown when the project does not support + * adding of an artifact to the classpath of the given type. + * @since org.netbeans.modules.java.project/1 1.24 + */ + protected boolean addProjects(Project[] projects, SourceGroup sg, String classPathType) throws IOException, UnsupportedOperationException { + List ants = new ArrayList(); + List antUris = new ArrayList(); + for (Project prj : projects) { + AntArtifact[] antArtifacts = AntArtifactQuery.findArtifactsByType(prj, JavaProjectConstants.ARTIFACT_TYPE_JAR); + for (AntArtifact aa : antArtifacts) { + ants.add(aa); + antUris.add(aa.getArtifactLocations()[0]); + } + } + return addAntArtifacts(ants.toArray(new AntArtifact[0]), antUris.toArray(new URI[0]), sg, classPathType); + } + /** * Takes a classpath root and tries to figure the best way to reference that file for that particular project. * The possible actions include relativization of path, copying to sharable libraries folder etc. @@ -322,6 +356,7 @@ } return f; } + private File copyFile(File file, FileObject newRoot) throws IOException { FileObject fo = FileUtil.toFileObject(file); @@ -410,6 +445,11 @@ public boolean addRoots (URI[] classPathRoots, ProjectClassPathModifierImplementation m, SourceGroup sourceGroup, String type) throws IOException, UnsupportedOperationException { assert m!= null; return m.addRoots (classPathRoots, sourceGroup, type); - } + } + + public boolean addProjects(Project[] projects, ProjectClassPathModifierImplementation pcmi, SourceGroup sg, String classPathType) throws IOException, UnsupportedOperationException { + assert pcmi != null; + return pcmi.addProjects(projects, sg, classPathType); + } } } diff --git a/maven/nbproject/project.xml b/maven/nbproject/project.xml --- a/maven/nbproject/project.xml +++ b/maven/nbproject/project.xml @@ -123,7 +123,7 @@ 1 - 1.18 + 1.24 diff --git a/maven/src/org/netbeans/modules/maven/CPExtender.java b/maven/src/org/netbeans/modules/maven/CPExtender.java --- a/maven/src/org/netbeans/modules/maven/CPExtender.java +++ b/maven/src/org/netbeans/modules/maven/CPExtender.java @@ -52,6 +52,9 @@ import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.project.MavenProject; +import org.netbeans.api.project.Project; import org.netbeans.modules.maven.indexer.api.NBVersionInfo; import org.netbeans.modules.maven.indexer.api.RepositoryInfo; import org.netbeans.modules.maven.indexer.api.RepositoryPreferences; @@ -346,7 +349,7 @@ public boolean addLibraries(final Library[] libraries, SourceGroup grp, String type) throws IOException { final Boolean[] added = new Boolean[1]; added[0] = libraries.length > 0; - String scope = ClassPath.EXECUTE.equals(type) ? "runtime" : null; //NOI18N + String scope = ClassPath.EXECUTE.equals(type) ? Artifact.SCOPE_RUNTIME : null; //NOI18N //figure if we deal with test or regular sources. String name = grp.getName(); if (MavenSourcesImpl.NAME_TESTSOURCE.equals(name)) { @@ -384,7 +387,7 @@ public boolean addRoots(final URL[] urls, SourceGroup grp, String type) throws IOException { final Boolean[] added = new Boolean[1]; added[0] = urls.length > 0; - String scope = ClassPath.EXECUTE.equals(type) ? "runtime" : null;//NOI18N + String scope = ClassPath.EXECUTE.equals(type) ? Artifact.SCOPE_RUNTIME : null;//NOI18N //figure if we deal with test or regular sources. String name = grp.getName(); if (MavenSourcesImpl.NAME_TESTSOURCE.equals(name)) { @@ -419,7 +422,42 @@ } return added[0]; } - + + @Override + protected boolean addProjects(final Project[] projects, SourceGroup sg, String classPathType) throws IOException, UnsupportedOperationException { + final Boolean[] added = new Boolean[1]; + + added[0] = projects.length > 0; + String scope = ClassPath.EXECUTE.equals(classPathType) ? Artifact.SCOPE_RUNTIME : null;//NOI18N + //figure if we deal with test or regular sources. + String name = sg.getName(); + if (MavenSourcesImpl.NAME_TESTSOURCE.equals(name)) { + scope = "test"; //NOI18N + } + final String fScope = scope; + ModelOperation operation = new ModelOperation() { + public void performOperation(POMModel model) { + for (Project prj: projects) { + NbMavenProject nbprj = prj.getLookup().lookup(NbMavenProject.class); + if (nbprj != null) { + MavenProject mp = nbprj.getMavenProject(); + Dependency dependency = ModelUtils.checkModelDependency(model, mp.getGroupId(), mp.getArtifactId(), true); + dependency.setVersion(mp.getVersion()); + if (fScope != null) { + dependency.setScope(fScope); + } + } else { + Logger.getLogger(CPExtender.class.getName()).warning("Attempting to add a non-Maven project dependency to a Maven project. Skipping."); //NOI18N + } + } + } + }; + FileObject pom = project.getProjectDirectory().getFileObject(POM_XML);//NOI18N + org.netbeans.modules.maven.model.Utilities.performPOMModelOperations(pom, Collections.singletonList(operation)); + return added[0]; + + } + public boolean removeRoots(URL[] arg0, SourceGroup arg1, String arg2) throws IOException, UnsupportedOperationException { throw new UnsupportedOperationException("Removing binary dependencies is not supported by Maven projects."); diff --git a/maven/src/org/netbeans/modules/maven/CPModifierLookupMerger.java b/maven/src/org/netbeans/modules/maven/CPModifierLookupMerger.java --- a/maven/src/org/netbeans/modules/maven/CPModifierLookupMerger.java +++ b/maven/src/org/netbeans/modules/maven/CPModifierLookupMerger.java @@ -48,6 +48,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashSet; +import org.netbeans.api.project.Project; import org.netbeans.api.project.SourceGroup; import org.netbeans.api.project.ant.AntArtifact; import org.netbeans.api.project.libraries.Library; @@ -231,6 +232,21 @@ return fallback.addAntArtifacts(arg0, arg1, arg2, arg3); } + @Override + protected boolean addProjects(Project[] projects, SourceGroup sg, String classPathType) throws IOException, UnsupportedOperationException { +// Collection list = context.lookupAll(ProjectClassPathModifierImplementation.class); +// for (ProjectClassPathModifierImplementation ext : list) { +// Boolean ret = (Boolean)retVal("addProjects", ext, //NOI18N +// new Class[] { new Project[0].getClass(), SourceGroup.class, String.class}, projects, sg, classPathType); +// if (ret.booleanValue()) { +// return ret.booleanValue(); +// } +// } + //use only the fallback, as that's the man with correct impl. others might have the ant based default method implementation preserved.. + return fallback.addProjects(projects, sg, classPathType); + } + + } }