Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension point to contribute code #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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/JavaSE-16"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
28 changes: 28 additions & 0 deletions bundles/com.obeo.loophole.extensionpoint.definition/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.obeo.loophole.extensionpoint.definition</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>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=16
org.eclipse.jdt.core.compiler.compliance=16
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=16
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Definition
Bundle-SymbolicName: com.obeo.loophole.extensionpoint.definition;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: OBEO
Automatic-Module-Name: com.obeo.loophole.extensionpoint.definition
Bundle-RequiredExecutionEnvironment: JavaSE-16
Export-Package: com.obeo.loophole.extensionpoint.definition
Require-Bundle: org.eclipse.core.runtime;bundle-version="3.22.0",
org.eclipse.e4.core.di;bundle-version="1.7.800"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
schema/,\
plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension-point id="injectCustomCode" name="Inject Custom Code" schema="schema/injectCustomCode.exsd"/>

</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="com.obeo.loophole.extensionpoint.definition" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
<appinfo>
<meta.schema plugin="com.obeo.loophole.extensionpoint.definition" id="injectCustomCode" name="Inject Custom Code"/>
</appinfo>
<documentation>
[Enter description of this extension point.]
</documentation>
</annotation>

<element name="extension">
<annotation>
<appinfo>
<meta.element />
</appinfo>
</annotation>
<complexType>
<choice>
<element ref="classContentAddition" minOccurs="1" maxOccurs="unbounded"/>
</choice>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>

</documentation>
<appinfo>
<meta.attribute translatable="true"/>
</appinfo>
</annotation>
</attribute>
</complexType>
</element>

<element name="classContentAddition">
<complexType>
<attribute name="class" type="string">
<annotation>
<documentation>

</documentation>
<appinfo>
<meta.attribute kind="java" basedOn=":com.obeo.loophole.extensionpoint.definition.IAdditionalCustomCode"/>
</appinfo>
</annotation>
</attribute>
</complexType>
</element>

<annotation>
<appinfo>
<meta.section type="since"/>
</appinfo>
<documentation>
[Enter the first release in which this extension point appears.]
</documentation>
</annotation>

<annotation>
<appinfo>
<meta.section type="examples"/>
</appinfo>
<documentation>
[Enter extension point usage example here.]
</documentation>
</annotation>

<annotation>
<appinfo>
<meta.section type="apiinfo"/>
</appinfo>
<documentation>
[Enter API information here.]
</documentation>
</annotation>

<annotation>
<appinfo>
<meta.section type="implementation"/>
</appinfo>
<documentation>
[Enter information about supplied implementation of this extension point.]
</documentation>
</annotation>


</schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*
*******************************************************************************/
package com.obeo.loophole.extensionpoint.definition;

