Skip to content

Instantly share code, notes, and snippets.

@elaatifi
Created October 10, 2014 22:41
Show Gist options
  • Save elaatifi/fcad368f54440e4643ca to your computer and use it in GitHub Desktop.
Save elaatifi/fcad368f54440e4643ca to your computer and use it in GitHub Desktop.
External configuration of non generic collection in Orika
orika.Ex$A.getCollection=java.lang.String
import java.lang.reflect.Method;
import java.util.Properties;
import ma.glasnost.orika.metadata.Type;
import ma.glasnost.orika.metadata.TypeFactory;
import ma.glasnost.orika.property.IntrospectorPropertyResolver;
public class CustomPropertyResolver extends IntrospectorPropertyResolver {
private Properties props;
public CustomPropertyResolver(String resource) {
this.props = new Properties();
try {
this.props.load(getClass().getResourceAsStream(resource));
} catch(Exception e) {
throw new RuntimeException("Can not load properties file : "+resource, e);
}
}
@Override
public Type<?> resolvePropertyType(Method readMethod, Class<?> rawType,
Class<?> owningType, Type<?> referenceType) {
Type<?> resolvedType = super
.resolvePropertyType(readMethod, rawType, owningType, referenceType);
String key = owningType.getName()+"."+readMethod.getName();
String overridedElementType = props.getProperty(key);
if(overridedElementType != null) {
try {
return TypeFactory.valueOf(rawType, Class.forName(overridedElementType));
} catch (ClassNotFoundException e) {
throw new RuntimeException("Can not find the class you provide", e);
}
}
return resolvedType;
}
}
import java.util.Arrays;
import java.util.List;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.ConfigurableMapper;
import ma.glasnost.orika.impl.DefaultMapperFactory.Builder;
public class Ex {
public static void main(String[] args) {
ConfigurableMapper mapper = new ConfigurableMapper() {
@Override
protected void configureFactoryBuilder(Builder factoryBuilder) {
factoryBuilder.propertyResolverStrategy(new CustomPropertyResolver("/orika/custom-types.properties"));
}
@Override
protected void configure(MapperFactory factory) {
factory.classMap(A.class, B.class).field("collection[0]", "number").register();
}
};
A a= new A();
a.collection = Arrays.asList("123", "221");
B b = mapper.map(a, B.class);
System.out.println(b);
}
public static class A {
private List collection;
public List getCollection() {
return collection;
}
public void setCollection(List collection) {
this.collection = collection;
}
}
public static class B {
public Integer number;
@Override
public String toString() {
return "B [number=" + number + "]";
}
}
}
@mukulmoudgil
Copy link

Hi can u explain how can we Use in java project , so that i create a single instance of the Orika and to be used by whole project .

for example i have multiple bean classes in the project and i want to create a single method as generic method , so that so it can be used by all . it should not be dependent upon any specific class. It should be genric

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment