]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - demo/plugins/org.argeo.demo.i18n/src/main/java/org/argeo/demo/i18n/ImplementationLoader.java
f51fbb33c5d173b6403352fbfc22d1215bc05e9c
1 package org
.argeo
.demo
.i18n
;
3 import java
.text
.MessageFormat
;
6 * This class enable single sourcing between RAP and RCP. For this to run
7 * correctly, following conventions must be respected:
9 * <li>Given the fact that a common interface named Xxx is defined in package
10 * aa.bb.cc, corresponding implementation named XxxImpl must be found in package
11 * aa.bb.cc.specific of both RAP and RCP UI bundles.
13 * thanks to {@link http
14 * ://eclipsesource.com/en/info/rcp-rap-single-sourcing-guideline/}, chapter 7
17 public class ImplementationLoader
{
18 // private final static Log log = LogFactory
19 // .getLog(ImplementationLoader.class);
21 public static Object
newInstance(
22 @SuppressWarnings("rawtypes") final Class type
) {
23 String name
= type
.getName();
24 // manually construct the implementation name for the given interface,
25 // assuming that convention have been respected.
26 String cName
= type
.getCanonicalName();
27 String pName
= cName
.substring(0, cName
.lastIndexOf('.') + 1);
28 String sName
= cName
.substring(cName
.lastIndexOf('.') + 1);
29 String implName
= pName
+ "specific." + sName
+ "Impl";
30 // String implName = cName + "Impl";
33 result
= type
.getClassLoader().loadClass(implName
).newInstance();
34 } catch (Throwable throwable
) {
35 String txt
= "Could not load implementation for {0}";
36 String msg
= MessageFormat
.format(txt
, new Object
[] { name
});
37 throw new RuntimeException(msg
, throwable
);