Skip to content

Commit

Permalink
Remove Guava. Build and tests now run on Java 6-11.
Browse files Browse the repository at this point in the history
  • Loading branch information
soberich committed Nov 20, 2019
1 parent ce6f3a8 commit f36641a
Show file tree
Hide file tree
Showing 23 changed files with 796 additions and 147 deletions.
8 changes: 3 additions & 5 deletions project.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ targetCompatibility = JavaVersion.VERSION_1_7; // defaults to sourceCompatibilit
*/
dependencies {
implementation(group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.9.9");
implementation(group: "com.google.guava", name: "guava", version: "28.1-android");
implementation(group: "com.github.java-json-tools", name: "msg-simple", version: "1.2-SNAPSHOT");
implementation(group: "com.google.code.findbugs", name: "jsr305", version: "2.0.1");
implementation(group: "com.google.code.findbugs", name: "jsr305", version: "3.0.2");
testImplementation(group: "org.testng", name: "testng", version: "6.8.7") {
exclude(group: "junit", module: "junit");
exclude(group: "org.beanshell", module: "bsh");
Expand All @@ -49,9 +48,8 @@ javadoc {
addStringOption("-release", "7");
}
links("https://docs.oracle.com/javase/7/docs/api/");
links("https://www.javadoc.io/doc/com.google.code.findbugs/jsr305/3.0.1/");
links("https://fasterxml.github.io/jackson-databind/javadoc/2.2.0/");
links("https://www.javadoc.io/doc/com.google.guava/guava/28.1-android/");
links("https://www.javadoc.io/doc/com.google.code.findbugs/jsr305/3.0.2/");
links("https://fasterxml.github.io/jackson-databind/javadoc/2.9/");
links("https://java-json-tools.github.io/msg-simple/");
}
}
9 changes: 5 additions & 4 deletions src/main/java/com/github/fge/jackson/JacksonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.google.common.collect.Maps;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

Expand Down Expand Up @@ -94,11 +95,11 @@ public static JsonNodeFactory nodeFactory()
*/
public static Map<String, JsonNode> asMap(final JsonNode node)
{
final Map<String, JsonNode> ret = Maps.newHashMap();
if (!node.isObject())
return ret;
return Collections.emptyMap();

final Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
final Map<String, JsonNode> ret = new HashMap<>();

Map.Entry<String, JsonNode> entry;

Expand All @@ -107,7 +108,7 @@ public static Map<String, JsonNode> asMap(final JsonNode node)
ret.put(entry.getKey(), entry.getValue());
}

return ret;
return Collections.unmodifiableMap(ret);
}

/**
Expand Down
65 changes: 29 additions & 36 deletions src/main/java/com/github/fge/jackson/JsonLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
package com.github.fge.jackson;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Preconditions;
import com.google.common.io.Closer;

import javax.annotation.Nonnull;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -74,50 +71,44 @@ private JsonLoader()
public static JsonNode fromResource(@Nonnull final String resource)
throws IOException
{
Preconditions.checkNotNull(resource);
Preconditions.checkArgument(resource.startsWith("/"),
"resource path does not start with a '/'");
if (resource == null) {
throw new NullPointerException();
}
if (!resource.startsWith("/")) {
throw new IllegalArgumentException("resource path does not start with a '/'");
}
URL url;
url = JsonLoader.class.getResource(resource);
if (url == null) {
final ClassLoader classLoader = firstNonNull(
Thread.currentThread().getContextClassLoader(),
JsonLoader.class.getClassLoader()
);
final ClassLoader classLoader;
if (Thread.currentThread().getContextClassLoader() != null) {
classLoader = Thread.currentThread().getContextClassLoader();
} else if (JsonLoader.class.getClassLoader() != null) {
classLoader = JsonLoader.class.getClassLoader();
} else {
throw new NullPointerException();
}
final String s = INITIAL_SLASH.matcher(resource).replaceFirst("");
url = classLoader.getResource(s);
}
if (url == null)
throw new IOException("resource " + resource + " not found");

final Closer closer = Closer.create();
final JsonNode ret;
final InputStream in;
InputStream in = null;

try {
in = closer.register(url.openStream());
in = url.openStream();
ret = READER.fromInputStream(in);
} finally {
closer.close();
if (in != null) {
in.close();
}
}

return ret;
}

/**
* Returns the first non-null parameter.
*
* Implementation note: Avoids the Guava method of the same name, to mitigate 'Dependency Hell'.
* This can be replaced by {@code MoreObjects.firstNonNull} when moving to Guava >= 18.0
* (Tip: Guava 20 seems like a good choice if Java 6 support is still necessary.)
*
* @throws NullPointerException if both are null.
*/
private static ClassLoader firstNonNull(ClassLoader first, ClassLoader second)
{
return first != null ? first : Preconditions.checkNotNull(second);
}

