add json parsers and benchmarks
This commit is contained in:
parent
3a6dedfd46
commit
ace846e5ad
1107 changed files with 113765 additions and 101222 deletions
21
benchmark/build.gradle
Normal file
21
benchmark/build.gradle
Normal file
|
@ -0,0 +1,21 @@
|
|||
plugins {
|
||||
id "io.morethan.jmhreport" version "0.9.0"
|
||||
}
|
||||
|
||||
jmhReport {
|
||||
jmhResultPath = project.file('build/reports/jmh/result.json')
|
||||
jmhReportOutput = project.file('build/reports/jmh')
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation project(':datastructures-json')
|
||||
implementation project(':datastructures-json-dsl')
|
||||
implementation project(':datastructures-json-flat')
|
||||
implementation project(':datastructures-json-iterator')
|
||||
implementation project(':datastructures-json-minimal')
|
||||
implementation project(':datastructures-json-noggit')
|
||||
implementation project(':datastructures-json-simple')
|
||||
implementation "com.google.code.gson:gson:${project.property('gson.version')}"
|
||||
implementation "com.fasterxml.jackson.core:jackson-databind:${project.property('jackson.version')}"
|
||||
implementation "org.json:json:${project.property('orgjson.version')}"
|
||||
}
|
|
@ -0,0 +1,129 @@
|
|||
package org.xbib.datastructures.benchmark;
|
||||
|
||||
import com.dslplatform.json.DslJson;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Level;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.Threads;
|
||||
import org.openjdk.jmh.annotations.Timeout;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
import org.xbib.datastructures.json.EmptyJsonListener;
|
||||
import org.xbib.datastructures.json.StandardJsonListener;
|
||||
import org.xbib.datastructures.json.StringParser;
|
||||
import org.xbib.datastructures.json.TinyJsonListener;
|
||||
import org.xbib.datastructures.json.flat.Json;
|
||||
import org.xbib.datastructures.json.noggit.ObjectBuilder;
|
||||
import org.xbib.datastructures.json.simple.JSONParser;
|
||||
import org.xbib.datastructures.json.simple.ParseException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
@Warmup(iterations = 5)
|
||||
@Measurement(iterations = 5)
|
||||
@Fork(value = 1, jvmArgsAppend = "-Xmx1024m")
|
||||
@Threads(4)
|
||||
@Timeout(time = 10, timeUnit = TimeUnit.MINUTES)
|
||||
public class JsonLargeBenchmark {
|
||||
|
||||
private String largeInput;
|
||||
|
||||
private Gson gson;
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private DslJson<?> dslJson;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setup() throws IOException {
|
||||
try (InputStream inputStream = JsonLargeBenchmark.class.getResourceAsStream("large.json")) {
|
||||
if (inputStream != null) {
|
||||
byte[] b = inputStream.readAllBytes();
|
||||
this.largeInput = new String(b, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
gson = new Gson();
|
||||
objectMapper = new ObjectMapper();
|
||||
dslJson = new DslJson<>();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object noggit() throws IOException {
|
||||
return ObjectBuilder.fromJSON(largeInput).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object simple() throws ParseException {
|
||||
return new JSONParser().parse(largeInput).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object orgjson() {
|
||||
return new org.json.JSONArray(largeInput).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object gson() {
|
||||
return gson.fromJson(largeInput, List.class).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object jackson() throws IOException {
|
||||
return objectMapper.readValue(largeInput, List.class).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object minimal() throws IOException {
|
||||
return org.xbib.datastructures.json.minimal.Json.parse(largeInput).asArray().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object flat() throws IOException {
|
||||
return Json.parse(largeInput).asList().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object jsoniter() {
|
||||
return org.xbib.datastructures.json.iterator.JsonIterator.deserialize(largeInput).asList().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object jsondsl() throws IOException {
|
||||
byte[] b = largeInput.getBytes(StandardCharsets.UTF_8);
|
||||
return dslJson.deserialize(Map.class, b, b.length);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object datastructuresEmpty() {
|
||||
StringParser stringParser = new StringParser(new EmptyJsonListener());
|
||||
stringParser.parse(largeInput);
|
||||
return stringParser.getNode(); // there is no object in get()
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object datastructuresTiny() {
|
||||
StringParser stringParser = new StringParser(new TinyJsonListener());
|
||||
stringParser.parse(largeInput);
|
||||
return stringParser.getNode().get().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object datastructuresStandard() {
|
||||
StringParser stringParser = new StringParser(new StandardJsonListener());
|
||||
stringParser.parse(largeInput);
|
||||
return stringParser.getNode().get().toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
package org.xbib.datastructures.benchmark;
|
||||
|
||||
import com.dslplatform.json.DslJson;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Level;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.Threads;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
import org.openjdk.jmh.annotations.Timeout;
|
||||
import org.xbib.datastructures.json.EmptyJsonListener;
|
||||
import org.xbib.datastructures.json.StandardJsonListener;
|
||||
import org.xbib.datastructures.json.StringParser;
|
||||
import org.xbib.datastructures.json.TinyJsonListener;
|
||||
import org.xbib.datastructures.json.flat.Json;
|
||||
import org.xbib.datastructures.json.noggit.ObjectBuilder;
|
||||
import org.xbib.datastructures.json.simple.JSONParser;
|
||||
import org.xbib.datastructures.json.simple.ParseException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
@Warmup(iterations = 5)
|
||||
@Measurement(iterations = 5)
|
||||
@Fork(value = 1, jvmArgsAppend = "-Xmx512m")
|
||||
@Threads(4)
|
||||
@Timeout(time = 10, timeUnit = TimeUnit.MINUTES)
|
||||
public class JsonMediumBenchmark {
|
||||
|
||||
private String mediumInput;
|
||||
|
||||
private Gson gson;
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private DslJson<?> dslJson;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setup() throws IOException {
|
||||
try (InputStream inputStream = JsonMediumBenchmark.class.getResourceAsStream("medium.json")) {
|
||||
if (inputStream != null) {
|
||||
byte[] b = inputStream.readAllBytes();
|
||||
this.mediumInput = new String(b, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
gson = new Gson();
|
||||
objectMapper = new ObjectMapper();
|
||||
dslJson = new DslJson<>();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object noggit() throws IOException {
|
||||
return ObjectBuilder.fromJSON(mediumInput).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object simple() throws ParseException {
|
||||
return new JSONParser().parse(mediumInput).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object orgjson() {
|
||||
return new org.json.JSONObject(mediumInput).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object gson() {
|
||||
return gson.fromJson(mediumInput, Map.class).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object jackson() throws IOException {
|
||||
return objectMapper.readValue(mediumInput, Map.class).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object minimal() throws IOException {
|
||||
return org.xbib.datastructures.json.minimal.Json.parse(mediumInput).asObject().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object flat() throws IOException {
|
||||
return Json.parse(mediumInput).asMap().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object jsoniter() {
|
||||
return org.xbib.datastructures.json.iterator.JsonIterator.deserialize(mediumInput).asMap().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object jsondsl() throws IOException {
|
||||
byte[] b = mediumInput.getBytes(StandardCharsets.UTF_8);
|
||||
return dslJson.deserialize(Map.class, b, b.length);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object datastructuresEmpty() {
|
||||
StringParser stringParser = new StringParser(new EmptyJsonListener());
|
||||
stringParser.parse(mediumInput);
|
||||
return stringParser.getNode(); // there is no object in get()
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object datastructuresTiny() {
|
||||
StringParser stringParser = new StringParser(new TinyJsonListener());
|
||||
stringParser.parse(mediumInput);
|
||||
return stringParser.getNode().get().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object datastructuresStandard() {
|
||||
StringParser stringParser = new StringParser(new StandardJsonListener());
|
||||
stringParser.parse(mediumInput);
|
||||
return stringParser.getNode().get().toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,129 @@
|
|||
package org.xbib.datastructures.benchmark;
|
||||
|
||||
import com.dslplatform.json.DslJson;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Level;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.Threads;
|
||||
import org.openjdk.jmh.annotations.Timeout;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
import org.xbib.datastructures.json.EmptyJsonListener;
|
||||
import org.xbib.datastructures.json.StandardJsonListener;
|
||||
import org.xbib.datastructures.json.StringParser;
|
||||
import org.xbib.datastructures.json.TinyJsonListener;
|
||||
import org.xbib.datastructures.json.flat.Json;
|
||||
import org.xbib.datastructures.json.noggit.ObjectBuilder;
|
||||
import org.xbib.datastructures.json.simple.JSONParser;
|
||||
import org.xbib.datastructures.json.simple.ParseException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
@BenchmarkMode(Mode.Throughput)
|
||||
@Warmup(iterations = 5)
|
||||
@Measurement(iterations = 5)
|
||||
@Fork(value = 1, jvmArgsAppend = "-Xmx256m")
|
||||
@Threads(4)
|
||||
@Timeout(time = 10, timeUnit = TimeUnit.MINUTES)
|
||||
public class JsonSmallBenchmark {
|
||||
|
||||
private String smallInput;
|
||||
|
||||
private Gson gson;
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private DslJson<?> dslJson;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setup() throws IOException {
|
||||
try (InputStream inputStream = JsonSmallBenchmark.class.getResourceAsStream("small.json")) {
|
||||
if (inputStream != null) {
|
||||
byte[] b = inputStream.readAllBytes();
|
||||
this.smallInput = new String(b, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
// reentrant
|
||||
gson = new Gson();
|
||||
objectMapper = new ObjectMapper();
|
||||
dslJson = new DslJson<>();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object noggit() throws IOException {
|
||||
return ObjectBuilder.fromJSON(smallInput).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object simple() throws ParseException {
|
||||
return new JSONParser().parse(smallInput).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object orgjson() {
|
||||
return new org.json.JSONObject(smallInput).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object gson() {
|
||||
return gson.fromJson(smallInput, Map.class).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object jackson() throws IOException {
|
||||
return objectMapper.readValue(smallInput, Map.class).toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object minimal() throws IOException {
|
||||
return org.xbib.datastructures.json.minimal.Json.parse(smallInput).asObject().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object flat() throws IOException {
|
||||
return Json.parse(smallInput).asMap().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object jsoniter() {
|
||||
return org.xbib.datastructures.json.iterator.JsonIterator.deserialize(smallInput).asMap().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object jsondsl() throws IOException {
|
||||
byte[] b = smallInput.getBytes(StandardCharsets.UTF_8);
|
||||
return dslJson.deserialize(Map.class, b, b.length);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object datastructuresEmpty() {
|
||||
StringParser stringParser = new StringParser(new EmptyJsonListener());
|
||||
stringParser.parse(smallInput);
|
||||
return stringParser.getNode(); // there is no object in get()
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object datastructuresTiny() {
|
||||
StringParser stringParser = new StringParser(new TinyJsonListener());
|
||||
stringParser.parse(smallInput);
|
||||
return stringParser.getNode().get().toString();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Object datastructuresStandard() {
|
||||
StringParser stringParser = new StringParser(new StandardJsonListener());
|
||||
stringParser.parse(smallInput);
|
||||
return stringParser.getNode().get().toString();
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,57 @@
|
|||
{
|
||||
"colors": [
|
||||
{
|
||||
"color": "black",
|
||||
"category": "hue",
|
||||
"type": "primary",
|
||||
"code": {
|
||||
"rgba": [255,255,255,1],
|
||||
"hex": "#000"
|
||||
}
|
||||
},
|
||||
{
|
||||
"color": "white",
|
||||
"category": "value",
|
||||
"code": {
|
||||
"rgba": [0,0,0,1],
|
||||
"hex": "#FFF"
|
||||
}
|
||||
},
|
||||
{
|
||||
"color": "red",
|
||||
"category": "hue",
|
||||
"type": "primary",
|
||||
"code": {
|
||||
"rgba": [255,0,0,1],
|
||||
"hex": "#FF0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"color": "blue",
|
||||
"category": "hue",
|
||||
"type": "primary",
|
||||
"code": {
|
||||
"rgba": [0,0,255,1],
|
||||
"hex": "#00F"
|
||||
}
|
||||
},
|
||||
{
|
||||
"color": "yellow",
|
||||
"category": "hue",
|
||||
"type": "primary",
|
||||
"code": {
|
||||
"rgba": [255,255,0,1],
|
||||
"hex": "#FF0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"color": "green",
|
||||
"category": "hue",
|
||||
"type": "secondary",
|
||||
"code": {
|
||||
"rgba": [0,255,0,1],
|
||||
"hex": "#0F0"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package org.xbib.datastructures.benchmark;
|
||||
|
||||
import com.dslplatform.json.DslJson;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.xbib.datastructures.json.flat.Json;
|
||||
import org.xbib.datastructures.json.noggit.ObjectBuilder;
|
||||
import org.xbib.datastructures.json.simple.JSONParser;
|
||||
import org.xbib.datastructures.json.simple.ParseException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JsonTest {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(JsonTest.class.getName());
|
||||
|
||||
private static String input;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() throws IOException {
|
||||
try (InputStream inputStream = JsonTest.class.getResourceAsStream("small.json")) {
|
||||
if (inputStream != null) {
|
||||
byte[] b = inputStream.readAllBytes();
|
||||
input = new String(b, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMap() throws IOException, ParseException {
|
||||
logger.log(Level.INFO, "noggit = " + ObjectBuilder.fromJSON(input).toString());
|
||||
logger.log(Level.INFO, "simple = " + new JSONParser().parse(input).toString()); // JSON
|
||||
logger.log(Level.INFO, "orgjson = " + new org.json.JSONObject(input).toString()); // JSON
|
||||
logger.log(Level.INFO, "gson = " + new Gson().fromJson(input, Map.class).toString());
|
||||
logger.log(Level.INFO, "jackson = " + new ObjectMapper().readValue(input, Map.class).toString());
|
||||
logger.log(Level.INFO, "minimal = " + org.xbib.datastructures.json.minimal.Json.parse(input).asObject().toString()); // JSON
|
||||
logger.log(Level.INFO, "flat = " + Json.parse(input).asMap().toString()); // JSON
|
||||
logger.log(Level.INFO, "jsoniter = " + org.xbib.datastructures.json.iterator.JsonIterator.deserialize(input).asMap().toString()); // JSON
|
||||
byte[] b = input.getBytes(StandardCharsets.UTF_8);
|
||||
logger.log(Level.INFO, "jsondsl = " + new DslJson<>().deserialize(Map.class, b, b.length));
|
||||
}
|
||||
}
|
|
@ -24,7 +24,7 @@ import java.util.TreeMap;
|
|||
@Warmup(iterations = 5)
|
||||
@Fork(1)
|
||||
@Measurement(iterations = 5)
|
||||
public class StructuresBenchmark {
|
||||
public class StrictBenchmark {
|
||||
|
||||
Integer[] integers = new Integer[1000];
|
||||
|
||||
|
@ -66,10 +66,7 @@ public class StructuresBenchmark {
|
|||
|
||||
@Benchmark
|
||||
public String linkedHashSet() {
|
||||
LinkedHashSet<Integer> set = new LinkedHashSet<>();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
set.add(integers[i]);
|
||||
}
|
||||
LinkedHashSet<Integer> set = new LinkedHashSet<>(Arrays.asList(integers).subList(0, 1000));
|
||||
return set.toString();
|
||||
}
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
This work is a derived work of
|
||||
|
||||
https://github.com/boonproject/boon/tree/master/boon/src/main/java/org/boon/json
|
||||
|
||||
Apache License 2.0, master branch, as of 2021-02-01
|
File diff suppressed because it is too large
Load diff
|
@ -1,254 +0,0 @@
|
|||
|
||||
/*
|
||||
* Copyright 2013-2014 Richard M. Hightower
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* __________ _____ __ .__
|
||||
* \______ \ ____ ____ ____ /\ / \ _____ | | _|__| ____ ____
|
||||
* | | _// _ \ / _ \ / \ \/ / \ / \\__ \ | |/ / |/ \ / ___\
|
||||
* | | ( <_> | <_> ) | \ /\ / Y \/ __ \| <| | | \/ /_/ >
|
||||
* |______ /\____/ \____/|___| / \/ \____|__ (____ /__|_ \__|___| /\___ /
|
||||
* \/ \/ \/ \/ \/ \//_____/
|
||||
* ____. ___________ _____ ______________.___.
|
||||
* | |____ ___ _______ \_ _____/ / _ \ / _____/\__ | |
|
||||
* | \__ \\ \/ /\__ \ | __)_ / /_\ \ \_____ \ / | |
|
||||
* /\__| |/ __ \\ / / __ \_ | \/ | \/ \ \____ |
|
||||
* \________(____ /\_/ (____ / /_______ /\____|__ /_______ / / ______|
|
||||
* \/ \/ \/ \/ \/ \/
|
||||
*/
|
||||
|
||||
package org.boon;
|
||||
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
|
||||
import static org.boon.Boon.sputs;
|
||||
import static org.boon.Exceptions.requireNonNull;
|
||||
import static org.boon.Lists.*;
|
||||
|
||||
public class Classpaths {
|
||||
|
||||
|
||||
public static List<URL> classpathResources( ClassLoader loader, String resource ) {
|
||||
try {
|
||||
|
||||
Enumeration<URL> resources = loader.getResources( resource );
|
||||
List<URL> list = list( resources );
|
||||
|
||||
if ( isEmpty( list ) && resource.startsWith( "/" ) ) {
|
||||
resource = resource.substring( 1 );
|
||||
return classpathResources( loader, resource );
|
||||
}
|
||||
|
||||
return list;
|
||||
|
||||
|
||||
} catch ( Exception ex ) {
|
||||
|
||||
return Exceptions.handle( List.class, sputs( "Unable to load listFromClassLoader for", resource ),
|
||||
ex );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static List<URL> classpathResources( Class<?> clazz, String resource ) {
|
||||
|
||||
|
||||
List<URL> list = classpathResources( Thread.currentThread().getContextClassLoader(), resource );
|
||||
|
||||
if ( isEmpty( list ) ) {
|
||||
list = classpathResources( clazz.getClassLoader(), resource );
|
||||
}
|
||||
|
||||
|
||||
if ( isEmpty( list ) && resource.startsWith( "/" ) ) {
|
||||
resource = resource.substring( 1 );
|
||||
return classpathResources( clazz, resource );
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<String> resources( Class<?> clazz, String resource ) {
|
||||
|
||||
|
||||
List<String> list = listFromClassLoader(Thread.currentThread().getContextClassLoader(), resource);
|
||||
|
||||
if ( isEmpty( list ) ) {
|
||||
list = listFromClassLoader(clazz.getClassLoader(), resource);
|
||||
}
|
||||
|
||||
|
||||
if ( isEmpty( list ) && resource.startsWith( "/" ) ) {
|
||||
resource = resource.substring( 1 );
|
||||
return resources( clazz, resource );
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
public static List<Path> paths( Class<?> clazz, String resource ) {
|
||||
|
||||
|
||||
List<Path> list = pathsFromClassLoader(Thread.currentThread().getContextClassLoader(), resource);
|
||||
|
||||
if ( isEmpty( list ) ) {
|
||||
list = pathsFromClassLoader(clazz.getClassLoader(), resource);
|
||||
}
|
||||
|
||||
|
||||
if ( isEmpty( list ) && resource.startsWith( "/" ) ) {
|
||||
resource = resource.substring( 1 );
|
||||
return paths( clazz, resource );
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the listFromClassLoader
|
||||
* @param loader
|
||||
* @param resource
|
||||
* @return
|
||||
*/
|
||||
public static List<String> listFromClassLoader(ClassLoader loader, String resource) {
|
||||
final List<URL> resourceURLs = Classpaths.classpathResources( loader, resource );
|
||||
final List<String> resourcePaths = Lists.list( String.class );
|
||||
final Map<URI, FileSystem> pathToZipFileSystems = new HashMap<>(); //So you don't have to keep loading the same jar/zip file.
|
||||
for ( URL resourceURL : resourceURLs ) {
|
||||
|
||||
if ( resourceURL.getProtocol().equals( "jar" ) ) {
|
||||
resourcesFromJar( resourcePaths, resourceURL, pathToZipFileSystems );
|
||||
|
||||
} else {
|
||||
resourcesFromFileSystem( resourcePaths, resourceURL );
|
||||
}
|
||||
}
|
||||
return resourcePaths;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Load the listFromClassLoader
|
||||
* @param loader
|
||||
* @param resource
|
||||
* @return
|
||||
*/
|
||||
public static List<Path> pathsFromClassLoader(ClassLoader loader, String resource) {
|
||||
final List<URL> resourceURLs = Classpaths.classpathResources( loader, resource );
|
||||
final List<Path> resourcePaths = Lists.list( Path.class );
|
||||
final Map<URI, FileSystem> pathToZipFileSystems = new HashMap<>(); //So you don't have to keep loading the same jar/zip file.
|
||||
for ( URL resourceURL : resourceURLs ) {
|
||||
|
||||
if ( resourceURL.getProtocol().equals( "jar" ) ) {
|
||||
pathsFromJar( resourcePaths, resourceURL, pathToZipFileSystems );
|
||||
|
||||
} else {
|
||||
pathsFromFileSystem( resourcePaths, resourceURL );
|
||||
}
|
||||
}
|
||||
return resourcePaths;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static void resourcesFromFileSystem( List<String> resourcePaths, URL u ) {
|
||||
URI fileURI = IO.createURI( u.toString() );
|
||||
|
||||
|
||||
add( resourcePaths, IO.uriToPath( fileURI ).toString() );
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static void pathsFromFileSystem( List<Path> resourcePaths, URL u ) {
|
||||
URI fileURI = IO.createURI( u.toString() );
|
||||
|
||||
|
||||
add( resourcePaths, IO.uriToPath( fileURI ) );
|
||||
}
|
||||
|
||||
private static void resourcesFromJar( List<String> resourcePaths, URL resourceURL, Map<URI, FileSystem> pathToZipFileSystems ) {
|
||||
|
||||
String str = resourceURL.toString();
|
||||
|
||||
final String[] strings = StringScanner.split( str, '!' );
|
||||
|
||||
URI fileJarURI = URI.create( strings[ 0 ] );
|
||||
String resourcePath = strings[ 1 ];
|
||||
|
||||
if ( !pathToZipFileSystems.containsKey( fileJarURI ) ) {
|
||||
pathToZipFileSystems.put( fileJarURI, IO.zipFileSystem(fileJarURI) );
|
||||
}
|
||||
|
||||
FileSystem fileSystem = pathToZipFileSystems.get( fileJarURI );
|
||||
|
||||
Path path = fileSystem.getPath(resourcePath);
|
||||
|
||||
if (path != null) {
|
||||
add( resourcePaths, str);
|
||||
}
|
||||
}
|
||||
|
||||
private static void pathsFromJar( List<Path> resourcePaths, URL resourceURL, Map<URI, FileSystem> pathToZipFileSystems ) {
|
||||
|
||||
String str = resourceURL.toString();
|
||||
|
||||
final String[] strings = StringScanner.split( str, '!' );
|
||||
|
||||
URI fileJarURI = URI.create( strings[ 0 ] );
|
||||
String resourcePath = strings[ 1 ];
|
||||
|
||||
if ( !pathToZipFileSystems.containsKey( fileJarURI ) ) {
|
||||
pathToZipFileSystems.put( fileJarURI, IO.zipFileSystem(fileJarURI) );
|
||||
}
|
||||
|
||||
FileSystem fileSystem = pathToZipFileSystems.get( fileJarURI );
|
||||
|
||||
Path path = fileSystem.getPath(resourcePath);
|
||||
|
||||
if (path != null) {
|
||||
add( resourcePaths, path);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void resourcePathsFromJar( List<Path> resourcePaths, URL resourceURL, Map<URI, FileSystem> pathToZipFileSystems ) {
|
||||
|
||||
String str = resourceURL.toString();
|
||||
|
||||
final String[] strings = StringScanner.split( str, '!' );
|
||||
|
||||
URI fileJarURI = URI.create( strings[ 0 ] );
|
||||
String resourcePath = strings[ 1 ];
|
||||
|
||||
if ( !pathToZipFileSystems.containsKey( fileJarURI ) ) {
|
||||
pathToZipFileSystems.put( fileJarURI, IO.zipFileSystem(fileJarURI) );
|
||||
}
|
||||
|
||||
FileSystem fileSystem = pathToZipFileSystems.get( fileJarURI );
|
||||
|
||||
Path path = fileSystem.getPath(resourcePath);
|
||||
|
||||
if (path != null) {
|
||||
add( resourcePaths, path);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package org.boon;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by Richard on 9/9/14.
|
||||
*/
|
||||
public interface Entry<K, V> extends Comparable<Entry>, Map.Entry<K, V>,
|
||||
Serializable, Cloneable {
|
||||
K key();
|
||||
|
||||
V value();
|
||||
|
||||
boolean equals(Entry o);
|
||||
}
|
|
@ -1,504 +0,0 @@
|
|||
/*
|
||||
* Copyright 2013-2014 Richard M. Hightower
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* __________ _____ __ .__
|
||||
* \______ \ ____ ____ ____ /\ / \ _____ | | _|__| ____ ____
|
||||
* | | _// _ \ / _ \ / \ \/ / \ / \\__ \ | |/ / |/ \ / ___\
|
||||
* | | ( <_> | <_> ) | \ /\ / Y \/ __ \| <| | | \/ /_/ >
|
||||
* |______ /\____/ \____/|___| / \/ \____|__ (____ /__|_ \__|___| /\___ /
|
||||
* \/ \/ \/ \/ \/ \//_____/
|
||||
* ____. ___________ _____ ______________.___.
|
||||
* | |____ ___ _______ \_ _____/ / _ \ / _____/\__ | |
|
||||
* | \__ \\ \/ /\__ \ | __)_ / /_\ \ \_____ \ / | |
|
||||
* /\__| |/ __ \\ / / __ \_ | \/ | \/ \ \____ |
|
||||
* \________(____ /\_/ (____ / /_______ /\____|__ /_______ / / ______|
|
||||
* \/ \/ \/ \/ \/ \/
|
||||
*/
|
||||
|
||||
package org.boon;
|
||||
|
||||
import org.boon.primitive.CharBuf;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.*;
|
||||
|
||||
import static org.boon.Maps.map;
|
||||
import static org.boon.primitive.Arry.add;
|
||||
import static org.boon.primitive.Arry.array;
|
||||
import static org.boon.Boon.sputs;
|
||||
import static org.boon.Sets.set;
|
||||
import static org.boon.Str.startsWithItemInCollection;
|
||||
|
||||
public class Exceptions {
|
||||
|
||||
|
||||
|
||||
private static final Set<String> ignorePackages = set("sun.", "com.sun.",
|
||||
"javax.java", "java.", "oracle.", "com.oracle.", "org.junit", "org.boon.Exceptions",
|
||||
"com.intellij");
|
||||
|
||||
|
||||
public static void requireNonNull(Object obj) {
|
||||
if (obj == null)
|
||||
die("Required object assertion exception");
|
||||
}
|
||||
|
||||
public static void requireNonNulls(String message, Object... array) {
|
||||
|
||||
int index = 0;
|
||||
for (Object obj : array) {
|
||||
if (obj == null)
|
||||
die(message, index);
|
||||
|
||||
index++;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void dieIfAnyParametersAreNull(String methodName, Object... parameters) {
|
||||
|
||||
requireNonNull(sputs("METHOD", methodName, "Parameter at index was null: index="));
|
||||
}
|
||||
|
||||
public static void requireNonNull(Object obj, String message) {
|
||||
if (obj == null)
|
||||
die(message);
|
||||
|
||||
}
|
||||
|
||||
public static boolean die() {
|
||||
throw new SoftenedException( "died" );
|
||||
}
|
||||
|
||||
public static boolean die( String message ) {
|
||||
throw new SoftenedException( message );
|
||||
}
|
||||
|
||||
|
||||
public static boolean die( Object... messages ) {
|
||||
throw new SoftenedException( sputs(messages) );
|
||||
}
|
||||
|
||||
|
||||
public static <T> T die( Class<T> clazz, String message ) {
|
||||
throw new SoftenedException( message );
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static <T> T die( Class<T> clazz, Object... messages ) {
|
||||
throw new SoftenedException( sputs(messages) );
|
||||
}
|
||||
|
||||
public static void handle( Exception e ) {
|
||||
throw new SoftenedException( e );
|
||||
}
|
||||
|
||||
|
||||
public static <T> T handle( Class<T> clazz, Exception e ) {
|
||||
|
||||
if ( e instanceof SoftenedException ) {
|
||||
throw ( SoftenedException ) e;
|
||||
}
|
||||
throw new SoftenedException( e );
|
||||
}
|
||||
|
||||
public static <T> T handle( Class<T> clazz, String message, Throwable e ) {
|
||||
|
||||
throw new SoftenedException( message, e );
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static <T> T handle( Class<T> clazz, Throwable e, Object... messages ) {
|
||||
|
||||
throw new SoftenedException( sputs(messages), e );
|
||||
}
|
||||
|
||||
public static void handle( Throwable e, Object... messages ) {
|
||||
|
||||
throw new SoftenedException( sputs(messages), e );
|
||||
}
|
||||
|
||||
|
||||
public static void printStackTrace(CharBuf charBuf, StackTraceElement[] stackTrace) {
|
||||
for (StackTraceElement st : stackTrace) {
|
||||
if (st.getClassName().contains("org.boon.Exceptions")) {
|
||||
continue;
|
||||
}
|
||||
charBuf.indent(10).println(st);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T tryIt( Class<T> clazz, TrialWithReturn<T> tryIt ) {
|
||||
try {
|
||||
return tryIt.tryIt();
|
||||
} catch ( Exception ex ) {
|
||||
throw new SoftenedException( ex );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void tryIt( Trial tryIt ) {
|
||||
try {
|
||||
tryIt.tryIt();
|
||||
} catch ( Exception ex ) {
|
||||
throw new SoftenedException( ex );
|
||||
}
|
||||
}
|
||||
|
||||
public static void handle( String message, Throwable e ) {
|
||||
throw new SoftenedException( message, e );
|
||||
}
|
||||
|
||||
public static void tryIt( String message, Trial tryIt ) {
|
||||
try {
|
||||
tryIt.tryIt();
|
||||
} catch ( Exception ex ) {
|
||||
throw new SoftenedException( message, ex );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static interface Trial {
|
||||
void tryIt() throws Exception;
|
||||
}
|
||||
|
||||
public static interface TrialWithReturn<T> {
|
||||
T tryIt() throws Exception;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static StackTraceElement[] getFilteredStackTrace(StackTraceElement[] stackTrace) {
|
||||
|
||||
|
||||
if (stackTrace == null || stackTrace.length == 0) {
|
||||
return new StackTraceElement[0];
|
||||
}
|
||||
List<StackTraceElement> list = new ArrayList<>();
|
||||
Set<String> seenThisBefore = new HashSet<>();
|
||||
|
||||
for (StackTraceElement st : stackTrace) {
|
||||
if ( startsWithItemInCollection( st.getClassName(), ignorePackages ) ) {
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
String key = Boon.sputs(st.getClassName(), st.getFileName(), st.getMethodName(), st.getLineNumber());
|
||||
if (seenThisBefore.contains(key)) {
|
||||
continue;
|
||||
} else {
|
||||
seenThisBefore.add(key);
|
||||
}
|
||||
|
||||
list.add(st);
|
||||
}
|
||||
|
||||
return array( StackTraceElement.class, list );
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class SoftenedException extends RuntimeException {
|
||||
|
||||
public SoftenedException( String message ) {
|
||||
super( message );
|
||||
}
|
||||
|
||||
public SoftenedException( String message, Throwable cause ) {
|
||||
super( message, cause );
|
||||
}
|
||||
|
||||
public SoftenedException( Throwable cause ) {
|
||||
super( "Wrapped Exception", cause );
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return super.getMessage() + ( getCause() == null ? "" :
|
||||
getCauseMessage() );
|
||||
}
|
||||
|
||||
private String getCauseMessage() {
|
||||
return "\n CAUSE " + getCause().getClass().getName() + " :: " +
|
||||
getCause().getMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLocalizedMessage() {
|
||||
return this.getMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public StackTraceElement[] getStackTrace() {
|
||||
if ( getRootCause() != null ) {
|
||||
return add(getRootCause().getStackTrace(), super.getStackTrace());
|
||||
} else {
|
||||
return super.getStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Throwable getCause() {
|
||||
return super.getCause();
|
||||
}
|
||||
|
||||
|
||||
public Throwable getRootCause() {
|
||||
|
||||
Throwable cause = super.getCause();
|
||||
|
||||
Throwable lastCause = super.getCause();
|
||||
|
||||
while (cause != null) {
|
||||
lastCause = cause;
|
||||
cause = cause.getCause();
|
||||
|
||||
}
|
||||
return lastCause;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void printStackTrace( CharBuf charBuf) {
|
||||
|
||||
|
||||
charBuf.puts("MESSAGE:", this.getMessage());
|
||||
if (this.getRootCause() !=null) {
|
||||
charBuf.puts("ROOT CAUSE MESSAGE:", this.getRootCause().getMessage());
|
||||
} else if (this.getCause()!=null) {
|
||||
charBuf.puts("CAUSE MESSAGE:", this.getCause().getMessage());
|
||||
}
|
||||
|
||||
|
||||
StackTraceElement[] stackTrace = this.getFilteredStackTrace();
|
||||
|
||||
if (stackTrace.length > 0) {
|
||||
charBuf.indent(5).addLine("This happens around this area in your code.");
|
||||
Exceptions.printStackTrace(charBuf, stackTrace);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ( getRootCause() != null ) {
|
||||
charBuf.addLine().puts("Caused by:", "message:", this.getRootCause().getMessage(), "type", this.getRootCause().getClass().getName());
|
||||
stackTrace = this.getRootCause().getStackTrace();
|
||||
Exceptions.printStackTrace(charBuf, stackTrace);
|
||||
}
|
||||
|
||||
charBuf.addLine().multiply('-', 50).addLine().multiply('-', 50).addLine();
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
|
||||
super.printStackTrace( new PrintWriter(writer) );
|
||||
|
||||
charBuf.add(writer);
|
||||
|
||||
charBuf.addLine().multiply('-', 50).addLine();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public StackTraceElement[] getFilteredStackTrace() {
|
||||
|
||||
|
||||
StackTraceElement[] filteredStackTrace = Exceptions.getFilteredStackTrace(super.getStackTrace());
|
||||
if ( filteredStackTrace.length > 0 ) {
|
||||
|
||||
if (super.getCause() != null) {
|
||||
StackTraceElement[] cause = Exceptions.getFilteredStackTrace(super.getCause().getStackTrace());
|
||||
|
||||
if (cause.length > 0) {
|
||||
filteredStackTrace= add(cause, filteredStackTrace);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (super.getCause() != null) {
|
||||
|
||||
filteredStackTrace = Exceptions.getFilteredStackTrace(super.getCause().getStackTrace());
|
||||
}
|
||||
}
|
||||
|
||||
return Exceptions.getFilteredStackTrace(super.getStackTrace());
|
||||
|
||||
}
|
||||
|
||||
|
||||
public CharBuf printStackTraceIntoCharBuf( ) {
|
||||
|
||||
CharBuf out = CharBuf.create(100);
|
||||
printStackTrace(out);
|
||||
return out;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printStackTrace( PrintStream s ) {
|
||||
s.print(printStackTraceIntoCharBuf().toString());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void printStackTrace( PrintWriter s ) {
|
||||
s.print(printStackTraceIntoCharBuf().toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printStackTrace() {
|
||||
System.err.print(printStackTraceIntoCharBuf().toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String toString( Exception ex ) {
|
||||
CharBuf buffer = CharBuf.create( 255 );
|
||||
buffer.addLine( ex.getLocalizedMessage() );
|
||||
|
||||
final StackTraceElement[] stackTrace = ex.getStackTrace();
|
||||
for ( StackTraceElement element : stackTrace ) {
|
||||
buffer.add( element.getClassName() );
|
||||
sputs( " ", buffer, "class", element.getClassName(),
|
||||
"method", element.getMethodName(), "line", element.getLineNumber() );
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static String asJson(Exception ex) {
|
||||
CharBuf buffer = CharBuf.create( 255 );
|
||||
|
||||
buffer.add('{');
|
||||
|
||||
buffer.addLine().indent(5).addJsonFieldName("message")
|
||||
.asJsonString(ex.getMessage()).addLine(',');
|
||||
|
||||
if (ex.getCause()!=null) {
|
||||
buffer.addLine().indent(5).addJsonFieldName("causeMessage")
|
||||
.asJsonString(ex.getCause().getMessage()).addLine(',');
|
||||
|
||||
|
||||
if (ex.getCause().getCause()!=null) {
|
||||
buffer.addLine().indent(5).addJsonFieldName("cause2Message")
|
||||
.asJsonString(ex.getCause().getCause().getMessage()).addLine(',');
|
||||
|
||||
if (ex.getCause().getCause().getCause()!=null) {
|
||||
buffer.addLine().indent(5).addJsonFieldName("cause3Message")
|
||||
.asJsonString(ex.getCause().getCause().getCause().getMessage()).addLine(',');
|
||||
|
||||
if (ex.getCause().getCause().getCause().getCause()!=null) {
|
||||
buffer.addLine().indent(5).addJsonFieldName("cause4Message")
|
||||
.asJsonString(ex.getCause().getCause().getCause().getCause().getMessage()).addLine(',');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
StackTraceElement[] stackTrace = getFilteredStackTrace(ex.getStackTrace());
|
||||
|
||||
if ( stackTrace!=null && stackTrace.length > 0 ) {
|
||||
|
||||
buffer.addLine().indent(5).addJsonFieldName("stackTrace").addLine();
|
||||
|
||||
stackTraceToJson(buffer, stackTrace);
|
||||
|
||||
buffer.add(',');
|
||||
}
|
||||
|
||||
buffer.addLine().indent(5).addJsonFieldName("fullStackTrace").addLine();
|
||||
stackTrace = ex.getStackTrace();
|
||||
stackTraceToJson(buffer, stackTrace);
|
||||
|
||||
buffer.add( '}' );
|
||||
return buffer.toString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static Map asMap(Exception ex) {
|
||||
StackTraceElement[] stackTrace = getFilteredStackTrace(ex.getStackTrace());
|
||||
|
||||
List stackTraceList = Lists.list(stackTrace);
|
||||
|
||||
List fullStackTrace = Lists.list(ex.getStackTrace());
|
||||
|
||||
|
||||
return map(
|
||||
|
||||
"message", ex.getMessage(),
|
||||
"causeMessage", ex.getCause()!=null ? ex.getCause().getMessage() : "none",
|
||||
"stackTrace", stackTraceList,
|
||||
"fullStackTrace", fullStackTrace
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void stackTraceToJson(CharBuf buffer, StackTraceElement[] stackTrace) {
|
||||
|
||||
if (stackTrace.length==0) {
|
||||
buffer.addLine("[]");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
buffer.multiply(' ', 16).addLine('[');
|
||||
|
||||
for ( int index = 0; index < stackTrace.length; index++ ) {
|
||||
StackTraceElement element = stackTrace[ index ];
|
||||
|
||||
if (element.getClassName( |