Adds DI and SPI examples
This commit is contained in:
11
di/pom.xml
Normal file
11
di/pom.xml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>fr.gasser</groupId>
|
||||
<artifactId>java-cookbook</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>di</artifactId>
|
||||
</project>
|
11
di/src/main/java/ovh/gasser/di/InjectConstant.java
Normal file
11
di/src/main/java/ovh/gasser/di/InjectConstant.java
Normal file
@ -0,0 +1,11 @@
|
||||
package ovh.gasser.di;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.FIELD })
|
||||
public @interface InjectConstant {
|
||||
}
|
18
di/src/main/java/ovh/gasser/di/InjectionUtils.java
Normal file
18
di/src/main/java/ovh/gasser/di/InjectionUtils.java
Normal file
@ -0,0 +1,18 @@
|
||||
package ovh.gasser.di;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
public interface InjectionUtils {
|
||||
static void populateConstants(Class<?> clazz, Map<?, ?> values) {
|
||||
Arrays.stream(clazz.getDeclaredFields())
|
||||
.filter(field -> field.isAnnotationPresent(InjectConstant.class))
|
||||
.forEach(field -> {
|
||||
try {
|
||||
field.set(null, values.get(field.getName()));
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException("Unable to setup constant for field " + field.getName(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
17
di/src/main/java/ovh/gasser/di/TestClass.java
Normal file
17
di/src/main/java/ovh/gasser/di/TestClass.java
Normal file
@ -0,0 +1,17 @@
|
||||
package ovh.gasser.di;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class TestClass {
|
||||
@InjectConstant
|
||||
public static String TEST;
|
||||
|
||||
static {
|
||||
Map<String, String> values = Map.of("TEST", "Injected value for TEST");
|
||||
InjectionUtils.populateConstants(TestClass.class, values);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(TEST);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package ovh.gasser.translation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.FIELD })
|
||||
public @interface InjectTranslation {
|
||||
}
|
50
di/src/main/java/ovh/gasser/translation/InjectionUtils.java
Normal file
50
di/src/main/java/ovh/gasser/translation/InjectionUtils.java
Normal file
@ -0,0 +1,50 @@
|
||||
package ovh.gasser.translation;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.xpath.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
final class InjectionUtils {
|
||||
private static final XPath xPath;
|
||||
private static final Document document;
|
||||
|
||||
static {
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
try {
|
||||
var resource = InjectionUtils.class.getResource("/translations.xml");
|
||||
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
|
||||
document = documentBuilder.parse(resource.getFile());
|
||||
XPathFactory xpf = XPathFactory.newInstance();
|
||||
xPath = xpf.newXPath();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Private ctor to prevent instantiation
|
||||
*/
|
||||
private InjectionUtils() {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
static void populateTranslations(Class<?> clazz, String language) {
|
||||
Arrays.stream(clazz.getDeclaredFields())
|
||||
.filter(field -> field.isAnnotationPresent(InjectTranslation.class))
|
||||
.forEach(field -> {
|
||||
try {
|
||||
String expression = String.format("/translations/string[@key='%s']/translation[@lang='%s']", field.getName(), language);
|
||||
XPathExpression xPathExpression = xPath.compile(expression);
|
||||
String s = (String) xPathExpression.evaluate(document, XPathConstants.STRING);
|
||||
field.set(null, s);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException("Unable to setup constant for field " + field.getName(), e);
|
||||
} catch (XPathExpressionException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
13
di/src/main/java/ovh/gasser/translation/Main.java
Normal file
13
di/src/main/java/ovh/gasser/translation/Main.java
Normal file
@ -0,0 +1,13 @@
|
||||
package ovh.gasser.translation;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
InjectionUtils.populateTranslations(TestClass.class, "DE");
|
||||
System.out.println(TestClass.HELLO);
|
||||
}
|
||||
|
||||
private static class TestClass {
|
||||
@InjectTranslation
|
||||
static String HELLO;
|
||||
}
|
||||
}
|
8
di/src/main/resources/translations.xml
Normal file
8
di/src/main/resources/translations.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<translations>
|
||||
<string key="HELLO">
|
||||
<translation lang="EN">Hello</translation>
|
||||
<translation lang="FR">Bonjour</translation>
|
||||
<translation lang="DE">Guten tag</translation>
|
||||
</string>
|
||||
</translations>
|
Reference in New Issue
Block a user