make the extension Requirement bytecode injector reusable

Signed-off-by: Raymond Augé <rotty3000@apache.org>
diff --git a/cdi-build-tools/pom.xml b/cdi-build-tools/pom.xml
new file mode 100644
index 0000000..d37a461
--- /dev/null
+++ b/cdi-build-tools/pom.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+	<modelVersion>4.0.0</modelVersion>
+
+	<parent>
+		<groupId>org.apache.aries.cdi</groupId>
+		<artifactId>org.apache.aries.cdi</artifactId>
+		<version>1.1.0-SNAPSHOT</version>
+		<relativePath>..</relativePath>
+	</parent>
+
+	<artifactId>org.apache.aries.cdi.build.tools</artifactId>
+	<name>Apache Aries CDI - Tools used during build</name>
+	<description>Apache Aries CDI - Tools used during build</description>
+
+	<properties>
+		<gpg.skip>true</gpg.skip>
+		<maven.deploy.skip>true</maven.deploy.skip>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.osgi</groupId>
+			<artifactId>org.osgi.service.cdi</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.osgi</groupId>
+			<artifactId>osgi.annotation</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>net.bytebuddy</groupId>
+			<artifactId>byte-buddy</artifactId>
+			<version>${byte.buddy.version}</version>
+			<scope>provided</scope>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>biz.aQute.bnd</groupId>
+				<artifactId>bnd-maven-plugin</artifactId>
+			</plugin>
+		</plugins>
+	</build>
+</project>
\ No newline at end of file
diff --git a/cdi-build-tools/src/main/java/org/apache/aries/cdi/build/tools/AddExtensionRequirement.java b/cdi-build-tools/src/main/java/org/apache/aries/cdi/build/tools/AddExtensionRequirement.java
new file mode 100644
index 0000000..9e6aef3
--- /dev/null
+++ b/cdi-build-tools/src/main/java/org/apache/aries/cdi/build/tools/AddExtensionRequirement.java
@@ -0,0 +1,127 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.aries.cdi.build.tools;
+
+import java.io.IOException;
+
+import org.osgi.annotation.bundle.Requirement;
+import org.osgi.service.cdi.CDIConstants;
+
+import net.bytebuddy.build.BuildLogger;
+import net.bytebuddy.build.Plugin;
+import net.bytebuddy.description.annotation.AnnotationDescription;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.dynamic.ClassFileLocator;
+import net.bytebuddy.dynamic.DynamicType.Builder;
+
+/**
+ * Example:
+ * <pre>
+ * &lt;transformation>
+ *   &lt;plugin>org.apache.aries.cdi.build.tools.AddExtensionRequirement.AddExtensionRequirement&lt;/plugin>
+ *   &lt;arguments>
+ *     &lt;argument>
+ *       &lt;index>1</index>
+ *       &lt;value>eclipse.microprofile.config&lt;/value>
+ *     &lt;/argument>
+ *     &lt;argument>
+ *       &lt;index>2&lt;/index>
+ *       &lt;value>${mp.config.version}&lt;/value>
+ *     &lt;/argument>
+ *     &lt;argument>
+ *       &lt;index>3&lt;/index>
+ *       &lt;value>org.eclipse.microprofile.config.inject.ConfigProperty&lt;/value>
+ *     &lt;/argument>
+ *   &lt;/arguments>
+ * &lt;/transformation>
+ * </pre>
+ */
+public class AddExtensionRequirement implements Plugin {
+
+	private final BuildLogger buildLogger;
+	private final String extension;
+	private final String version;
+	private final String name;
+	private final Match match;
+
+	private enum Match {
+		CLASS, PACKAGE, PREFIX
+	}
+
+	public AddExtensionRequirement(
+		BuildLogger buildLogger, String extension, String version, String glob) {
+
+		this.buildLogger = buildLogger;
+		this.extension = extension;
+		this.version = version;
+
+		String name = glob;
+
+		if (glob.endsWith(".**")) {
+			match = Match.PREFIX;
+			name = glob.substring(0, glob.length() - 3);
+		}
+		else if (glob.endsWith(".*")) {
+			match = Match.PACKAGE;
+			name = glob.substring(0, glob.length() - 2);
+		}
+		else {
+			match = Match.CLASS;
+			name = glob;
+		}
+
+		this.name = name;
+	}
+
+	@Override
+	public boolean matches(TypeDescription target) {
+		String className = target.getName();
+		String packageName = target.getPackage().getName();
+
+		boolean matches = false;
+		switch (match) {
+			case CLASS: {
+				matches = className.equals(name);
+				break;
+			}
+			case PACKAGE: {
+				matches = packageName.equals(name);
+				break;
+			}
+			case PREFIX: {
+				matches = packageName.startsWith(name);
+			}
+		}
+
+		return matches;
+	}
+
+	@Override
+	public Builder<?> apply(Builder<?> builder, TypeDescription typeDescription, ClassFileLocator cfl) {
+		buildLogger.info("Processing class: " + typeDescription.getActualName());
+
+		return builder.annotateType(
+			AnnotationDescription.Builder.ofType(Requirement.class)
+				.define("namespace", CDIConstants.CDI_EXTENSION_PROPERTY)
+				.define("name", extension)
+				.define("version", version)
+				.build());
+	}
+
+	@Override
+	public void close() throws IOException {
+	}
+
+}
diff --git a/cdi-extension-mp-config/pom.xml b/cdi-extension-mp-config/pom.xml
index 3aa538a..3059819 100644
--- a/cdi-extension-mp-config/pom.xml
+++ b/cdi-extension-mp-config/pom.xml
@@ -57,10 +57,6 @@
 				<configuration>
 					<bnd><![CDATA[
 						Export-Package: org.eclipse.microprofile.config.*
-						Import-Package: \
-							!net.bytebuddy.*,\
-							!org.osgi.annotation.bundle.*,\
-							*
 						-includepackage: org.apache.geronimo.config.*
 						-cdiannotations:
 						-noclassforname: true
@@ -82,12 +78,20 @@
 				<configuration>
 					<transformations>
 						<transformation>
-							<plugin>org.apache.aries.cdi.extension.mp.config.BB</plugin>
+							<plugin>org.apache.aries.cdi.build.tools.AddExtensionRequirement</plugin>
 							<arguments>
 								<argument>
 									<index>1</index>
+									<value>eclipse.microprofile.config</value>
+								</argument>
+								<argument>
+									<index>2</index>
 									<value>${mp.config.version}</value>
 								</argument>
+								<argument>
+									<index>3</index>
+									<value>org.eclipse.microprofile.config.inject.ConfigProperty</value>
+								</argument>
 							</arguments>
 						</transformation>
 					</transformations>
@@ -98,6 +102,11 @@
 						<artifactId>osgi.annotation</artifactId>
 						<version>7.0.0</version>
 					</dependency>
+					<dependency>
+						<groupId>org.apache.aries.cdi</groupId>
+						<artifactId>org.apache.aries.cdi.build.tools</artifactId>
+						<version>${project.version}</version>
+					</dependency>
 				</dependencies>
 			</plugin>
 		</plugins>
diff --git a/cdi-extension-mp-config/src/main/java/org/apache/aries/cdi/extension/mp/config/BB.java b/cdi-extension-mp-config/src/main/java/org/apache/aries/cdi/extension/mp/config/BB.java
deleted file mode 100644
index 1de5285..0000000
--- a/cdi-extension-mp-config/src/main/java/org/apache/aries/cdi/extension/mp/config/BB.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/**
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.aries.cdi.extension.mp.config;
-
-import java.io.IOException;
-
-import org.osgi.annotation.bundle.Requirement;
-import org.osgi.service.cdi.CDIConstants;
-
-import net.bytebuddy.build.BuildLogger;
-import net.bytebuddy.build.Plugin;
-import net.bytebuddy.description.annotation.AnnotationDescription;
-import net.bytebuddy.description.type.TypeDescription;
-import net.bytebuddy.dynamic.ClassFileLocator;
-import net.bytebuddy.dynamic.DynamicType.Builder;
-
-public class BB implements Plugin {
-
-	private final BuildLogger buildLogger;
-	private final String mpVersion;
-
-	public BB(BuildLogger buildLogger, String mpVersion) {
-		this.buildLogger = buildLogger;
-		this.mpVersion = mpVersion;
-	}
-
-	@Override
-	public boolean matches(TypeDescription target) {
-		return target.getName().equals("org.eclipse.microprofile.config.inject.ConfigProperty");
-	}
-
-	@Override
-	public Builder<?> apply(Builder<?> builder, TypeDescription typeDescription, ClassFileLocator cfl) {
-		buildLogger.info("Processing class: " + typeDescription.getActualName());
-
-		return builder.annotateType(
-			AnnotationDescription.Builder.ofType(Requirement.class)
-				.define("namespace", CDIConstants.CDI_EXTENSION_PROPERTY)
-				.define("name", "eclipse.microprofile.config")
-				.define("version", mpVersion)
-				.build());
-	}
-
-	@Override
-	public void close() throws IOException {
-	}
-
-}
diff --git a/pom.xml b/pom.xml
index f567cc4..e5a375a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -36,6 +36,7 @@
 		<bnd.version>4.3.1</bnd.version>
 		<jsp.version>2.0</jsp.version>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<byte.buddy.version>1.10.3</byte.buddy.version>
 		<surefire.version>2.12</surefire.version>
 		<slf4j.version>1.7.28</slf4j.version>
 		<weld.version>3.0.5.Final</weld.version>
@@ -57,6 +58,7 @@
 	</scm>
 
 	<modules>
+		<module>cdi-build-tools</module>
 		<module>cdi-extra</module>
 		<module>cdi-spi</module>
 		<module>cdi-extender</module>