Skip to content

Commit

Permalink
Merge pull request #327 from Degubi/mappingctx_refactor
Browse files Browse the repository at this point in the history
Refactor MappingContext.java
  • Loading branch information
aguibert committed Sep 5, 2019
2 parents 09d7a24 + ba72ac0 commit 1dd96ea
Showing 1 changed file with 14 additions and 55 deletions.
69 changes: 14 additions & 55 deletions src/main/java/org/eclipse/yasson/internal/MappingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
Expand All @@ -34,31 +33,6 @@
* @author Roman Grigoriadi
*/
public class MappingContext {

private static class ParseClassModelFunction implements Function<Class, ClassModel> {

private ClassModel parentClassModel;

private ClassParser classParser;

private JsonbContext jsonbContext;

public ParseClassModelFunction(ClassModel parentClassModel, ClassParser classParser, JsonbContext jsonbContext) {
this.parentClassModel = parentClassModel;
this.classParser = classParser;
this.jsonbContext = jsonbContext;
}

@Override
public ClassModel apply(Class aClass) {
final JsonbAnnotatedElement<Class<?>> clsElement = jsonbContext.getAnnotationIntrospector().collectAnnotations(aClass);
final ClassCustomization customization = jsonbContext.getAnnotationIntrospector().introspectCustomization(clsElement);
final ClassModel newClassModel = new ClassModel(aClass, customization, parentClassModel, jsonbContext.getConfigProperties().getPropertyNamingStrategy());
classParser.parseProperties(newClassModel, clsElement);
return newClassModel;
}
}

private final JsonbContext jsonbContext;

private final ConcurrentHashMap<Class<?>, ClassModel> classes = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -90,48 +64,33 @@ public ClassModel getOrCreateClassModel(Class<?> clazz) {
if (classModel != null) {
return classModel;
}
final Deque<Class> newClassModels = new ArrayDeque<>();
for (Class classToParse = clazz; classToParse != Object.class; classToParse = classToParse.getSuperclass()) {

Deque<Class<?>> newClassModels = new ArrayDeque<>();
for (Class<?> classToParse = clazz; classToParse != Object.class; classToParse = classToParse.getSuperclass()) {
if (classToParse == null){
break;
}
newClassModels.push(classToParse);
}
if (clazz == Object.class) {
classes.computeIfAbsent(clazz, (c) -> new ClassModel(c, null, null, null));
return classes.get(clazz);
return classes.computeIfAbsent(clazz, (c) -> new ClassModel(c, null, null, null));
}

ClassModel parentClassModel = null;
while (!newClassModels.isEmpty()) {
Class toParse = newClassModels.pop();
parentClassModel = classes.computeIfAbsent(toParse, new ParseClassModelFunction(parentClassModel, classParser, jsonbContext));
Class<?> toParse = newClassModels.pop();
parentClassModel = classes.computeIfAbsent(toParse, createParseClassModelFunction(parentClassModel, classParser, jsonbContext));
}
return classes.get(clazz);
}

/**
* Provided class class model is returned first by iterator.
* Following class models are sorted by hierarchy from provided class up to the Object.class.
*
* @param clazz class to start iteration of class models from
* @return iterator of class models
*/
public Iterator<ClassModel> classModelIterator(final Class<?> clazz) {
return new Iterator<ClassModel>() {
private Class<?> next = clazz;

@Override
public boolean hasNext() {
return next != Object.class;
}

@Override
public ClassModel next() {
final ClassModel result = classes.get(next);
next = next.getSuperclass();
return result;
}
private static Function<Class<?>, ClassModel> createParseClassModelFunction(ClassModel parentClassModel, ClassParser classParser, JsonbContext jsonbContext){
return aClass -> {
JsonbAnnotatedElement<Class<?>> clsElement = jsonbContext.getAnnotationIntrospector().collectAnnotations(aClass);
ClassCustomization customization = jsonbContext.getAnnotationIntrospector().introspectCustomization(clsElement);
ClassModel newClassModel = new ClassModel(aClass, customization, parentClassModel, jsonbContext.getConfigProperties().getPropertyNamingStrategy());
classParser.parseProperties(newClassModel, clsElement);
return newClassModel;
};
}

Expand Down Expand Up @@ -164,4 +123,4 @@ public ContainerSerializerProvider getSerializerProvider(Class<?> clazz) {
public void addSerializerProvider(Class<?> clazz, ContainerSerializerProvider serializerProvider) {
serializers.putIfAbsent(clazz, serializerProvider);
}
}
}

0 comments on commit 1dd96ea

Please sign in to comment.