Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performed refactoring techniques to make the code more clear #388

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Committing the refactoring changes for the project
  • Loading branch information
Rutvik Pareshbhai Patel committed Mar 25, 2022
commit e44c0a6d3e8565e0fdf64fe41095b0e8b77d9fda
15 changes: 8 additions & 7 deletions core/src/main/java/ma/glasnost/orika/MappingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class MappingContext {
protected List<Map<MapperKey, ClassMap<?, ?>>> mappersSeen;
protected Map<Object, Object> properties;
protected Map<Object, Object> globalProperties;
protected boolean isNew = true;
protected boolean isNewConcreteClass = true;
protected boolean containsCycle = true;
protected int depth;
protected Type<?> resolvedSourceType;
Expand Down Expand Up @@ -139,7 +139,7 @@ public int getDepth() {
*/
@SuppressWarnings("unchecked")
public <S, D> Type<? extends D> getConcreteClass(Type<S> sourceType, Type<D> destinationType) {
if (isNew) {
if (isNewConcreteClass) {
return null;
}
final Type<?> type = mapping.get(sourceType);
Expand All @@ -158,7 +158,7 @@ public <S, D> Type<? extends D> getConcreteClass(Type<S> sourceType, Type<D> des
*/
public void registerConcreteClass(Type<?> subjectClass, Type<?> concreteClass) {
mapping.put(subjectClass, concreteClass);
isNew = false;
isNewConcreteClass = false;
}

/**
Expand All @@ -181,7 +181,7 @@ public <S, D> void cacheMappedObject(S source, Type<Object> destinationType, D d
}
localCache.put(source, destination);

isNew = false;
isNewConcreteClass = false;
}
}

Expand All @@ -197,7 +197,7 @@ public <S, D> void cacheMappedObject(S source, Type<Object> destinationType, D d
@SuppressWarnings("unchecked")
public <D> D getMappedObject(Object source, Type<?> destinationType) {

if (isNew || !containsCycle) {
if (isNewConcreteClass || !containsCycle) {
return null;
}
Map<Object, Object> localCache = (Map<Object, Object>) typeCache.get(destinationType.getUniqueIndex());
Expand Down Expand Up @@ -261,7 +261,8 @@ public void beginMapping() {
* the destination object being mapped into
* @deprecated This variant exists for backwards compatibility only; if
* overriding, override
* {@link #beginMapping(Type, Object, Type, String, Object)}
* {@link #
* //beginMapping(Type, Object, Type, String, Object)}
* instead.
*/
@Deprecated
Expand Down Expand Up @@ -466,7 +467,7 @@ public void reset() {
resolvedSourceType = null;
resolvedDestinationType = null;
resolvedStrategy = null;
isNew = true;
isNewConcreteClass = true;
depth = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ protected static <T> List<T> asList(Iterable<T> iterable) {
}
return ts;
}

protected static List<Object> asList(Object[] iterable) {
return new ArrayList<>(Arrays.asList(iterable));
}


protected static List<Object> asList(byte[] iterable) {
ArrayList<Object> ts = new ArrayList<>();
for (Object i : iterable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

import ma.glasnost.orika.ObjectFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public abstract class GeneratedObjectFactory extends GeneratedObjectBase implements ObjectFactory<Object> {

protected static List<Object> asList(Object[] iterable) {
return new ArrayList<>(Arrays.asList(iterable));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ma.glasnost.orika.generated;
package ma.glasnost.orika.impl.generated;

/**
* This interface is used as a parameter during class generation in order to be able to avoid Illegal reflective access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public interface AggregateSpecification extends BaseSpecification {

void setMapperFactory(MapperFactory mapperFactory);


/**
* @param fieldMappings
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package ma.glasnost.orika.impl.generator;

import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.metadata.FieldMap;

/**
Expand All @@ -16,4 +16,7 @@ public interface BaseSpecification {
* @return true if this specification applies to the given MappedTypePair
*/
boolean appliesTo(FieldMap fieldMap);


void setMapperFactory(MapperFactory mapperFactory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,35 @@
* ObjectFactory capable of instantiating a given target type.
*/
public class ObjectFactoryGenerator {

private final static Logger LOGGER = LoggerFactory.getLogger(ObjectFactoryGenerator.class);

private final ConstructorResolverStrategy constructorResolverStrategy;
private final MapperFactory mapperFactory;
private final String nameSuffix;

/**
* Creates a new ObjectFactoryGenerator instance
*
*
* @param mapperFactory
* @param constructorResolverStrategy
* @param compilerStrategy
*/
public ObjectFactoryGenerator(MapperFactory mapperFactory, ConstructorResolverStrategy constructorResolverStrategy,
CompilerStrategy compilerStrategy) {
CompilerStrategy compilerStrategy) {
this.mapperFactory = mapperFactory;
this.nameSuffix = String.valueOf(System.nanoTime());
this.constructorResolverStrategy = constructorResolverStrategy;
}

/**
* @param type
* @param sourceType
* @param context
* @return an instance of the newly generated ObjectFactory
*/
public GeneratedObjectFactory build(Type<?> type, Type<?> sourceType, MappingContext context) {

String className = type.getSimpleName() + "_" + sourceType.getSimpleName() + "_ObjectFactory" + nameSuffix;
try {
StringBuilder logDetails;
Expand All @@ -89,22 +89,22 @@ public GeneratedObjectFactory build(Type<?> type, Type<?> sourceType, MappingCon
final SourceCodeContext factoryCode = new SourceCodeContext(className,
packageNeighbour,
GeneratedObjectFactory.class, context, logDetails);

UsedTypesContext usedTypes = new UsedTypesContext();
UsedConvertersContext usedConverters = new UsedConvertersContext();
UsedMapperFacadesContext usedMapperFacades = new UsedMapperFacadesContext();

addCreateMethod(factoryCode, usedTypes, usedConverters, usedMapperFacades, type, sourceType, context, logDetails);

GeneratedObjectFactory objectFactory = (GeneratedObjectFactory) factoryCode.getInstance();
objectFactory.setMapperFacade(mapperFactory.getMapperFacade());

if (logDetails != null) {
LOGGER.debug(logDetails.toString());
}

return objectFactory;

} catch (final Exception e) {
if (e instanceof MappingException) {
throw (MappingException) e;
Expand All @@ -120,21 +120,21 @@ private static String getPackageName(Type<?> type) {
}

private void addCreateMethod(SourceCodeContext code, UsedTypesContext usedTypes, UsedConvertersContext usedConverters,
UsedMapperFacadesContext usedMappers, Type<?> type, Type<?> sourceType, MappingContext mappingContext, StringBuilder logDetails) {
UsedMapperFacadesContext usedMappers, Type<?> type, Type<?> sourceType, MappingContext mappingContext, StringBuilder logDetails) {

final StringBuilder out = new StringBuilder();
out.append("public Object create(Object s, ").append(MappingContext.class.getCanonicalName()).append(" mappingContext) {");
out.append(format("if(s == null) throw new %s(\"source object must be not null\");",
IllegalArgumentException.class.getCanonicalName()));

out.append(addSourceClassConstructor(code, type, sourceType, mappingContext, logDetails));
out.append(addUnmatchedSourceHandler(code, type, sourceType, mappingContext, logDetails));

out.append("\n}");

code.addMethod(out.toString());
}

/**
* @param code
* @param destinationType
Expand All @@ -144,50 +144,50 @@ private void addCreateMethod(SourceCodeContext code, UsedTypesContext usedTypes,
* @return
*/
private String addSourceClassConstructor(SourceCodeContext code, Type<?> destinationType, Type<?> sourceType,
MappingContext mappingContext, StringBuilder logDetails) {
MappingContext mappingContext, StringBuilder logDetails) {

MapperKey mapperKey = new MapperKey(sourceType, destinationType);
ClassMap<Object, Object> classMap = mapperFactory.getClassMap(mapperKey);

if (classMap == null) {
classMap = mapperFactory.getClassMap(new MapperKey(destinationType, sourceType));
}

StringBuilder out = new StringBuilder();
if (classMap != null) {
if (destinationType.isArray()) {
out.append(addArrayClassConstructor(code, destinationType, sourceType, classMap.getFieldsMapping().size()));
} else {

out.append(format("if (s instanceof %s) {", sourceType.getCanonicalName()));
out.append(format("%s source = (%s) s;", sourceType.getCanonicalName(), sourceType.getCanonicalName()));
out.append("\ntry {\n");

ConstructorMapping<?> constructorMapping = (ConstructorMapping<?>) constructorResolverStrategy.resolve(classMap,
destinationType);
Constructor<?> constructor = constructorMapping.getConstructor();

if (constructor == null) {
throw new IllegalArgumentException("no suitable constructors found for " + destinationType);
} else if (logDetails != null) {
logDetails.append("\n\tUsing constructor: ").append(constructor);
}

List<FieldMap> properties = constructorMapping.getMappedFields();
Type<?>[] constructorArguments = constructorMapping.getParameterTypes();

if (constructorArguments == null || properties.size() != constructorArguments.length) {
throw new MappingException("While attempting to generate ObjectFactory using constructor '" + constructor
+ "', an automatic mapping of the source type ('" + sourceType
+ "') to this constructor call could not be determined. Please "
+ "register a custom ObjectFactory implementation which is able to create an instance of '" + destinationType
+ "' from an instance of '" + sourceType + "'.");
}

int argIndex = 0;

argIndex = 0;

for (FieldMap fieldMap : properties) {
VariableRef v = new VariableRef(constructorArguments[argIndex], "arg" + argIndex++);
VariableRef s = new VariableRef(fieldMap.getSource(), "source");
Expand All @@ -196,7 +196,7 @@ private String addSourceClassConstructor(SourceCodeContext code, Type<?> destina
out.append(statement(v.declare()));
out.append(code.mapFields(fieldMap, s, v));
}

out.append(format("return new %s(", destinationType.getCanonicalName()));
for (int i = 0; i < properties.size(); i++) {
out.append(format("arg%d", i));
Expand All @@ -216,27 +216,27 @@ private String addSourceClassConstructor(SourceCodeContext code, Type<?> destina
}
return out.toString();
}

/**
* Adds a default constructor call (where possible) as fail-over case when
* no specific source type has been matched.
*
*
* @param code
* @param type
* @param mappingContext
* @param logDetails
* @return
*/
private String addUnmatchedSourceHandler(SourceCodeContext code, Type<?> type, Type<?> sourceType, MappingContext mappingContext,
StringBuilder logDetails) {
StringBuilder logDetails) {
StringBuilder out = new StringBuilder();
for (Constructor<?> constructor : type.getRawType().getConstructors()) {
if (constructor.getParameterTypes().length == 0 && Modifier.isPublic(constructor.getModifiers())) {
out.append(format("return new %s();", type.getCanonicalName()));
break;
}
}

/*
* If no default constructor field exists, attempt to locate and call a
* constructor which takes a single argument of source type
Expand All @@ -252,17 +252,17 @@ private String addUnmatchedSourceHandler(SourceCodeContext code, Type<?> type, T
}
}
}

if (out.length() == 0) {

out.append(format(
"throw new %s(s.getClass().getCanonicalName() + \" is an unsupported source class for constructing instances of "
+ type.getCanonicalName() + "\");", IllegalArgumentException.class.getCanonicalName()));
}

return out.toString();
}

/**
* @param type
* @param size
Expand All @@ -271,4 +271,4 @@ private String addArrayClassConstructor(SourceCodeContext code, Type<?> type, Ty
return format("if (s instanceof %s) {", sourceType.getCanonicalName()) + "return new "
+ type.getRawType().getComponentType().getCanonicalName() + "[" + size + "];" + "\n}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.Properties;
import ma.glasnost.orika.converter.ConverterFactory;
import ma.glasnost.orika.generated.GeneratedPackageClass;
import ma.glasnost.orika.impl.generated.GeneratedPackageClass;
import ma.glasnost.orika.impl.AggregateFilter;
import ma.glasnost.orika.impl.GeneratedObjectBase;
import ma.glasnost.orika.impl.generator.CompilerStrategy.SourceCodeGenerationException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public interface Specification extends BaseSpecification {

void setMapperFactory(MapperFactory mapperFactory);


/**
* Generates code for a boolean equality test between the two variable types,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static String capitalize(String string) {
* <p>Uncapitalizes a String, changing the first character to lower case as per {@link
* Character#toLowerCase(char)}. No other characters are changed.</p>
*
* <p>For a word based algorithm, see {@link org.apache.commons.lang3.text.WordUtils#uncapitalize(String)}.
* <p>For a word based algorithm, see {@link //org.apache.commons.lang3.text.WordUtils#uncapitalize(String)}.
* A {@code null} input String returns {@code null}.</p>
*
* <pre>
Expand All @@ -55,7 +55,7 @@ public static String capitalize(String string) {
*
* @param str the String to uncapitalize, may be null
* @return the uncapitalized String, {@code null} if null String input
* @see org.apache.commons.lang3.text.WordUtils#uncapitalize(String)
* @see //org.apache.commons.lang3.text.WordUtils#uncapitalize(String)
* @see #capitalize(String)
* @since 2.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ boolean casNext(Node<K,V> cmp, Node<K,V> val) {
* because callers will have already read value field and need
* to use that read (not another done here) and so directly
* test if value points to node.
* @param n a possibly null reference to a node
* @param
* //n a possibly null reference to a node
* @return true if this node is a marker node
*/
boolean isMarker() {
Expand Down
Loading