public ClassMap<A,B> copyWithUsedMappers(Set<MapperKey> usedMappers) { return new ClassMap<A,B>(aType, bType, fieldsMapping, customizedMapper, usedMappers, constructorA, constructorB, sourcesMappedOnNull, destinationsMappedOnNull, favorsExtension); }
@SuppressWarnings({ "unchecked" }) private <S, D> void buildObjectFactories(ClassMap<S, D> classMap, MappingContext context) { Type<?> aType = classMap.getAType(); Type<?> bType = classMap.getBType(); if (classMap.getConstructorA() != null && lookupExistingObjectFactory(aType, TypeFactory.TYPE_OF_OBJECT, context) == null) { GeneratedObjectFactory objectFactory = objectFactoryGenerator.build(aType, bType, context); registerObjectFactory(objectFactory, (Type<Object>) aType); } if (classMap.getConstructorB() != null && lookupExistingObjectFactory(bType, TypeFactory.TYPE_OF_OBJECT, context) == null) { GeneratedObjectFactory objectFactory = objectFactoryGenerator.build(bType, aType, context); registerObjectFactory(objectFactory, (Type<Object>) bType); } }
String className = "Orika_" + getBTypeName() + "_" + getATypeName() + "_Mapper"; boolean aIsPublic = Modifier.isPublic(getAType().getRawType().getModifiers()); boolean bIsPublic = Modifier.isPublic(getBType().getRawType().getModifiers()); } else { return prependPackageName(getPackageName(getBType()), className); return prependPackageName(getPackageName(getAType()), className); } else { String aPackage = getPackageName(getAType()); if (aPackage.equals(getPackageName(getBType()))) { return prependPackageName(aPackage, className); } else { throw new RuntimeException(getAType() + " and " + getBType() + " are both package private but are in different packages");
/** * @return */ public String getMapperClassName() { return "Orika_" + getBTypeName() + "_" + getATypeName() + "_Mapper"; }
if (logDetails != null) { if (aToB) { logDetails.append("\n\t" + code.getClassSimpleName() + ".mapAToB(" + classMap.getAType() + ", " + classMap.getBTypeName() + ") {"); } else { logDetails.append("\n\t" + code.getClassSimpleName() + ".mapBToA(" + classMap.getBType() + ", " + classMap.getATypeName() + ") {"); VariableRef destination; if (aToB) { source = new VariableRef(classMap.getAType(), "source"); destination = new VariableRef(classMap.getBType(), "destination"); } else { source = new VariableRef(classMap.getBType(), "source"); destination = new VariableRef(classMap.getAType(), "destination"); "// destinationType: " + destination.type() + destination.declare("b"), "\n\n"); for (FieldMap currentFieldMap : classMap.getFieldsMapping()) {
private boolean isAlreadyExistsInUsedMappers(FieldMap fieldMap, ClassMap<?, ?> classMap) { Set<ClassMap<Object, Object>> usedClassMapSet = mapperFactory.lookupUsedClassMap(new MapperKey(classMap.getAType(), classMap.getBType())); if (!fieldMap.isByDefault()) { return false; } for (ClassMap<Object, Object> usedClassMap : usedClassMapSet) { for (FieldMap usedFieldMap : usedClassMap.getFieldsMapping()) { if (usedFieldMap.getSource().equals(fieldMap.getSource()) && usedFieldMap.getDestination().equals(fieldMap.getDestination())) { return true; } } } return false; }
private void buildClassMapRegistry() { // prepare a map for classmap (stored as set) Map<MapperKey, ClassMap<Object, Object>> classMapsDictionary = new HashMap<MapperKey, ClassMap<Object, Object>>(); Set<ClassMap<Object, Object>> classMaps = new HashSet<ClassMap<Object, Object>>(classMapRegistry.values()); for (final ClassMap<Object, Object> classMap : classMaps) { classMapsDictionary.put(new MapperKey(classMap.getAType(), classMap.getBType()), classMap); } for (final ClassMap<?, ?> classMap : classMaps) { MapperKey key = new MapperKey(classMap.getAType(), classMap.getBType()); Set<ClassMap<Object, Object>> usedClassMapSet = new HashSet<ClassMap<Object, Object>>(); for (final MapperKey parentMapperKey : classMap.getUsedMappers()) { ClassMap<Object, Object> usedClassMap = classMapsDictionary.get(parentMapperKey); if (usedClassMap == null) { throw new MappingException("Cannot find class mapping using mapper : " + classMap.getMapperClassName()); } usedClassMapSet.add(usedClassMap); } usedMapperMetadataRegistry.put(key, usedClassMapSet); } }
compilerStrategy.assureTypeIsAccessible(classMap.getAType().getRawType()); compilerStrategy.assureTypeIsAccessible(classMap.getBType().getRawType()); String srcName = TypeFactory.nameOf(classMap.getAType(), classMap.getBType()); String dstName = TypeFactory.nameOf(classMap.getBType(), classMap.getAType()); logDetails.append("Generating new mapper for (" + srcName + ", " + dstName + ")"); final SourceCodeContext mapperCode = new SourceCodeContext(classMap.getMapperClassName(), GeneratedMapperBase.class, context, logDetails); instance.setAType(classMap.getAType()); instance.setBType(classMap.getBType()); instance.setFavorsExtension(classMap.favorsExtension()); classMap = classMap.copy(mappedFields); context.registerMapperGeneration(classMap);
public GeneratedMapperBase build(ClassMap<?, ?> classMap, MappingContext context) { compilerStrategy.assureTypeIsAccessible(classMap.getAType().getRawType()); compilerStrategy.assureTypeIsAccessible(classMap.getBType().getRawType()); if (LOGGER.isDebugEnabled()) { logDetails = new StringBuilder(); logDetails.append("Generating new mapper for (" + classMap.getAType()+", " + classMap.getBTypeName() +")"); } else { logDetails = null; classMap.getMapperClassName(), GeneratedMapperBase.class, context, logDetails); instance.setAType(classMap.getAType()); instance.setBType(classMap.getBType()); classMap = classMap.copy(mappedFields); context.registerMapperGeneration(classMap);
@SuppressWarnings("unchecked") public synchronized <A, B> void registerClassMap(ClassMap<A, B> classMap) { classMapRegistry.put(new MapperKey(classMap.getAType(), classMap.getBType()), (ClassMap<Object, Object>) classMap); if (isBuilding || isBuilt) { MappingContext context = contextFactory.getContext(); try { if (classMap.getUsedMappers().isEmpty()) { classMap = classMap.copyWithUsedMappers(discoverUsedMappers(classMap)); } GeneratedMapperBase generatedMapper = buildMapper(classMap, /** isAutoGenerated == **/ isBuilding, context); buildObjectFactories(classMap, context); initializeUsedMappers(generatedMapper, classMap, context); mapperFacade.factoryModified(this); } finally { contextFactory.release(context); } } }
private void collectUsedMappers(ClassMap<?, ?> classMap, List<Mapper<Object, Object>> parentMappers, MapperKey parentMapperKey) { Mapper<Object, Object> parentMapper = lookupMapper(parentMapperKey); if (parentMapper == null) { throw new MappingException("Cannot find used mappers for : " + classMap.getMapperClassName()); } parentMappers.add(parentMapper); Set<ClassMap<Object, Object>> usedClassMapSet = usedMapperMetadataRegistry.get(parentMapperKey); if (usedClassMapSet != null) { for (ClassMap<Object, Object> cm : usedClassMapSet) { collectUsedMappers(cm, parentMappers, new MapperKey(cm.getAType(), cm.getBType())); } } }
@SuppressWarnings("unchecked") private GeneratedMapperBase buildMapper(ClassMap<?, ?> classMap, boolean isAutoGenerated, MappingContext context) { register(classMap.getAType(), classMap.getBType()); register(classMap.getBType(), classMap.getAType()); final MapperKey mapperKey = new MapperKey(classMap.getAType(), classMap.getBType()); final GeneratedMapperBase mapper = this.mapperGenerator.build(classMap, context); mapper.setMapperFacade(mapperFacade); mapper.setFromAutoMapping(isAutoGenerated); if (classMap.getCustomizedMapper() != null) { final Mapper<Object, Object> customizedMapper = (Mapper<Object, Object>) classMap.getCustomizedMapper(); mapper.setCustomMapper(customizedMapper); } mappersRegistry.add(mapper); classMapRegistry.put(mapperKey, (ClassMap<Object, Object>) classMap); return mapper; }
@SuppressWarnings("unchecked") private void initializeUsedMappers(ClassMap<?, ?> classMap) { Mapper<Object, Object> mapper = lookupMapper(new MapperKey(classMap.getAType(), classMap.getBType())); if (!classMap.getUsedMappers().isEmpty()) { for (MapperKey parentMapperKey : classMap.getUsedMappers()) { collectUsedMappers(classMap, parentMappers, parentMapperKey); if (!key.getAType().equals(classMap.getAType()) || !key.getBType().equals(classMap.getBType())) { if (key.getAType().isAssignableFrom(classMap.getAType()) && key.getBType().isAssignableFrom(classMap.getBType())) { usedMappers.add(key); for (int i = 0; i < usedMappers.length; ++i) { Mapper<Object, Object> usedMapper = usedMappers[i]; if (usedMapper.getAType().isAssignableFrom(classMap.getBType()) && usedMapper.getBType().isAssignableFrom(classMap.getAType())) { usedMappers[i] = ReversedMapper.reverse(usedMapper);
@Test public void testClassMapBuilderExtension() { MapperFactory factory = new DefaultMapperFactory.Builder().classMapBuilderFactory(new ScoringClassMapBuilder.Factory()).build(); ClassMap<Source, Destination> map = factory.classMap(Source.class, Destination.class).byDefault().toClassMap(); Map<String, String> mapping = new HashMap<String, String>(); for (FieldMap f : map.getFieldsMapping()) { mapping.put(f.getSource().getExpression(), f.getDestination().getExpression()); } /* * Check that properties we expect were mapped */ Assert.assertEquals("name.first", mapping.get("firstName")); Assert.assertEquals("name.last", mapping.get("lastName")); Assert.assertEquals("streetAddress", mapping.get("postalAddress.street")); Assert.assertEquals("countryCode", mapping.get("postalAddress.country.alphaCode")); Assert.assertEquals("currentAge", mapping.get("age")); Assert.assertEquals("birthState", mapping.get("stateOfBirth")); /* * Check that properties that we don't expect aren't mapped by accident */ Assert.assertFalse(mapping.containsKey("driversLicenseNumber")); Assert.assertFalse(mapping.containsKey("eyeColor")); }
if (classMap.getUsedMappers().isEmpty()) { classMapEntry.setValue(classMap.copyWithUsedMappers(discoverUsedMappers(classMap)));
if (logDetails != null) { if (aToB) { logDetails.append("\n\t" +code.getClassSimpleName() + ".mapAToB("+ classMap.getAType()+", " + classMap.getBTypeName() +") {"); } else { logDetails.append("\n\t" +code.getClassSimpleName() + ".mapBToA("+ classMap.getBType()+", " + classMap.getATypeName() +") {"); VariableRef destination; if (aToB) { source = new VariableRef(classMap.getAType(), "source"); destination = new VariableRef(classMap.getBType(), "destination"); } else { source = new VariableRef(classMap.getBType(), "source"); destination = new VariableRef(classMap.getAType(), "destination"); "\n\n"); for (FieldMap currentFieldMap : classMap.getFieldsMapping()) {
private boolean isAlreadyExistsInUsedMappers(FieldMap fieldMap, ClassMap<?, ?> classMap) { Set<ClassMap<Object, Object>> usedClassMapSet = mapperFactory.lookupUsedClassMap(new MapperKey(classMap.getAType(), classMap.getBType())); if (!fieldMap.isByDefault()) { return false; } for (ClassMap<Object, Object> usedClassMap : usedClassMapSet) { for(FieldMap usedFieldMap: usedClassMap.getFieldsMapping()) { if (usedFieldMap.getSource().equals(fieldMap.getSource()) && usedFieldMap.getDestination().equals(fieldMap.getDestination())) { return true; } } } return false; }
/** * Builds up metadata regarding which classmaps are used by others. */ private void buildClassMapRegistry() { // prepare a map for classmap (stored as set) Map<MapperKey, ClassMap<Object, Object>> classMapsDictionary = new HashMap<MapperKey, ClassMap<Object, Object>>(); for (final ClassMap<Object, Object> classMap : classMapRegistry.values()) { classMapsDictionary.put(new MapperKey(classMap.getAType(), classMap.getBType()), classMap); } for (final ClassMap<?, ?> classMap : classMapRegistry.values()) { MapperKey key = new MapperKey(classMap.getAType(), classMap.getBType()); Set<ClassMap<Object, Object>> usedClassMapSet = new LinkedHashSet<ClassMap<Object, Object>>(); for (final MapperKey parentMapperKey : classMap.getUsedMappers()) { ClassMap<Object, Object> usedClassMap = classMapsDictionary.get(parentMapperKey); if (usedClassMap == null) { throw exceptionUtil.newMappingException("Cannot find class mapping using mapper : " + classMap.getMapperClassName()); } usedClassMapSet.add(usedClassMap); } usedMapperMetadataRegistry.put(key, usedClassMapSet); } }
private Set<MapperKey> discoverUsedMappers(MappedTypePair<?, ?> classMapBuilder) { Set<MapperKey> mappers = new LinkedHashSet<MapperKey>(); /* * Attempt to auto-determine used mappers for this classmap; however, we * should only add the most-specific of the available mappers to avoid * calling the same mapper multiple times during a single map request; */ for (ClassMap<?, ?> map : classMapRegistry.values()) { if (map.getAType().isAssignableFrom(classMapBuilder.getAType()) && map.getBType().isAssignableFrom(classMapBuilder.getBType())) { if (!map.getAType().equals(classMapBuilder.getAType()) || !map.getBType().equals(classMapBuilder.getBType())) { MapperKey key = new MapperKey(map.getAType(), map.getBType()); mappers.add(key); } } else if (map.getAType().isAssignableFrom(classMapBuilder.getBType()) && map.getBType().isAssignableFrom(classMapBuilder.getAType())) { if (!map.getAType().equals(classMapBuilder.getBType()) || !map.getBType().equals(classMapBuilder.getAType())) { MapperKey key = new MapperKey(map.getBType(), map.getAType()); mappers.add(key); } } } return mappers; }