/**
* Read a {@link JsonNode} from an URL.
*
Expand All @@ -141,15 +132,16 @@ public static JsonNode fromURL(final URL url)
public static JsonNode fromPath(final String path)
throws IOException
{
final Closer closer = Closer.create();
final JsonNode ret;
final FileInputStream in;
FileInputStream in = null;

try {
in = closer.register(new FileInputStream(path));
in = new FileInputStream(path);
ret = READER.fromInputStream(in);
} finally {
closer.close();
if (in != null) {
in.close();
}
}

return ret;
Expand All @@ -166,15 +158,16 @@ public static JsonNode fromPath(final String path)
public static JsonNode fromFile(final File file)
throws IOException
{
final Closer closer = Closer.create();
final JsonNode ret;
final FileInputStream in;
FileInputStream in = null;

try {
in = closer.register(new FileInputStream(file));
in = new FileInputStream(file);
ret = READER.fromInputStream(in);
} finally {
closer.close();
if (in != null) {
in.close();
}
}

return ret;
Expand Down
33 changes: 20 additions & 13 deletions src/main/java/com/github/fge/jackson/JsonNodeReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.github.fge.Builder;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.bundle.PropertiesBundle;
import com.google.common.io.Closer;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
Expand Down Expand Up @@ -93,16 +92,20 @@ public JsonNodeReader()
public JsonNode fromInputStream(final InputStream in)
throws IOException
{
final Closer closer = Closer.create();
final JsonParser parser;
final MappingIterator<JsonNode> iterator;
JsonParser parser = null;
MappingIterator<JsonNode> iterator = null;

try {
parser = closer.register(reader.getFactory().createParser(in));
parser = reader.getFactory().createParser(in);
iterator = reader.readValues(parser);
return readNode(closer.register(iterator));
return readNode(iterator);
} finally {
closer.close();
if (parser != null) {
parser.close();
}
if (iterator != null) {
iterator.close();
}
}
}

Expand All @@ -117,16 +120,20 @@ public JsonNode fromInputStream(final InputStream in)
public JsonNode fromReader(final Reader r)
throws IOException
{
final Closer closer = Closer.create();
final JsonParser parser;
final MappingIterator<JsonNode> iterator;
JsonParser parser = null;
MappingIterator<JsonNode> iterator = null;

try {
parser = closer.register(reader.getFactory().createParser(r));
parser = reader.getFactory().createParser(r);
iterator = reader.readValues(parser);
return readNode(closer.register(iterator));
return readNode(iterator);
} finally {
closer.close();
if (parser != null) {
parser.close();
}
if (iterator != null) {
iterator.close();
}
}
}

Expand Down
29 changes: 24 additions & 5 deletions src/main/java/com/github/fge/jackson/JsonNumEquals.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
package com.github.fge.jackson;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Equivalence;
import com.google.common.collect.Sets;
import com.github.fge.jackson.com.google.common.base.Equivalence;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
* An {@link Equivalence} strategy for JSON Schema equality
* An {@code com.google.common.base.Equivalence} like strategy for JSON Schema equality
*
* <p>{@link JsonNode} does a pretty good job of obeying the {@link
* Object#equals(Object) equals()}/{@link Object#hashCode() hashCode()}
Expand Down Expand Up @@ -183,13 +183,32 @@ private boolean objectEquals(final JsonNode a, final JsonNode b)
/*
* Grab the key set from the first node
*/
final Set<String> keys = Sets.newHashSet(a.fieldNames());
final Set<String> keys = new HashSet<>();
Iterator<String> iterator1 = a.fieldNames();
while (iterator1.hasNext()) {
final String next = iterator1.next();
if (next != null) {
keys.add(next);
} else {
throw new NullPointerException();
}
}

/*
* Grab the key set from the second node, and see if both sets are the
* same. If not, objects are not equal, no need to check for children.
*/
final Set<String> set = Sets.newHashSet(b.fieldNames());
final Set<String> set = new HashSet<>();
Iterator<String> iterator2 = b.fieldNames();
while (iterator2.hasNext()) {
final String next = iterator2.next();
if (next != null) {
set.add(next);
} else {
throw new NullPointerException();
}
}

if (!set.equals(keys))
return false;

Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/github/fge/jackson/NodeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.MissingNode;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;

import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ public enum NodeType
* #getNodeType(JsonNode)})
*/
private static final Map<JsonToken, NodeType> TOKEN_MAP
= new EnumMap<JsonToken, NodeType>(JsonToken.class);
= new EnumMap<>(JsonToken.class);

static {
TOKEN_MAP.put(JsonToken.START_ARRAY, ARRAY);
Expand All @@ -98,13 +98,13 @@ public enum NodeType
TOKEN_MAP.put(JsonToken.START_OBJECT, OBJECT);
TOKEN_MAP.put(JsonToken.VALUE_STRING, STRING);

final ImmutableMap.Builder<String, NodeType> builder
= ImmutableMap.builder();
final Map<String, NodeType> builder
= new HashMap<>();

for (final NodeType type: NodeType.values())
builder.put(type.name, type);

NAME_MAP = builder.build();
NAME_MAP = Collections.unmodifiableMap(builder);
}

NodeType(final String name)
Expand Down Expand Up @@ -140,8 +140,9 @@ public static NodeType getNodeType(final JsonNode node)
{
final JsonToken token = node.asToken();
final NodeType ret = TOKEN_MAP.get(token);

Preconditions.checkNotNull(ret, "unhandled token type " + token);
if (ret == null) {
throw new NullPointerException("unhandled token type " + token);
}

return ret;
}
Expand Down
Loading

0 comments on commit f36641a

Please sign in to comment.