Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core')
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.checkstyle19
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.classpath7
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.pmd7
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.project46
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.settings/org.eclipse.jdt.core.prefs7
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/META-INF/MANIFEST.MF17
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/about.html28
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/build.properties16
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/resources/Activator.java.template45
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/resources/MANIFEST.MF.template11
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/Activator.java50
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/BuildPropertiesUtils.java146
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/PluginUtils.java605
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exception/PdeCoreUtilsRuntimeException.java33
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/BuildPropertiesUtils.java37
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/PluginUtils.java73
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/TargetPlatformUtils.java576
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/exception/PdeCoreUtilsException.java33
-rw-r--r--plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/exception/ReflexiveDiscouragedAccessException.java33
19 files changed, 1789 insertions, 0 deletions
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.checkstyle b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.checkstyle
new file mode 100644
index 00000000000..11b5e4fd4de
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.checkstyle
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Copyright (c) 2010 Mia-Software.
+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:
+ Nicolas Guyomar (Mia-Software)
+ Alban Ménager (Soft-Maint) - Bug 387470 - [EFacet][Custom] Editors
+-->
+<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
+ <local-check-config name="EmfFacet" location="/org.eclipse.emf.facet.archi.tech.rules/checkstyle/EmfFacet.checkstyle" type="project" description=""/>
+ <fileset name="all" enabled="true" check-config-name="EmfFacet" local="true">
+ <file-match-pattern match-pattern="." include-pattern="true"/>
+ <file-match-pattern match-pattern="Messages.java" include-pattern="false"/>
+ </fileset>
+</fileset-config>
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.classpath b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.classpath
new file mode 100644
index 00000000000..2d1a4302f04
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.pmd b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.pmd
new file mode 100644
index 00000000000..a64878bfdf6
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.pmd
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<pmd>
+ <useProjectRuleSet>true</useProjectRuleSet>
+ <ruleSetFile>../org.eclipse.emf.facet.archi.tech.rules/pmd/ruleset.xml</ruleSetFile>
+ <includeDerivedFiles>false</includeDerivedFiles>
+ <violationsAsErrors>true</violationsAsErrors>
+</pmd>
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.project b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.project
new file mode 100644
index 00000000000..4d1a1708d72
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.project
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.papyrus.emf.facet.util.pde.core</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.api.tools.apiAnalysisBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>net.sourceforge.pmd.eclipse.plugin.pmdBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ <nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
+ <nature>org.eclipse.pde.api.tools.apiAnalysisNature</nature>
+ <nature>net.sourceforge.pmd.eclipse.plugin.pmdNature</nature>
+ </natures>
+</projectDescription>
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.settings/org.eclipse.jdt.core.prefs b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 00000000000..44217f8c068
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.5
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/META-INF/MANIFEST.MF b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/META-INF/MANIFEST.MF
new file mode 100644
index 00000000000..01f8db600e2
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/META-INF/MANIFEST.MF
@@ -0,0 +1,17 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: EMF Facet PDE Core Utils (Incubation)
+Bundle-SymbolicName: org.eclipse.papyrus.emf.facet.util.pde.core
+Bundle-Version: 0.4.0.qualifier
+Bundle-Activator: org.eclipse.emf.facet.util.pde.core.internal.Activator
+Bundle-Vendor: Eclipse Modeling Project
+Require-Bundle: org.eclipse.core.runtime;bundle-version="3.6.0",
+ org.eclipse.pde.core;bundle-version="3.6.0",
+ org.eclipse.core.resources;bundle-version="3.6.0",
+ org.eclipse.papyrus.emf.facet.util.core;bundle-version="0.4.0",
+ org.eclipse.jdt.core;bundle-version="3.6.0",
+ com.ibm.icu;bundle-version="4.2.1"
+Bundle-RequiredExecutionEnvironment: J2SE-1.5
+Bundle-ActivationPolicy: lazy
+Export-Package: org.eclipse.emf.facet.util.pde.core.internal.exported,
+ org.eclipse.emf.facet.util.pde.core.internal.exported.exception
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/about.html b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/about.html
new file mode 100644
index 00000000000..d1714826b27
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/about.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
+<title>About</title>
+</head>
+<body lang="EN-US">
+<h2>About This Content</h2>
+
+<p>January 4, 2011</p>
+<h3>License</h3>
+
+<p>The Eclipse Foundation makes available all content in this plug-in (&quot;Content&quot;). Unless otherwise
+indicated below, the Content is provided to you under the terms and conditions of the
+Eclipse Public License Version 1.0 (&quot;EPL&quot;). A copy of the EPL is available
+at <a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>.
+For purposes of the EPL, &quot;Program&quot; will mean the Content.</p>
+
+<p>If you did not receive this Content directly from the Eclipse Foundation, the Content is
+being redistributed by another party (&quot;Redistributor&quot;) and different terms and conditions may
+apply to your use of any object code in the Content. Check the Redistributor's license that was
+provided with the Content. If no such license exists, contact the Redistributor. Unless otherwise
+indicated below, the terms and conditions of the EPL still apply to any source code in the Content
+and such source code may be obtained at <a href="http://www.eclipse.org/">http://www.eclipse.org</a>.</p>
+
+</body>
+</html> \ No newline at end of file
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/build.properties b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/build.properties
new file mode 100644
index 00000000000..44f07d46c98
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/build.properties
@@ -0,0 +1,16 @@
+# Copyright (c) 2013 Mia-Software
+# 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:
+# Gregoire Dupe (Mia-Software) - Bug 417789 - org.eclipse.emf.facet.util.core should not so much dependencies
+# Fabien Treguer (Soft-Maint) - Bug 367168 - [Unit Test Failure][0.2/3.8] org.eclipse.emf.facet.util.core.tests.internal.PluginUtilsTests.testPluginUtils
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+ .,\
+ about.html,\
+ resources/
+src.includes = about.html
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/resources/Activator.java.template b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/resources/Activator.java.template
new file mode 100644
index 00000000000..329f8643720
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/resources/Activator.java.template
@@ -0,0 +1,45 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Mia-Software.
+ * 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:
+ * Nicolas Bros (Mia-Software)
+ *******************************************************************************/
+package {0};
+
+import org.eclipse.core.runtime.Plugin;
+import org.osgi.framework.BundleContext;
+
+/** The activator class controls the plug-in life cycle */
+public class Activator extends Plugin {
+
+ // The plug-in ID
+ public static final String PLUGIN_ID = "{0}"; //$NON-NLS-1$
+
+ // The shared instance
+ private static Activator plugin;
+
+ @Override
+ public void start(final BundleContext context) throws Exception {
+ super.start(context);
+ Activator.plugin = this;
+ }
+
+ @Override
+ public void stop(final BundleContext context) throws Exception {
+ Activator.plugin = null;
+ super.stop(context);
+ }
+
+ /**
+ * Returns the shared instance
+ *
+ * @return the shared instance
+ */
+ public static Activator getDefault() {
+ return Activator.plugin;
+ }
+}
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/resources/MANIFEST.MF.template b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/resources/MANIFEST.MF.template
new file mode 100644
index 00000000000..5f3cd4fbfdc
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/resources/MANIFEST.MF.template
@@ -0,0 +1,11 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: {projectName}
+Bundle-SymbolicName: {packageName};singleton:=true
+Bundle-Version: 0.0.1.qualifier
+Bundle-Activator: {activator}
+Bundle-Vendor: bundle vendor
+Require-Bundle: org.eclipse.core.runtime
+Bundle-RequiredExecutionEnvironment: {javaVersion}
+Bundle-ActivationPolicy: lazy
+Bundle-ClassPath: .,bin
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/Activator.java b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/Activator.java
new file mode 100644
index 00000000000..fe427a0c4d9
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/Activator.java
@@ -0,0 +1,50 @@
+/**
+ * Copyright (c) 2013 Mia-Software
+ * 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:
+ * Gregoire Dupe (Mia-Software) - Bug 417789 - org.eclipse.emf.facet.util.core should not so much dependencies
+ */
+ package org.eclipse.emf.facet.util.pde.core.internal;
+
+import org.eclipse.core.runtime.Plugin;
+import org.osgi.framework.BundleContext;
+
+@SuppressWarnings("PMD.UseSingleton")
+// @SuppressWarnings("PMD.UseSingleton") We cannot conforms to this rule because
+// this class is an Eclipse plug-in activator which will be instantiated by the
+// Eclipse framework.
+public class Activator extends Plugin {
+
+ private static Activator plugin;
+
+ @Override
+ @SuppressWarnings("PMD.SignatureDeclareThrowsException")
+ // @SuppressWarnings("PMD.SignatureDeclareThrowsException") We have to use
+ // this signature because this is an override of an Eclipse framework's
+ // method.
+ public void start(final BundleContext bundleContext) throws Exception {
+ super.start(bundleContext);
+ Activator.plugin = this;
+ }
+
+ @Override
+ @SuppressWarnings("PMD.SignatureDeclareThrowsException")
+ // @SuppressWarnings("PMD.SignatureDeclareThrowsException") We have to use
+ // this signature because this is an override of an Eclipse framework's
+ // method.
+ public void stop(final BundleContext bundleContext) throws Exception {
+ // NOPMD: PMD say "Assigning an Object to null is a code smell."
+ // No choice to right it in another way : this is an Eclipse pattern.
+ Activator.plugin = null; // NOPMD
+ super.stop(bundleContext);
+ }
+
+ public static Plugin getDefault() {
+ return Activator.plugin;
+ }
+
+}
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/BuildPropertiesUtils.java b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/BuildPropertiesUtils.java
new file mode 100644
index 00000000000..820c73852c1
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/BuildPropertiesUtils.java
@@ -0,0 +1,146 @@
+/*******************************************************************************
+ * Copyright (c) 2010-2013 Mia-Software.
+ * 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:
+ * Nicolas Bros (Mia-Software) - initial API and implementation
+ * Nicolas Bros (Mia-Software) - Bug 339991 - don't use internal APIs in org.eclipse.emf.facet.util.core.internal.BuildPropertiesUtils
+ * Laurent Pichierri (Soft-Maint) - Bug 371204 - Compatibility with Helios
+ * Gregoire Dupe (Mia-Software) - Bug 371204 - Compatibility with Helios
+ * Gregoire Dupe (Mia-Software) - Bug 417789 - org.eclipse.emf.facet.util.core should not so much dependencies
+ *******************************************************************************/
+package org.eclipse.emf.facet.util.pde.core.internal;
+
+import java.io.ByteArrayInputStream;
+import java.lang.reflect.Method;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.emf.facet.util.core.Logger;
+import org.eclipse.emf.facet.util.pde.core.internal.exception.PdeCoreUtilsRuntimeException;
+import org.eclipse.pde.core.IEditableModel;
+import org.eclipse.pde.core.build.IBuild;
+import org.eclipse.pde.core.build.IBuildEntry;
+import org.eclipse.pde.core.build.IBuildModel;
+import org.eclipse.pde.core.plugin.IPluginModelBase;
+import org.eclipse.pde.core.plugin.PluginRegistry;
+
+public final class BuildPropertiesUtils {
+
+ // This class has been copied from org.eclipse.emf.facet.infra.common.core.internal.utils.BuildPropertiesUtils
+
+ private BuildPropertiesUtils() {
+ // utilities class
+ }
+
+ /**
+ * Get the build model of an Eclipse plug-in
+ *
+ * @param model
+ * a plug-in's PDE model
+ * @return a build model or <code>null</code> if the build.properties file
+ * does not exist
+ * @throws CoreException
+ */
+ public static IBuildModel getBuildModel(final IPluginModelBase model) throws CoreException {
+ final IProject project = model.getUnderlyingResource().getProject();
+ final IPluginModelBase pluginModelBase = PluginRegistry.findModel(project);
+ //Here we use a reflexive call to be able to build against an Helios platform.
+ //This method will build with Helios, but will fail at runtime.
+ //We do not have a better solution. Otherwise we would have to use an internal API.
+ //We prefer some loose of functionality with Helios that to use internal API.
+ IBuildModel buildModel = null;
+ try {
+ final Class<? extends IPluginModelBase> class1 = pluginModelBase.getClass();
+ final Method method = class1.getMethod("getBuildModel"); //$NON-NLS-1$
+ if (method != null) {
+ buildModel = (IBuildModel) method.invoke(pluginModelBase);
+ }
+ } catch (Exception e) {
+ Logger.logError(e, Activator.getDefault());
+ }
+ return buildModel;
+ }
+
+ /**
+ * @param project
+ * the project for which to get a build model top level object
+ * @return a build model top level object or <code>null</code> if the
+ * build.properties file does not exist
+ * @throws CoreException
+ */
+ public static IBuild getBuild(final IProject project) throws CoreException {
+ IBuild result = null;
+ final IPluginModelBase pluginModel = PluginRegistry.findModel(project);
+ if (pluginModel != null) {
+ final IBuildModel buildModel = getBuildModel(pluginModel);
+ if (buildModel != null) {
+ result = buildModel.getBuild();
+ }
+ }
+ return result;
+ }
+
+ /**
+ * @param elementFile
+ * the file whose presence in the build.properties is being
+ * tested
+ * @return whether the file is in the build.properties (<code>false</code>
+ * if the build.properties file does not exist)
+ * @throws CoreException
+ */
+ public static boolean isInBuild(final IFile elementFile) throws CoreException {
+ final IProject project = elementFile.getProject();
+ final IPath filePath = elementFile.getFullPath().removeFirstSegments(1);
+ final IBuild build = getBuild(project);
+ boolean result = false;
+ if (build != null) {
+ final IBuildEntry entry = build.getEntry(IBuildEntry.BIN_INCLUDES);
+ if (entry != null) {
+ final String[] tokens = entry.getTokens();
+ for (String token : tokens) {
+ final IPath path = new Path(token);
+ // if "abc/" is included, then "abc/def/" is too
+ if (path.isPrefixOf(filePath)) {
+ result = true;
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ public static void addToBuild(final IFile elementFile) throws CoreException {
+ final IProject project = elementFile.getProject();
+ if (isInBuild(elementFile)) {
+ return;
+ }
+ final IPath filePath = elementFile.getFullPath().removeFirstSegments(1);
+ final IPluginModelBase pluginModel = PluginRegistry.findModel(project);
+ final IFile buildProperties = elementFile.getProject().getFile("build.properties"); //$NON-NLS-1$
+ if (!buildProperties.exists()) {
+ buildProperties.create(new ByteArrayInputStream(new byte[0]), true,
+ new NullProgressMonitor());
+ }
+ final IBuildModel buildModel = getBuildModel(pluginModel);
+ if (buildModel == null) {
+ throw new PdeCoreUtilsRuntimeException("Couldn't get build model"); //$NON-NLS-1$
+ }
+ final IBuild build = buildModel.getBuild();
+ IBuildEntry entry = build.getEntry(IBuildEntry.BIN_INCLUDES);
+ // if entry "bin.includes" does not exist, then create it
+ if (entry == null) {
+ entry = buildModel.getFactory().createEntry(IBuildEntry.BIN_INCLUDES);
+ build.add(entry);
+ }
+ entry.addToken(filePath.toPortableString());
+ ((IEditableModel) buildModel).save();
+ }
+}
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/PluginUtils.java b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/PluginUtils.java
new file mode 100644
index 00000000000..a8b557e2b31
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/PluginUtils.java
@@ -0,0 +1,605 @@
+/*******************************************************************************
+ * Copyright (c) 2010-2013 Mia-Software.
+ * 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:
+ * Nicolas Bros (Mia-Software) - initial API and implementation
+ * Nicolas Guyomar (Mia-Software) - Bug 340339 - Need some Utils class for Folder/File/Project management
+ * Gregoire Dupe (Mia-Software) - Bug 340339 - Need some Utils class for Folder/File/Project management
+ * Nicolas Guyomar (Mia-Software) - Bug 340681 - Facet column implementation
+ * Nicolas Bros (Mia-Software) - Bug 380391 - PluginUtils#importPlugin should use the Bundle API
+ * Gregoire Dupe (Mia-Software) - Bug 408344 - [Releng] Deep folders cause build break
+ *******************************************************************************/
+package org.eclipse.emf.facet.util.pde.core.internal;
+
+import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.IWorkspaceRunnable;
+import org.eclipse.core.resources.IncrementalProjectBuilder;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.MultiStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.emf.facet.util.core.Logger;
+import org.eclipse.emf.facet.util.core.internal.exported.FileUtils;
+import org.eclipse.emf.facet.util.core.internal.exported.FolderUtils;
+import org.eclipse.emf.facet.util.core.internal.exported.IFilter;
+import org.eclipse.jdt.core.IClasspathEntry;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaConventions;
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.pde.core.plugin.IExtensions;
+import org.eclipse.pde.core.plugin.IPluginAttribute;
+import org.eclipse.pde.core.plugin.IPluginElement;
+import org.eclipse.pde.core.plugin.IPluginExtension;
+import org.eclipse.pde.core.plugin.IPluginModelBase;
+import org.eclipse.pde.core.plugin.IPluginObject;
+import org.eclipse.pde.core.plugin.PluginRegistry;
+import org.osgi.framework.Bundle;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+import org.xml.sax.SAXException;
+
+import com.ibm.icu.lang.UCharacter;
+
+public final class PluginUtils {
+
+ // This class has been copied from
+ // org.eclipse.emf.facet.infra.common.core.internal.utils.PluginUtils
+
+ private PluginUtils() {
+ // utilities class
+ }
+
+ /**
+ * Returns whether the given file is registered in the plugin.xml of the
+ * given project, using the given extension point.
+ *
+ * @param extensionPoint
+ * the extension point that is used to register elements of this
+ * kind (elements must be declared with a "file" attribute)
+ */
+ public static boolean isRegistered(final IFile elementFile, final String extensionPoint) {
+ final IProject project = elementFile.getProject();
+ final IPluginModelBase pluginModel = PluginRegistry.findModel(project);
+ boolean result = false;
+ if (pluginModel != null) {
+ final IExtensions extensions2 = pluginModel.getExtensions();
+ final IPluginExtension[] extensions = extensions2.getExtensions();
+ for (IPluginExtension pluginExtension : extensions) {
+ if (extensionPoint.equals(pluginExtension.getPoint())) {
+ final IPluginObject[] children = pluginExtension.getChildren();
+ for (IPluginObject child : children) {
+ if (child instanceof IPluginElement) {
+ final IPluginElement pluginElement = (IPluginElement) child;
+ final IPluginAttribute[] attributes = pluginElement.getAttributes();
+ for (IPluginAttribute pluginAttribute : attributes) {
+ if ("file".equalsIgnoreCase(pluginAttribute.getName())) { //$NON-NLS-1$
+ final String strFile = pluginAttribute.getValue();
+ if (strFile != null && strFile.length() > 0) {
+ final IFile file = project.getFile(strFile);
+ if (file.exists() && elementFile.equals(file)) {
+ result = true;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Registers the given file in the plugin.xml of the given project, using
+ * the given extension point.
+ *
+ * @param project
+ * the project in which the element should be registered
+ * @param extensionToCheck
+ * the file extension of the element that should be registered
+ * @param extensionPoint
+ * the extension point that is used to register elements of this
+ * kind (elements must be declared with a "file" attribute)
+ */
+ /**
+ * Registers the given file in the plugin.xml of the given project, using
+ * the given extension point.
+ *
+ * @param file
+ * the file to register
+ * @param extensionPointId
+ * the extension point that is used to register the file
+ * @param elementName
+ * the name of the XML element in which an attribute named "file"
+ * will be set to the path of the file
+ * @throws ParserConfigurationException
+ * @throws IOException
+ * @throws SAXException
+ * @throws TransformerException
+ * @throws CoreException
+ */
+ public static void register(final IFile file, final String extensionPointId,
+ final String elementName) {
+ if (isRegistered(file, extensionPointId)) {
+ return;
+ }
+
+ final IPath filePath = file.getFullPath().removeFirstSegments(1);
+ final IProject project = file.getProject();
+
+ final IFile pluginXML = project.getFile("plugin.xml"); //$NON-NLS-1$
+ if (pluginXML.exists()) {
+ final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder docBuilder;
+
+ try {
+ docBuilder = docFactory.newDocumentBuilder();
+ final String pluginXmlLoc = pluginXML.getLocation().toOSString();
+ final Document doc = docBuilder.parse(pluginXmlLoc);
+ final Element root = doc.getDocumentElement();
+
+ final Text whitespace = doc.createTextNode(" "); //$NON-NLS-1$
+ root.appendChild(whitespace);
+
+ final Node extensionNode = doc.createElement("extension"); //$NON-NLS-1$
+ final Attr pointAttr = doc.createAttribute("point"); //$NON-NLS-1$
+ pointAttr.setValue(extensionPointId);
+ extensionNode.getAttributes().setNamedItem(pointAttr);
+ root.appendChild(extensionNode);
+
+ final Node elementNode = doc.createElement(elementName);
+ final Attr fileAttr = doc.createAttribute("file"); //$NON-NLS-1$
+ fileAttr.setValue(filePath.toString());
+ elementNode.getAttributes().setNamedItem(fileAttr);
+ extensionNode.appendChild(elementNode);
+
+ final TransformerFactory trFactory = TransformerFactory.newInstance();
+ final int indent = 3;
+ trFactory.setAttribute("indent-number", Integer.valueOf(indent)); //$NON-NLS-1$
+ final Transformer transformer = trFactory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
+ final StreamResult result = new StreamResult(new StringWriter());
+ final DOMSource source = new DOMSource(doc);
+ transformer.transform(source, result);
+ final String xmlString = result.getWriter().toString();
+
+ final byte[] byteArray = xmlString.getBytes("UTF-8"); //$NON-NLS-1$
+ pluginXML.setContents(new ByteArrayInputStream(byteArray), true, true,
+ new NullProgressMonitor());
+
+ } catch (Exception e) {
+ Logger.logError(e, Activator.getDefault());
+ }
+ } else {
+ try {
+ // create plugin.xml
+ final String template = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //$NON-NLS-1$
+ + "<?eclipse version=\"3.4\"?>\n" + "<plugin>\n" //$NON-NLS-1$ //$NON-NLS-2$
+ + " <extension point=\"{0}\">\n" + " <{1} file=\"{2}\"/>\n" //$NON-NLS-1$ //$NON-NLS-2$
+ + " </extension>\n" //$NON-NLS-1$
+ + "</plugin>\n"; //$NON-NLS-1$
+
+ final String content = NLS.bind(template, new Object[] { extensionPointId, elementName,
+ filePath.toString() });
+ byte[] byteArray;
+ byteArray = content.getBytes("UTF-8"); //$NON-NLS-1$
+ pluginXML.create(new ByteArrayInputStream(byteArray), true,
+ new NullProgressMonitor());
+ try {
+ BuildPropertiesUtils.addToBuild(pluginXML);
+ } catch (Exception e) {
+ Logger.logError(e, "Error adding file " + pluginXML.getFullPath() //$NON-NLS-1$
+ + " to the build.properties", null); //$NON-NLS-1$
+ }
+ } catch (Exception e) {
+ Logger.logError(e, Activator.getDefault());
+ }
+ }
+ }
+
+ private static final String JAVA_VERSION = "J2SE-1.5"; //$NON-NLS-1$
+ private static final String ACTIVATOR_NAME = "Activator"; //$NON-NLS-1$
+ private static final String MANIFEST_MF_TPL = "resources/MANIFEST.MF.template"; //$NON-NLS-1$
+ private static final String ACTIVATOR_TPL = "resources/Activator.java.template"; //$NON-NLS-1$
+ private static final String PDE_CLASSPATH_ID = "org.eclipse.pde.core.requiredPlugins"; //$NON-NLS-1$
+ private static final String PLUGIN_NATURE = "org.eclipse.pde.PluginNature"; //$NON-NLS-1$
+
+ private static void addPdeClassPath(final IProject project)
+ throws JavaModelException {
+ final IJavaProject javaProject = JavaCore.create(project);
+ final IClasspathEntry[] oldClassPath = javaProject.getRawClasspath();
+ for (IClasspathEntry classpathEntry : oldClassPath) {
+ if (classpathEntry.getPath().equals(new Path(PluginUtils.PDE_CLASSPATH_ID))) {
+ return;
+ }
+ }
+ final IClasspathEntry[] newClassPath = new IClasspathEntry[oldClassPath.length + 1];
+ System.arraycopy(oldClassPath, 0, newClassPath, 0, oldClassPath.length);
+ newClassPath[oldClassPath.length] = JavaCore
+ .newContainerEntry(new Path(PluginUtils.PDE_CLASSPATH_ID));
+ javaProject.setRawClasspath(newClassPath, new NullProgressMonitor());
+ }
+
+ /**
+ * @author Gregoire Dupe (Mia-Software) - Removing "Require-Bundle"
+ * statement
+ * @throws IOException
+ */
+ private static void createManifest(final IProject project)
+ throws CoreException, IOException {
+ final IFolder folder = project.getFolder("META-INF"); //$NON-NLS-1$
+ if (!folder.exists()) {
+ folder.create(true, true, new NullProgressMonitor());
+ }
+ final IFile manifestFile = folder.getFile("MANIFEST.MF"); //$NON-NLS-1$
+ if (!manifestFile.exists()) {
+ String template = FileUtils.getFileContents(Activator.getDefault()
+ .getBundle(), PluginUtils.MANIFEST_MF_TPL);
+ template = template.replace("{projectName}", project.getName()); //$NON-NLS-1$
+ final String packageName = PluginUtils
+ .stringToValidPackageName(project.getName());
+ template = template.replace("{packageName}", packageName); //$NON-NLS-1$
+ final String activator = PluginUtils
+ .bundleActivatorQualifiedName(packageName);
+ template = template.replace("{activator}", activator); //$NON-NLS-1$
+ template = template.replace("{javaVersion}", //$NON-NLS-1$
+ PluginUtils.JAVA_VERSION);
+ final InputStream source = new ByteArrayInputStream(
+ template.getBytes());
+ manifestFile.create(source, true, new NullProgressMonitor());
+ }
+ }
+
+ private static String bundleActivatorQualifiedName(final String packageName) {
+ return packageName + '.' + PluginUtils.ACTIVATOR_NAME;
+ }
+
+ private static void createActivator(final IProject project)
+ throws CoreException, IOException {
+ final String packageName = PluginUtils.stringToValidPackageName(project
+ .getName());
+ final String qualifiedName = PluginUtils
+ .bundleActivatorQualifiedName(packageName);
+ final String path = qualifiedName.replaceAll("\\.", "/"); //$NON-NLS-1$ //$NON-NLS-2$
+ final IFile activatorFile = project
+ .getFile(new Path("src").append(path).addFileExtension("java")); //$NON-NLS-1$ //$NON-NLS-2$
+ FolderUtils.createFolder((IFolder) activatorFile.getParent());
+ if (!activatorFile.exists()) {
+ final String template = FileUtils.getFileContents(Activator
+ .getDefault().getBundle(), PluginUtils.ACTIVATOR_TPL);
+ final String activatorContents = template.replace("{0}", packageName); //$NON-NLS-1$
+ final InputStream source = new ByteArrayInputStream(
+ activatorContents.getBytes());
+ activatorFile.create(source, true, new NullProgressMonitor());
+ }
+ }
+
+ /** Transform the given name into a valid package and bundle name */
+ private static String stringToValidPackageName(final String name) {
+ final StringBuilder builder = new StringBuilder();
+ char prev = ' ';
+ for (int i = 0; i < name.length(); i++) {
+ final char character = name.charAt(i);
+ if (character >= 'a' && character <= 'z' || character >= 'A'
+ && character <= 'Z' || character == '_') {
+ builder.append(character);
+ prev = character;
+ } else if (character >= '0' && character <= '9') {
+ if (builder.length() == 0 || prev == '.') {
+ builder.append("_"); //$NON-NLS-1$
+ }
+ builder.append(character);
+ prev = character;
+ } else if (character == '.') {
+ if (prev == '.') {
+ continue;
+ }
+ if (builder.length() == 0 || prev >= '0' && prev <= '9') {
+ builder.append("_"); //$NON-NLS-1$
+ }
+ builder.append(character);
+ prev = character;
+ } else {
+ builder.append("_"); //$NON-NLS-1$
+ }
+ }
+
+ String result = builder.toString();
+ // first letter to lowercase
+ if (result.length() > 0 && UCharacter.isUpperCase(result.charAt(0))) {
+ result = UCharacter.toLowerCase(result.charAt(0))
+ + result.substring(1);
+ }
+
+ final IStatus status = JavaConventions.validatePackageName(result,
+ JavaCore.VERSION_1_5, JavaCore.VERSION_1_5);
+ if (!status.isOK()) {
+ Logger.logWarning(
+ "Couldn't make valid package name from project name: " //$NON-NLS-1$
+ + status.getMessage(), Activator.getDefault());
+ result = name;
+ }
+ return result;
+ }
+
+ private static void addPdeNature(final IProject project)
+ throws CoreException {
+ final String pluginNature = PluginUtils.PLUGIN_NATURE;
+ final IProjectDescription description = project.getDescription();
+ final String[] natures = description.getNatureIds();
+ if (!Arrays.asList(natures).contains(pluginNature)) {
+ String[] newNatures = new String[natures.length + 1];
+ System.arraycopy(natures, 0, newNatures, 0, natures.length);
+ newNatures[natures.length] = pluginNature;
+ description.setNatureIds(newNatures);
+ project.setDescription(description, new NullProgressMonitor());
+ }
+ }
+
+ /**
+ * This method transforms a Java project into a plug-in project by creating
+ * a MANIFEST.MF and an activator and by adding a the PDE nature and the PDE
+ * classpath.
+ *
+ * @param project
+ * The project to transform in an plug-in project
+ * @throws CoreException
+ * @throws IOException
+ */
+ public static void configureAsPluginProject(final IProject project)
+ throws CoreException, IOException {
+ PluginUtils.addPdeNature(project);
+ // PDE builders are automatically added when the PDE nature is added
+ PluginUtils.addPdeClassPath(project);
+ PluginUtils.createManifest(project);
+ PluginUtils.createActivator(project);
+ }
+
+ /**
+ * This method returns true if the project is a plug-in project.
+ *
+ * @param project
+ * @return True if the project is a plug-in project.
+ * @throws CoreException
+ */
+ public static boolean isPluginProject(final IProject project)
+ throws CoreException {
+ boolean result = false;
+ if (project.isAccessible()) {
+ result = project.getNature(PluginUtils.PLUGIN_NATURE) != null;
+ }
+ return result;
+ }
+
+ /**
+ * This method returns true if the path refers a file or a folder contained
+ * in a plug-in project.
+ *
+ * @param path
+ * @return True if the path refers a file or a folder contained in a plug-in
+ * project.
+ * @throws CoreException
+ */
+ public static boolean isInPluginProject(final IPath path)
+ throws CoreException {
+ IProject project;
+ if (path.segmentCount() == 1) {
+ project = ResourcesPlugin.getWorkspace().getRoot()
+ .getProject(path.segment(0));
+ } else {
+ final IFolder folder = ResourcesPlugin.getWorkspace().getRoot()
+ .getFolder(path);
+ project = folder.getProject();
+ }
+ return isPluginProject(project);
+ }
+
+ /**
+ * This method imports a plug-in in the workspace.
+ *
+ * @param bundle
+ * the bundle to import into a new project
+ * @return the created project
+ * @throws CoreException
+ * in case of error
+ */
+ public static IProject importPlugin(final Bundle bundle) throws CoreException {
+ return PluginUtils.importPlugin(bundle, new IFilter<String>() {
+ public boolean filter(final String fileName) {
+ return true;
+ }
+ });
+ }
+
+ /**
+ * This method imports a plug-in in the workspace.
+ *
+ * @param bundle
+ * the bundle to import into a new project
+ * @param filter
+ * can be used to filter out files or folders from the import
+ * @return the created project
+ * @throws CoreException
+ * in case of error
+ */
+ public static IProject importPlugin(final Bundle bundle, final IFilter<String> filter) throws CoreException {
+ final IProject[] project = new IProject[1];
+ final IWorkspaceRunnable workspaceRunnable = new IWorkspaceRunnable() {
+ public void run(final IProgressMonitor monitor) throws CoreException {
+ project[0] = internalImportPlugin(bundle, filter);
+ }
+ };
+ ResourcesPlugin.getWorkspace().run(workspaceRunnable, new NullProgressMonitor());
+ return project[0];
+ }
+
+ /**
+ * This method imports a plug-in in the workspace.
+ *
+ * @param bundle
+ * the bundle to import into a new project
+ * @param filter
+ * can be used to filter out files or folders from the import
+ * @return the created project
+ * @throws CoreException
+ * in case of error
+ */
+ protected static IProject internalImportPlugin(final Bundle bundle, final IFilter<String> filter) throws CoreException {
+ final IProject project = createProjectWithUniqueName(bundle.getSymbolicName());
+ final List<IStatus> errors = new ArrayList<IStatus>();
+ final List<URL> urls = getURLsToCopy(bundle, project,
+ "/", filter, errors); //$NON-NLS-1$
+ for (URL url : urls) {
+ copyUrlToFile(project, errors, url);
+ }
+ handleErrors(errors);
+ project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
+ project.build(IncrementalProjectBuilder.CLEAN_BUILD, new NullProgressMonitor());
+ project.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
+ return project;
+ }
+
+ private static void copyUrlToFile(final IProject project,
+ final List<IStatus> errors, final URL url) throws CoreException {
+ try {
+ final InputStream inputStream = url.openStream();
+ final String strSubpath = url.toString().replaceAll(
+ "bundleentry://[^/]*/", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ final IFile file = project.getFile(new Path(strSubpath));
+ if (file.exists()) {
+ file.delete(true, new NullProgressMonitor());
+ }
+ final IContainer parent = file.getParent();
+ if ((!parent.exists()) && parent instanceof IFolder) {
+ createDir((IFolder) parent);
+ }
+ file.create(inputStream, true, new NullProgressMonitor());
+ inputStream.close();
+ } catch (FileNotFoundException e) {
+ /*
+ * gdupe> We ignore files removed during the execution of
+ * internalImportPlugin.
+ */
+ String message;
+ try {
+ message = String.format("Ignoring the missing file %s.", //$NON-NLS-1$
+ url.toURI());
+ } catch (URISyntaxException e1) {
+ message = String.format("Ignoring the missing file."); //$NON-NLS-1$
+ }
+ Logger.logError(message, Activator.getDefault());
+ } catch (IOException e) {
+ final Bundle localBundle = Activator.getDefault().getBundle();
+ final String symbolicName = localBundle.getSymbolicName();
+ final Status status = new Status(IStatus.ERROR, symbolicName,
+ e.getMessage(), e);
+ errors.add(status);
+ }
+ }
+
+ private static void createDir(final IFolder folder) throws CoreException {
+ final IContainer parent = folder.getParent();
+ if ((!parent.exists()) && parent instanceof IFolder) {
+ createDir((IFolder) parent);
+ }
+ folder.create(true, true, new NullProgressMonitor());
+ }
+ protected static void handleErrors(final List<IStatus> errors) throws CoreException {
+ if (!errors.isEmpty()) {
+ final IStatus[] statusArray = errors.toArray(new IStatus[errors.size()]);
+ final IStatus status = new MultiStatus(Activator.getDefault().getBundle().getSymbolicName(),
+ IStatus.ERROR, statusArray, "Errors importing project", new Exception()); //$NON-NLS-1$
+ throw new CoreException(status);
+ }
+ }
+
+ private static List<URL> getURLsToCopy(final Bundle bundle,
+ final IProject project, final String path,
+ final IFilter<String> filter, final List<IStatus> errors) {
+ final List<URL> result = new ArrayList<URL>();
+ final Enumeration<?> entryPaths = bundle.getEntryPaths(path);
+ while (entryPaths != null && entryPaths.hasMoreElements()) {
+ final Object nextElement = entryPaths.nextElement();
+ if (nextElement instanceof String) {
+ final String strSubpath = (String) nextElement;
+ if (filter.filter(strSubpath)) {
+ // directory
+ if (strSubpath.endsWith("/")) { //$NON-NLS-1$
+ final List<URL> fromSubFolder = getURLsToCopy(bundle,
+ project, strSubpath, filter, errors);
+ result.addAll(fromSubFolder);
+ } else {
+ final URL url = bundle.getEntry(strSubpath);
+ if (url != null) {
+ result.add(url);
+ }
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ protected static IProject createProjectWithUniqueName(final String baseName) throws CoreException {
+ final IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ final IWorkspaceRoot root = workspace.getRoot();
+ IProject project = root.getProject(baseName);
+ if (project.exists()) {
+ int version = 1;
+ final int maxIter = 100;
+ while (project.exists() && version < maxIter) {
+ final String uniqueName = baseName + " (" + version + ')'; //$NON-NLS-1$
+ project = root.getProject(uniqueName);
+ version++;
+ }
+ }
+ project.create(new NullProgressMonitor());
+ project.open(new NullProgressMonitor());
+ return project;
+ }
+}
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exception/PdeCoreUtilsRuntimeException.java b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exception/PdeCoreUtilsRuntimeException.java
new file mode 100644
index 00000000000..984c4f1b3e0
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exception/PdeCoreUtilsRuntimeException.java
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Mia-Software.
+ * 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:
+ * Gregoire Dupe (Mia-Software) - Bug 408344 - [Releng] Deep folders cause build break
+ *******************************************************************************/
+package org.eclipse.emf.facet.util.pde.core.internal.exception;
+
+public class PdeCoreUtilsRuntimeException extends RuntimeException {
+
+ private static final long serialVersionUID = -7462304304846835284L;
+
+ public PdeCoreUtilsRuntimeException() {
+ super();
+ }
+
+ public PdeCoreUtilsRuntimeException(final String message) {
+ super(message);
+ }
+
+ public PdeCoreUtilsRuntimeException(final Throwable cause) {
+ super(cause);
+ }
+
+ public PdeCoreUtilsRuntimeException(final String message, final Throwable cause) {
+ super(message, cause);
+ }
+
+}
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/BuildPropertiesUtils.java b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/BuildPropertiesUtils.java
new file mode 100644
index 00000000000..246d88c9133
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/BuildPropertiesUtils.java
@@ -0,0 +1,37 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Mia-Software.
+ * 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:
+ * Gregoire Dupe (Mia-Software) - Bug 387470 - [EFacet][Custom] Editors
+ *******************************************************************************/
+package org.eclipse.emf.facet.util.pde.core.internal.exported;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.pde.core.build.IBuildModel;
+import org.eclipse.pde.core.plugin.IPluginModelBase;
+
+/**
+ * @since 0.3
+ */
+public final class BuildPropertiesUtils {
+
+ private BuildPropertiesUtils() {
+ // Must not be used
+ }
+
+ public static void addToBuild(final IFile file) throws CoreException {
+ org.eclipse.emf.facet.util.pde.core.internal.BuildPropertiesUtils
+ .addToBuild(file);
+ }
+
+ public static IBuildModel getBuildModel(final IPluginModelBase model)
+ throws CoreException {
+ return org.eclipse.emf.facet.util.pde.core.internal.BuildPropertiesUtils
+ .getBuildModel(model);
+ }
+}
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/PluginUtils.java b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/PluginUtils.java
new file mode 100644
index 00000000000..5ac7457f626
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/PluginUtils.java
@@ -0,0 +1,73 @@
+/**
+ * Copyright (c) 2011 Mia-Software.
+ *
+ * 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:
+ * Grégoire Dupé (Soft-Maint) - Bug 387470 - [EFacet][Custom] Editors
+ */
+package org.eclipse.emf.facet.util.pde.core.internal.exported;
+
+import java.io.IOException;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.emf.facet.util.core.internal.exported.IFilter;
+import org.osgi.framework.Bundle;
+
+/**
+ * @since 0.4
+ */
+public final class PluginUtils {
+
+ private PluginUtils() {
+ // Must not be used.
+ }
+
+ public static boolean isInPluginProject(final IPath path)
+ throws CoreException {
+ return org.eclipse.emf.facet.util.pde.core.internal.PluginUtils
+ .isInPluginProject(path);
+ }
+
+ public static void register(final IFile file,
+ final String extensionPointId, final String elementName) {
+ org.eclipse.emf.facet.util.pde.core.internal.PluginUtils.register(file,
+ extensionPointId, elementName);
+ }
+
+ public static boolean isRegistered(final IFile iFile,
+ final String extensionPointId) {
+ return org.eclipse.emf.facet.util.pde.core.internal.PluginUtils
+ .isRegistered(iFile, extensionPointId);
+ }
+
+ public static void configureAsPluginProject(final IProject project)
+ throws CoreException, IOException {
+ org.eclipse.emf.facet.util.pde.core.internal.PluginUtils
+ .configureAsPluginProject(project);
+ }
+
+ public static boolean isPluginProject(final IProject project)
+ throws CoreException {
+ return org.eclipse.emf.facet.util.pde.core.internal.PluginUtils
+ .isPluginProject(project);
+ }
+
+ public static IProject importPlugin(final Bundle bundle,
+ final IFilter<String> filter) throws CoreException {
+ return org.eclipse.emf.facet.util.pde.core.internal.PluginUtils
+ .importPlugin(bundle, filter);
+ }
+
+ public static IProject importPlugin(final Bundle bundle)
+ throws CoreException {
+ return org.eclipse.emf.facet.util.pde.core.internal.PluginUtils
+ .importPlugin(bundle);
+ }
+}
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/TargetPlatformUtils.java b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/TargetPlatformUtils.java
new file mode 100644
index 00000000000..38771f96461
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/TargetPlatformUtils.java
@@ -0,0 +1,576 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Soft-Maint.
+ * 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:
+ * Fabien Treguer (Soft-Maint) - Bug 418565 - [Unit Test Failure] Missing dependencies during tests, target platform creation and load
+ ******************************************************************************/
+package org.eclipse.emf.facet.util.pde.core.internal.exported;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Array;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.emf.facet.util.pde.core.internal.Activator;
+import org.eclipse.emf.facet.util.pde.core.internal.exported.exception.PdeCoreUtilsException;
+import org.eclipse.emf.facet.util.pde.core.internal.exported.exception.ReflexiveDiscouragedAccessException;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Version;
+
+public final class TargetPlatformUtils {
+
+ private static final String BUNDLE_SEPARATOR = ","; //$NON-NLS-1$
+ private static final String JAR_EXT = ".jar"; //$NON-NLS-1$
+ private static final String JAR_DIRECTORY = "jarFiles"; //$NON-NLS-1$
+ private static final int BUFFER_SIZE = 4096;
+ private static final String BUNDLE_PREFIX = "reference:file:"; //$NON-NLS-1$
+ private static final String FRAMEWORK_PREFIX = "file:"; //$NON-NLS-1$
+ private static final String OSGI_PROP = "osgi.bundles"; //$NON-NLS-1$
+ private static final String OSGI_PROP_FRK = "osgi.framework"; //$NON-NLS-1$
+ private static final String ARRAY_PREFIX = "[L"; //$NON-NLS-1$
+
+ private TargetPlatformUtils() {
+ //Must not be used.
+ }
+
+ /**
+ * Creates and loads a target platform with all needed bundles.
+ * @throws PdeCoreUtilsException
+ * @throws IOException
+ */
+ public static void loadTargetPlatform() throws PdeCoreUtilsException {
+ String copyDirectoryPath;
+ try {
+ /*ITargetPlatformService*/
+ final Object targetPlServ = reflexiveCall(
+ true,
+ "org.eclipse.pde.internal.core.target.TargetPlatformService", //$NON-NLS-1$
+ "getDefault", //$NON-NLS-1$
+ null);
+ /*ITargetDefinition*/
+ final Object targetDefinition = reflexiveCall(
+ false,
+ targetPlServ,
+ "newTarget", //$NON-NLS-1$
+ null);
+ copyDirectoryPath = Activator.getDefault().getStateLocation()
+ + File.separator + JAR_DIRECTORY;
+
+ final File copyDirectory = new File(copyDirectoryPath);
+ copyDirectory.mkdirs();
+
+ final List<String> dirPaths = parseBundlesList();
+
+ /*IBundleContainer*/
+ final Object[] dirContainers =
+ copyJarsAndGetContainers(dirPaths, copyDirectory);
+ reflexiveCall(
+ false,
+ targetDefinition,
+ getContainerSetterName(),
+ new Object[]{dirContainers});
+ reflexiveCall(
+ false,
+ targetDefinition,
+ "resolve", //$NON-NLS-1$
+ new Object[]{new NullProgressMonitor()});
+ reflexiveCall(
+ true,
+ getLoadTargetDefinitionJobQualifiedName(),
+ "load", //$NON-NLS-1$
+ new Object[]{targetDefinition});
+ reflexiveCall(
+ false,
+ targetPlServ,
+ "saveTargetDefinition", //$NON-NLS-1$
+ new Object[]{targetDefinition});
+ } catch (Exception e) {
+ throw new PdeCoreUtilsException(e);
+ }
+ new File(copyDirectoryPath).delete();
+ }
+
+ private static boolean isHigherVersion() {
+ final Version version = new Version(3, 7, 1);
+ final Bundle bundle = Platform.getBundle("org.eclipse.pde.core"); //$NON-NLS-1$
+ final Version currentVersion = bundle.getVersion();
+ return currentVersion.compareTo(version) > 0;
+ }
+
+ private static String getLoadTargetDefinitionJobQualifiedName() {
+ String lTDefJobName;
+ if (isHigherVersion()) {
+ lTDefJobName =
+ "org.eclipse.pde.core.target.LoadTargetDefinitionJob"; //$NON-NLS-1$
+ } else {
+ lTDefJobName =
+ "org.eclipse.pde.internal.core.target.provisional.LoadTargetDefinitionJob"; //$NON-NLS-1$
+ }
+ return lTDefJobName;
+ }
+
+ private static String getContainerSetterName() {
+ String setterName;
+ if (isHigherVersion()) {
+ setterName =
+ "setTargetLocations"; //$NON-NLS-1$
+ } else {
+ setterName =
+ "setBundleContainers"; //$NON-NLS-1$
+ }
+ return setterName;
+ }
+
+ /**
+ * Parses osgi.bundles property value to load all required bundles directories.
+ * @return List<String> array
+ * @throws PdeCoreUtilsException
+ * @throws IOException
+ */
+ private static List<String> parseBundlesList()
+ throws PdeCoreUtilsException, IOException {
+ final LinkedList<String> result = new LinkedList<String>();
+ final String bundlesFromConfig = System.getProperty(OSGI_PROP);
+ final String frwkFromConfig = System.getProperty(OSGI_PROP_FRK);
+ if (bundlesFromConfig != null && !"".equals(bundlesFromConfig)) { //$NON-NLS-1$
+ final String[] bundlesStr =
+ bundlesFromConfig.split(BUNDLE_SEPARATOR);
+ result.addAll(trimPaths(bundlesStr, BUNDLE_PREFIX));
+ }
+ if (frwkFromConfig != null && !"".equals(frwkFromConfig)) { //$NON-NLS-1$
+ final String[] frameworkStr =
+ frwkFromConfig.split(BUNDLE_SEPARATOR);
+ result.addAll(trimPaths(frameworkStr, FRAMEWORK_PREFIX));
+ }
+ return result;
+ }
+
+ /**
+ * Returns list of trimmed paths;
+ * @param paths String[]
+ * @param prefix String
+ * @return List<String>
+ */
+ private static List<String> trimPaths(final String[] paths,
+ final String prefix) {
+ final LinkedList<String> result = new LinkedList<String>();
+ for (String path : paths) {
+ int indexSuffix = path.indexOf("@"); //$NON-NLS-1$
+ if (indexSuffix < 0) {
+ indexSuffix = path.length();
+ }
+ result.add(path.substring(prefix.length(),
+ indexSuffix));
+ }
+ return result;
+ }
+
+ /**
+ * Reflexive call of DirectoryBundleContainer constructor.
+ * @param containerPath String
+ * @return Object
+ * @throws PdeCoreUtilsException
+ */
+ private static Object newDirectoryBundleContainer(
+ final String containerPath)
+ throws PdeCoreUtilsException {
+ Object container = null;
+ try {
+ final Bundle bundle = Activator.getDefault().getBundle();
+ final Class<?> classs =
+ bundle.loadClass("org.eclipse.pde.internal.core.target.DirectoryBundleContainer"); //$NON-NLS-1$
+ final Class<?>[] paramTypes = new Class[] {containerPath.getClass()};
+ final Constructor<?> constructor = classs.getConstructor(paramTypes);
+ final Object[] initargs = new Object[] { containerPath };
+ container = constructor.newInstance(initargs);
+ } catch (Exception e) {
+ throw new PdeCoreUtilsException(e);
+ }
+ return container;
+ }
+
+ /**
+ * Copies all needed jar files in a directory and returns set of containers.
+ * @param bundlesStr List<String>
+ * @param copyDirectory File
+ * @return Object[]
+ * @throws PdeCoreUtilsException
+ * @throws IOException
+ */
+ private static Object[] copyJarsAndGetContainers(
+ final List<String> bundlesStr, final File copyDirectory)
+ throws PdeCoreUtilsException, IOException {
+ final List<Object> bundleContainers = new LinkedList<Object>();
+ final Iterator<String> bundlesStrIter = bundlesStr.iterator();
+ while (bundlesStrIter.hasNext()) {
+ final File bundleFile = new File(bundlesStrIter.next()); // NOPMD
+ // NOPMD: gdupe> No other way to write this code
+ if (bundleFile.exists()) {
+ if (bundleFile.isDirectory()) {
+ copyJarFileOrAddContainerDirectory(bundleFile,
+ bundleContainers, copyDirectory);
+ } else {
+ try {
+ //FIXME gdupe> is a call to close() required ?
+ final JarFile jarFile = new JarFile(bundleFile); // NOPMD
+ // NOPMD: gdupe> No other way to write this code
+ copyJarFile(jarFile, copyDirectory);
+ } catch (IOException e) {
+ throw new PdeCoreUtilsException(e);
+ }
+ }
+ }
+ }
+ final Object copyDirContainer =
+ newDirectoryBundleContainer(copyDirectory.getAbsolutePath());
+ bundleContainers.add(copyDirContainer);
+ return bundleContainers.toArray();
+ }
+
+ /**
+ * Copies jarFile to the specified directory or creates a container
+ * if bundleFile is a directory.
+ * @param bundleFile File
+ * @param bundleContainers List<Object>
+ * @param copyDirectory File
+ * @throws PdeCoreUtilsException
+ * @throws IOException
+ */
+ private static void copyJarFileOrAddContainerDirectory(
+ final File bundleFile, final List<Object> bundleContainers,
+ final File copyDirectory)
+ throws PdeCoreUtilsException, IOException {
+ Object container;
+ //FIXME gdupe> is a call to close() required ?
+ final JarFile jarFile = getFirstMatchingJarFile(bundleFile);
+ if (jarFile == null) {
+ container = newDirectoryBundleContainer(bundleFile.getAbsolutePath());
+ bundleContainers.add(container);
+ } else {
+ copyJarFile(jarFile, copyDirectory);
+ }
+ }
+
+ /**
+ * Return first jar file that match with the bundle directory.
+ * @param bundleFile File
+ * @return JarFile
+ * @throws IOException
+ */
+ private static JarFile getFirstMatchingJarFile(final File bundleFile)
+ throws IOException {
+ JarFile result = null;
+ if (bundleFile.exists() && bundleFile.isDirectory()) {
+ final String bundleFileName = bundleFile.getName();
+ final File jar = findJarFile(bundleFile, bundleFileName);
+ if (jar != null) {
+ result = new JarFile(jar);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Search jar file in directory and sub-directories.
+ * @param directory File
+ * @param fileName String
+ * @return File
+ */
+ private static File findJarFile(final File directory,
+ final String fileName) {
+ final File[] children = directory.listFiles();
+ final LinkedList<File> toVisit = new LinkedList<File>();
+ int cpt = 0;
+ File foundFile = null;
+ String childName;
+ while (foundFile != null && cpt < children.length) {
+ final File child = children[cpt];
+ if (child.isFile()) {
+ childName = child.getName();
+ if (childName.startsWith(fileName)
+ && childName.endsWith(JAR_EXT)) {
+ foundFile = child;
+ }
+ } else {
+ toVisit.add(child);
+ }
+ cpt++;
+ }
+ if (foundFile == null) {
+ foundFile = searchInSubDirectories(fileName, toVisit);
+ }
+ return foundFile;
+ }
+
+ /**
+ * Search jar file and sub-directories.
+ * @param fileName String
+ * @param toVisit List<File>
+ * @return File
+ */
+ private static File searchInSubDirectories(final String fileName,
+ final List<File> toVisit) {
+ File result = null;
+ final Iterator<File> childrenIterator = toVisit.iterator();
+ while (childrenIterator.hasNext() && result != null) {
+ final File child = childrenIterator.next();
+ result = findJarFile(child.getAbsoluteFile(), fileName);
+ }
+ return result;
+ }
+
+
+ /**
+ * Copies a jarFile into specified directory.
+ * @param jarFile JarFile
+ * @param copyTo File
+ * @throws PdeCoreUtilsException
+ */
+ private static void copyJarFile(final JarFile jarFile, final File copyTo)
+ throws PdeCoreUtilsException {
+ final String fileName = jarFile.getName();
+ final String bundleName = fileName.substring(fileName
+ .lastIndexOf(File.separator));
+ final File copyToDirectory = new File(copyTo, bundleName);
+ JarOutputStream jarOutputStream = null;
+ try {
+ jarOutputStream = new JarOutputStream(
+ new FileOutputStream(copyToDirectory));
+ final Enumeration<JarEntry> jarFileEntries = jarFile.entries();
+ final byte[] buffer = new byte[BUFFER_SIZE];
+ while (jarFileEntries.hasMoreElements()) {
+ final JarEntry sourceEntry = jarFileEntries.nextElement();
+ final InputStream inputStream =
+ jarFile.getInputStream(sourceEntry);
+ final JarEntry targetEntry =
+ new JarEntry(sourceEntry.getName()); // NOPMD
+ // ftreguer> NOPMD This loop has to create one jar entry for
+ // each loop iteration
+ jarOutputStream.putNextEntry(targetEntry);
+ int bytesRead = inputStream.read(buffer);
+ while (bytesRead != -1) {
+ jarOutputStream.write(buffer, 0, bytesRead);
+ bytesRead = inputStream.read(buffer);
+ }
+ inputStream.close();
+ jarOutputStream.flush();
+ jarOutputStream.closeEntry();
+ }
+ } catch (IOException e) {
+ throw new PdeCoreUtilsException(e);
+ } finally { // NOPMD gdupe> No other choice if I want to managed the
+ // stream closing
+ if (jarOutputStream != null) {
+ try {
+ jarOutputStream.close();
+ } catch (IOException e) {
+ throw new PdeCoreUtilsException(e);
+ }
+ }
+ }
+ }
+
+ /**
+ * Calls a method reflexively on an object or a class (static call) with
+ * specified arguments.
+ * @param staticCall boolean
+ * @param callOn Object
+ * @param calledMethodName String
+ * @param args Object[]
+ * @throws ExpException
+ */
+ private static Object reflexiveCall(final boolean staticCall,
+ final Object callOn, final String calledMethodName,
+ final Object[] args) throws ReflexiveDiscouragedAccessException {
+ Object result = null;
+ Method aMethod;
+ final Class<?>[] classes = getClassesFromArgs(args);
+ try {
+ if (staticCall) {
+ // ftreguer> Static Call, the second parameter should be a
+ // String representing Class name.
+ if (callOn instanceof String) {
+ final Class<?> clazz = Activator.getDefault()
+ .getBundle().loadClass((String) callOn);
+ aMethod = getMethod(clazz, calledMethodName, classes);
+ } else {
+ throw new ReflexiveDiscouragedAccessException(
+ "Invalid parameter, callOn parameter should be a String. (Static Call)"); //$NON-NLS-1$
+ }
+ } else {
+ aMethod = getMethod(callOn.getClass(),
+ calledMethodName, classes);
+ }
+ if (aMethod == null) {
+ throw new ReflexiveDiscouragedAccessException(
+ "API break, no method with specified arguments found."); //$NON-NLS-1$
+ }
+ result = aMethod.invoke(callOn, manageArguments(args));
+ } catch (Exception e) {
+ throw new ReflexiveDiscouragedAccessException(e);
+ }
+ return result;
+ }
+
+
+ /**
+ * Transforms Object arrays to specific arrays.
+ * @param arguments Object[]
+ * @return Object[]
+ */
+ private static Object[] manageArguments(final Object[] arguments) {
+ Object[] result;
+ if (arguments != null && arguments.length > 0) {
+ result = new Object[arguments.length];
+ Object arg;
+ for (int i = 0; i < arguments.length; i++) {
+ arg = arguments[i];
+ if (isArray(arg)) {
+ final Object argElement = ((Object[]) arg)[0];
+ final int arraySize = Array.getLength(arg);
+ Object[] argTmp = (Object[]) Array.newInstance(
+ argElement.getClass(), arraySize);
+ for (int y = 0; y < arraySize; y++) {
+ argTmp[y] = ((Object[]) arg)[y];
+ }
+ result[i] = argTmp;
+ } else {
+ result[i] = arg;
+ }
+ }
+ } else {
+ result = arguments;
+ }
+ return result;
+ }
+
+ /**
+ * Returns true if the parameter represents an array.
+ * @param obj Object
+ * @return boolean
+ */
+ private static boolean isArray(final Object obj) {
+ return obj.getClass().getName().startsWith(ARRAY_PREFIX);
+ }
+
+ /**
+ * Returns array of Class that represents types of each arguments.
+ * @param args Object[]
+ * @return Class<?>[]
+ */
+ private static Class<?>[] getClassesFromArgs(final Object[] args) {
+ Class<?>[] classes = null;
+ if (args != null) {
+ classes = new Class[args.length];
+ Object arg = null;
+ for (int i = 0; i < args.length; i++) {
+ arg = args[i];
+ if (arg != null) {
+ if (isArray(arg)) {
+ arg = ((Object[]) arg)[0];
+ arg = Array.newInstance(arg.getClass(), 0);
+ }
+ classes[i] = arg.getClass();
+ }
+ }
+ }
+ return classes;
+ }
+
+ /**
+ * Returns a Method object that correspond to the specified name
+ * and parameters.
+ * @param clazz Class<?>
+ * @param methodName String
+ * @param parameterTypes Class<?>[]
+ * @return Method
+ * @throws ClassNotFoundException
+ */
+ private static Method getMethod(final Class<?> clazz,
+ final String methodName, final Class<?>... parameterTypes)
+ throws ClassNotFoundException {
+ Method result = null;
+ final Method[] methods = clazz.getMethods();
+ for (Method method : methods) {
+ if (methodName.equals(method.getName())) {
+ final Class<?>[] parameters = method.getParameterTypes();
+ if (checkTypes(parameters, parameterTypes)) {
+ result = method;
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Checks if all parameters are the same in both arrays.
+ * @param refParams
+ * @param parameterToTest
+ * @return boolean
+ * @throws ClassNotFoundException
+ */
+ private static boolean checkTypes(final Class<?>[] refParams,
+ final Class<?>[] parameterToTest) throws ClassNotFoundException {
+ boolean result = true;
+ if (refParams != null && parameterToTest != null
+ && (refParams.length == parameterToTest.length)) {
+ String refClazzName = null;
+ String clazzName = null;
+ for (int i = 0; i < parameterToTest.length; i++) {
+ refClazzName = refParams[i].getName();
+ clazzName = parameterToTest[i].getName();
+ if (!clazzName.equals(refClazzName)) {
+ final boolean isRefArray = refClazzName
+ .startsWith(ARRAY_PREFIX);
+ final boolean isParamArray = clazzName
+ .startsWith(ARRAY_PREFIX);
+ if (isRefArray == isParamArray) {
+ if (isRefArray) {
+ refClazzName = refClazzName
+ .substring(ARRAY_PREFIX.length(),
+ refClazzName.length() - 1);
+ clazzName = clazzName
+ .substring(ARRAY_PREFIX.length(),
+ clazzName.length() - 1);
+ }
+ result = paramExtendsRef(refClazzName, clazzName);
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Returns true if className is a SubType of refClassName.
+ * @param refClassName String
+ * @param className String
+ * @return boolean
+ * @throws ClassNotFoundException
+ */
+ private static boolean paramExtendsRef(final String refClassName,
+ final String className) throws ClassNotFoundException {
+ final Bundle bundle = Activator.getDefault().getBundle();
+ final Class<?> refClazz = bundle.loadClass(refClassName);
+ final Class<?> clazz = bundle.loadClass(className);
+ return refClazz.isAssignableFrom(clazz);
+ }
+
+}
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/exception/PdeCoreUtilsException.java b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/exception/PdeCoreUtilsException.java
new file mode 100644
index 00000000000..23e6b5ade02
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/exception/PdeCoreUtilsException.java
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Soft-Maint.
+ * 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:
+ * Fabien Treguer (Soft-Maint) - Bug 418565 - [Unit Test Failure] Missing dependencies during tests, target platform creation and load
+ ******************************************************************************/
+package org.eclipse.emf.facet.util.pde.core.internal.exported.exception;
+
+public class PdeCoreUtilsException extends Exception {
+
+ private static final long serialVersionUID = -7462304304846835284L;
+
+ public PdeCoreUtilsException() {
+ super();
+ }
+
+ public PdeCoreUtilsException(final String message) {
+ super(message);
+ }
+
+ public PdeCoreUtilsException(final Throwable cause) {
+ super(cause);
+ }
+
+ public PdeCoreUtilsException(final String message, final Throwable cause) {
+ super(message, cause);
+ }
+
+}
diff --git a/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/exception/ReflexiveDiscouragedAccessException.java b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/exception/ReflexiveDiscouragedAccessException.java
new file mode 100644
index 00000000000..653c4828fc2
--- /dev/null
+++ b/plugins/facet/org.eclipse.papyrus.emf.facet.util.pde.core/src/org/eclipse/emf/facet/util/pde/core/internal/exported/exception/ReflexiveDiscouragedAccessException.java
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Soft-Maint.
+ * 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:
+ * Fabien Treguer (Soft-Maint) - Bug 418565 - [Unit Test Failure] Missing dependencies during tests, target platform creation and load
+ ******************************************************************************/
+package org.eclipse.emf.facet.util.pde.core.internal.exported.exception;
+
+public class ReflexiveDiscouragedAccessException extends Exception {
+
+ private static final long serialVersionUID = -7462304304846835284L;
+
+ public ReflexiveDiscouragedAccessException() {
+ super();
+ }
+
+ public ReflexiveDiscouragedAccessException(final String message) {
+ super(message);
+ }
+
+ public ReflexiveDiscouragedAccessException(final Throwable cause) {
+ super(cause);
+ }
+
+ public ReflexiveDiscouragedAccessException(final String message, final Throwable cause) {
+ super(message, cause);
+ }
+
+}

Back to the top