/**
* Interface to implement to contribute additional custom code.
*
* @author Gabriel Jolly - Initial contribution
*/
public interface IAdditionalCustomCode {

/**
* Check if the context is good.
*
* @param className Name of the class
* @param customClassName Name of the custom class
* @return true if the given class have additional custom code to provide, false
* otherwise.
*/
public boolean apply(String className, String customClassName);

/**
* Generate code to add in custom classes.
*
* @param className Name of the class
* @param customClassName Name of the custom class
* @param sb StringBuilder to contribute
*/
public void generateCode(String className, String customClassName, StringBuilder sb);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*
*******************************************************************************/
package com.obeo.loophole.extensionpoint.definition;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.e4.core.di.annotations.Execute;

/**
* Entry point of the code contribution.
*
* @author Gabriel Jolly - Initial contribution
*/
public class InjectCustomCodeContribution {
private static final String INJECT_CUSTOM_CODE_ID = "com.obeo.loophole.extensionpoint.definition.injectCustomCode";

@Execute
public void execute(IExtensionRegistry registry, String className, String customClassName, StringBuilder sb) {
IConfigurationElement[] config = registry.getConfigurationElementsFor(INJECT_CUSTOM_CODE_ID);
try {
for (IConfigurationElement e : config) {
System.out.println("Evaluating extension");
final Object o = e.createExecutableExtension("class");
if (o instanceof IAdditionalCustomCode) {
IAdditionalCustomCode additionalCustomCode = (IAdditionalCustomCode) o;
if (additionalCustomCode.apply(className, customClassName)) {
additionalCustomCode.generateCode(className, customClassName, sb);
}
}
}
} catch (CoreException ex) {
System.out.println(ex.getMessage());
}
}

/**
* Allow to contribute additional custom code.
*
* @param className Name of the class to contribute
* @param customClassName Name of the custom class to contribute
* @param sb {@link StringBuilder} to contribute
*/
public static void addGeneratedCode(String className, String customClassName, StringBuilder sb) {
new InjectCustomCodeContribution().execute(Platform.getExtensionRegistry(), className, customClassName, sb);
}
}
3 changes: 2 additions & 1 deletion bundles/org.eclipselabs.emf.loophole/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.5.0,4.0.0)",
org.eclipse.emf.codegen.ecore.ui;bundle-version="[2.5.0,3.0.0)",
org.eclipse.emf.edit;bundle-version="[2.5.0,3.0.0)",
org.eclipse.emf.ecore.xmi;bundle-version="[2.5.0,3.0.0)",
org.eclipse.jdt.core;bundle-version="[3.5.0,4.0.0)"
org.eclipse.jdt.core;bundle-version="[3.5.0,4.0.0)",
com.obeo.loophole.extensionpoint.definition
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/*******************************************************************************
* Copyright (c) 2013 Obeo.
* 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:
* Obeo - initial API and implementation
* Copyright (c) 2013, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*
*******************************************************************************/
package org.eclipselabs.emf.loophole.internal.generator;

import com.obeo.loophole.extensionpoint.definition.InjectCustomCodeContribution;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
Expand All @@ -34,6 +36,7 @@

/**
* @author Mikaël Barbero - Remove any MWE dependencies
* @author Gabriel Jolly - Extension point
*/
public class LoopholeGeneratorAdapterDescriptor implements GeneratorAdapterFactory.Descriptor {

Expand Down Expand Up @@ -83,6 +86,7 @@ protected void generateCustomClass(String className, String customClassName, URI
sb.append("package ").append(customClassName.substring(0, customLastIndexOfDot)).append(";\n\n");
sb.append("import ").append(className).append(";\n\n");
sb.append("public class ").append(customClassName.substring(customLastIndexOfDot+1)).append(" extends ").append(className.substring(lastIndexOfDot+1)).append(" {\n\n");
InjectCustomCodeContribution.addGeneratedCode(className, customClassName, sb);
sb.append("}\n");

try {
Expand Down Expand Up @@ -110,6 +114,7 @@ protected void generateCustomProvider(String className, String customClassName,
sb.append("\tpublic ").append(customClassName.substring(customLastIndexOfDot+1)).append("(AdapterFactory adapterFactory) {\n");
sb.append("\t\tsuper(adapterFactory);\n");
sb.append("\t}\n");
InjectCustomCodeContribution.addGeneratedCode(className, customClassName, sb);
sb.append("}\n");

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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/JavaSE-16"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
28 changes: 28 additions & 0 deletions samples/com.obeo.loophole.extensionpoint.contribution/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.obeo.loophole.extensionpoint.contribution</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>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=16
org.eclipse.jdt.core.compiler.compliance=16
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=16
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Contribution
Bundle-SymbolicName: com.obeo.loophole.extensionpoint.contribution;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: OBEO
Automatic-Module-Name: com.obeo.loophole.extensionpoint.contribution
Bundle-RequiredExecutionEnvironment: JavaSE-16
Require-Bundle: com.obeo.loophole.extensionpoint.definition;bundle-version="1.0.0",
org.eclipse.core.runtime;bundle-version="3.22.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
plugin.xml
Loading