Adds DI and SPI examples

This commit is contained in:
2020-03-15 18:59:33 +01:00
parent 9f120b6317
commit e407698e84
20 changed files with 302 additions and 3 deletions

11
di/pom.xml Normal file
View 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>

View 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 {
}

View 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);
}
});
}
}

View 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);
}
}

View File

@ -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 {
}

View 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);
}
});
}
}

View 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;
}
}

View 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>