Skip to content

Commit 6bd82dd

Browse files
committed
Apply most changes from #190 Sonar Fixes
1 parent fc0eeed commit 6bd82dd

File tree

5 files changed

+20
-31
lines changed

5 files changed

+20
-31
lines changed

jsonb/src/main/java/io/avaje/jsonb/core/ViewBuilder.java renamed to jsonb/src/main/java/io/avaje/jsonb/core/CoreViewBuilder.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.avaje.jsonb.spi.BufferedJsonWriter;
55
import io.avaje.jsonb.spi.BytesJsonWriter;
66
import io.avaje.jsonb.spi.PropertyNames;
7+
import io.avaje.jsonb.spi.ViewBuilder;
78
import io.avaje.jsonb.stream.JsonOutput;
89

910
import java.io.IOException;
@@ -13,26 +14,23 @@
1314
import java.lang.invoke.MethodHandles;
1415
import java.lang.invoke.MethodType;
1516
import java.lang.reflect.Field;
16-
import java.util.ArrayList;
17-
import java.util.Collection;
18-
import java.util.List;
19-
import java.util.Stack;
17+
import java.util.*;
2018

21-
final class ViewBuilder implements io.avaje.jsonb.spi.ViewBuilder {
19+
final class CoreViewBuilder implements ViewBuilder {
2220

2321
private final MethodHandles.Lookup lookup = MethodHandles.lookup();
24-
private final Stack<Items> stack = new Stack<>();
22+
private final Deque<Items> stack = new ArrayDeque<>();
2523
private final ViewDsl viewDsl;
2624
private final Names names;
2725
private Items current;
2826
private Element resultElement;
2927

30-
ViewBuilder(ViewDsl viewDsl) {
28+
CoreViewBuilder(ViewDsl viewDsl) {
3129
this.viewDsl = viewDsl;
3230
this.names = new Names();
3331
}
3432

35-
private ViewBuilder(ViewDsl viewDsl, Names names) {
33+
private CoreViewBuilder(ViewDsl viewDsl, Names names) {
3634
this.viewDsl = viewDsl;
3735
this.names = names;
3836
}
@@ -98,7 +96,7 @@ public void endObject() {
9896
@Override
9997
public void addArray(String name, JsonAdapter<?> adapter, MethodHandle methodHandle) {
10098
try {
101-
ViewBuilder nested = new ViewBuilder(viewDsl, names);
99+
CoreViewBuilder nested = new CoreViewBuilder(viewDsl, names);
102100
adapter.viewBuild().build(nested);
103101
JsonView<Object> nestedView = nested.build();
104102
if (name == null) {

jsonb/src/main/java/io/avaje/jsonb/core/DJsonb.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ final class DJsonb implements Jsonb {
3636
boolean failOnUnknown,
3737
boolean mathAsString,
3838
BufferRecycleStrategy strategy) {
39+
3940
this.builder = new CoreAdapterBuilder(this, factories, mathAsString);
4041
if (adapter != null) {
4142
this.io = adapter;
@@ -184,10 +185,10 @@ <T> JsonView<T> buildView(final String dsl, final JsonAdapter<T> adapter, final
184185
final ViewKey key = new ViewKey(dsl, type);
185186
return (JsonView<T>) viewCache.computeIfAbsent(key, o -> {
186187
try {
187-
ViewBuilder viewBuilder = new ViewBuilder(ViewDsl.parse(dsl));
188+
CoreViewBuilder viewBuilder = new CoreViewBuilder(ViewDsl.parse(dsl));
188189
adapter.viewBuild().build(viewBuilder);
189190
return viewBuilder.build(this);
190-
} catch (Throwable e) {
191+
} catch (Exception e) {
191192
throw new IllegalStateException(e);
192193
}
193194
});
@@ -316,7 +317,7 @@ static <T> JsonAdapter.Factory newAdapterFactory(Type type, JsonAdapter<T> jsonA
316317
return (targetType, jsonb) -> simpleMatch(type, targetType) ? jsonAdapter : null;
317318
}
318319

319-
static <T> JsonAdapter.Factory newAdapterFactory(Type type, AdapterBuilder builder) {
320+
static JsonAdapter.Factory newAdapterFactory(Type type, AdapterBuilder builder) {
320321
requireNonNull(type);
321322
requireNonNull(builder);
322323
return (targetType, jsonb) -> simpleMatch(type, targetType) ? builder.build(jsonb).nullSafe() : null;

jsonb/src/main/java/io/avaje/jsonb/core/ObjectJsonReader.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public <T> T unwrap(Class<T> type) {
3131

3232
@Override
3333
public void unmappedField(String fieldName) {
34-
34+
// do nothing
3535
}
3636

3737
@Override
@@ -134,10 +134,7 @@ public String readString() {
134134

135135
@Override
136136
public String readRaw() {
137-
if (currentValue instanceof String) {
138-
return (String) currentValue;
139-
}
140-
return currentValue.toString();
137+
return readString();
141138
}
142139

143140
@Override

jsonb/src/main/java/io/avaje/jsonb/core/Util.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static Type removeSubtypeWildcard(Type type) {
111111
}
112112

113113
static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
114-
return resolve(context, contextRawType, toResolve, new LinkedHashSet<TypeVariable<?>>());
114+
return resolve(context, contextRawType, toResolve, new LinkedHashSet<>());
115115
}
116116

117117
private static Type resolve(
@@ -284,16 +284,10 @@ static final class ParameterizedTypeImpl implements ParameterizedType {
284284
private final Type rawType;
285285
public final Type[] typeArguments;
286286

287-
public ParameterizedTypeImpl(Type ownerType, Type rawType, Type... typeArguments) {
287+
ParameterizedTypeImpl(Type ownerType, Type rawType, Type... typeArguments) {
288288
// Require an owner type if the raw type needs it.
289289
if (ownerType != null && rawType instanceof Class<?>) {
290-
Class<?> enclosingClass = ((Class<?>) rawType).getEnclosingClass();
291-
if (enclosingClass == null || Util.rawType(ownerType) != enclosingClass) {
292-
throw new IllegalArgumentException("unexpected owner type for " + rawType + ": " + ownerType);
293-
294-
} else if (enclosingClass != null) {
295-
throw new IllegalArgumentException("unexpected owner type for " + rawType + ": null");
296-
}
290+
throw new IllegalArgumentException("unexpected owner type for " + rawType + ": " + ownerType);
297291
}
298292

299293
this.ownerType = ownerType == null ? null : canonicalize(ownerType);
@@ -386,9 +380,7 @@ static final class WildcardTypeImpl implements WildcardType {
386380
private final Type lowerBound;
387381

388382
WildcardTypeImpl(Type[] upperBounds, Type[] lowerBounds) {
389-
if (lowerBounds.length > 1) throw new IllegalArgumentException();
390-
if (upperBounds.length != 1) throw new IllegalArgumentException();
391-
383+
if (lowerBounds.length > 1 || upperBounds.length != 1) throw new IllegalArgumentException();
392384
if (lowerBounds.length == 1) {
393385
if (lowerBounds[0] == null) throw new NullPointerException();
394386
checkNotPrimitive(lowerBounds[0]);

jsonb/src/main/java/io/avaje/jsonb/core/ViewDsl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package io.avaje.jsonb.core;
22

3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
35
import java.util.Map;
46
import java.util.Set;
5-
import java.util.Stack;
67

78
final class ViewDsl {
89

9-
private final Stack<Entry> stack = new Stack<>();
10+
private final Deque<Entry> stack = new ArrayDeque<>();
1011
private Entry current;
1112

1213
static ViewDsl parse(String dsl) {

0 commit comments

Comments
 (0)