Skip to content

Refactor tidy CoreAdapterBuilder #42

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

Merged
merged 3 commits into from
Dec 14, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package io.avaje.jsonb.generator;

import io.avaje.jsonb.Json;

import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

import io.avaje.jsonb.Json;

class BeanReader {

private final TypeElement beanType;
Expand Down Expand Up @@ -44,27 +42,22 @@ class BeanReader {
this.constructor = typeReader.constructor();
}

public BeanReader(
TypeElement beanType,
TypeElement mixInElement,
ProcessingContext context) {

this.beanType = beanType;
this.type = beanType.getQualifiedName().toString();
this.shortName = shortName(beanType);
final NamingConventionReader ncReader = new NamingConventionReader(beanType);
this.namingConvention = ncReader.get();
this.typeProperty = ncReader.typeProperty();
this.typeReader = new TypeReader(beanType, mixInElement, context, namingConvention);
typeReader.process();
this.nonAccessibleField = typeReader.nonAccessibleField();
this.hasSubTypes = typeReader.hasSubTypes();
this.allFields = typeReader.allFields();
this.constructor = typeReader.constructor();

public BeanReader(TypeElement beanType, TypeElement mixInElement, ProcessingContext context) {
this.beanType = beanType;
this.type = beanType.getQualifiedName().toString();
this.shortName = shortName(beanType);
final NamingConventionReader ncReader = new NamingConventionReader(beanType);
this.namingConvention = ncReader.get();
this.typeProperty = ncReader.typeProperty();
this.typeReader = new TypeReader(beanType, mixInElement, context, namingConvention);
typeReader.process();
this.nonAccessibleField = typeReader.nonAccessibleField();
this.hasSubTypes = typeReader.hasSubTypes();
this.allFields = typeReader.allFields();
this.constructor = typeReader.constructor();
}

@Override
@Override
public String toString() {
return beanType.toString();
}
Expand Down Expand Up @@ -198,6 +191,7 @@ void writeViewSupport(Append writer) {
writeViewBuild(writer);
}
}

private void writeView(Append writer) {
writer.eol();
writer.append(" @Override").eol();
Expand Down Expand Up @@ -375,4 +369,5 @@ private void writeFromJsonSwitch(Append writer, boolean defaultConstructor, Stri
writer.append(" }").eol();
writer.append(" reader.endObject();").eol();
}

}
22 changes: 0 additions & 22 deletions jsonb/src/main/java/io/avaje/jsonb/core/CollectionAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,6 @@ abstract class CollectionAdapter<C extends Collection<T>, T> extends JsonAdapter
return null;
};

/**
* Helper that takes a base JsonAdapter and returns another JsonAdapter for a List
* containing elements of the base type.
*/
static <T> JsonAdapter<List<T>> listOf(JsonAdapter<T> base) {
return new CollectionAdapter<List<T>, T>(base) {
@Override
List<T> newCollection() {
return new ArrayList<>();
}
};
}

static <T> JsonAdapter<Set<T>> setOf(JsonAdapter<T> base) {
return new CollectionAdapter<Set<T>, T>(base) {
@Override
Set<T> newCollection() {
return new LinkedHashSet<>();
}
};
}

static <T> JsonAdapter<Collection<T>> newListAdapter(Type type, Jsonb jsonb) {
Type elementType = Util.collectionElementType(type);
JsonAdapter<T> elementAdapter = jsonb.adapter(elementType);
Expand Down
14 changes: 3 additions & 11 deletions jsonb/src/main/java/io/avaje/jsonb/core/CoreAdapterBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

/**
Expand All @@ -31,7 +32,7 @@ class CoreAdapterBuilder {
private final DJsonb context;
private final List<JsonAdapter.Factory> factories;
private final ThreadLocal<LookupChain> lookupChainThreadLocal = new ThreadLocal<>();
private final Map<Object, JsonAdapter<?>> adapterCache = new LinkedHashMap<>();
private final Map<Object, JsonAdapter<?>> adapterCache = new ConcurrentHashMap<>();
private final ReentrantLock lock = new ReentrantLock();

CoreAdapterBuilder(DJsonb context, List<JsonAdapter.Factory> userFactories, boolean mathAsString) {
Expand All @@ -51,16 +52,7 @@ class CoreAdapterBuilder {
*/
@SuppressWarnings("unchecked")
<T> JsonAdapter<T> get(Object cacheKey) {
lock.lock();
try {
JsonAdapter<?> result = adapterCache.get(cacheKey);
if (result != null) {
return (JsonAdapter<T>) result;
}
} finally {
lock.unlock();
}
return null;
return (JsonAdapter<T>) adapterCache.get(cacheKey);
}

/**
Expand Down