Skip to content
Permalink
Browse files

Improve error handling in AnnotationBasedJsonparsable and rename buil…

…der to factory

Signed-off-by: Klem Yannic (INST/ECS1) <yannic.klem@bosch-si.com>
  • Loading branch information
Yannic92 committed Apr 29, 2019
1 parent c611c45 commit b74f1f6171bbbd43152c6083ce92df0517c336dd
@@ -14,7 +14,7 @@

import java.lang.annotation.Annotation;

public abstract class AbstractAnnotationBasedJsonParsableBuilder<T, A extends Annotation> {
public abstract class AbstractAnnotationBasedJsonParsableFactory<T, A extends Annotation> {

AnnotationBasedJsonParsable<T> fromAnnotation(final A annotation, final Class<? extends T> classToParse) {
final String methodName = getMethodNameFor(annotation);
@@ -25,15 +25,15 @@
protected AbstractGlobalJsonParsableRegistry(
final Class<T> parsedClass,
final Class<A> annotationClass,
final AbstractAnnotationBasedJsonParsableBuilder<T, A> annotationBasedJsonParsableBuilder) {
final AbstractAnnotationBasedJsonParsableFactory<T, A> annotationBasedJsonParsableBuilder) {

super(initAnnotationBasedParseStrategies(parsedClass, annotationClass, annotationBasedJsonParsableBuilder));
}

protected AbstractGlobalJsonParsableRegistry(
final Class<T> parsedClass,
final Class<A> annotationClass,
final AbstractAnnotationBasedJsonParsableBuilder<T, A> annotationBasedJsonParsableBuilder,
final AbstractAnnotationBasedJsonParsableFactory<T, A> annotationBasedJsonParsableBuilder,
final Map<String, JsonParsable<T>> parseStrategies) {

super(mergeParsingStrategies(
@@ -55,7 +55,7 @@ protected AbstractGlobalJsonParsableRegistry(
private static <T, A extends Annotation> Map<String, JsonParsable<T>> initAnnotationBasedParseStrategies(
final Class<T> baseClass,
final Class<A> annotationClass,
final AbstractAnnotationBasedJsonParsableBuilder<T, A> annotationBasedJsonParsableBuilder) {
final AbstractAnnotationBasedJsonParsableFactory<T, A> annotationBasedJsonParsableBuilder) {

final Map<String, JsonParsable<T>> parseRegistries = new HashMap<>();
final Iterable<Class<?>> annotatedClasses = ClassIndex.getAnnotated(annotationClass);
@@ -17,9 +17,11 @@

import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonParseException;
import org.eclipse.ditto.model.base.exceptions.DittoJsonException;
import org.eclipse.ditto.json.JsonParseException;
import org.eclipse.ditto.model.base.headers.DittoHeaders;

public final class AnnotationBasedJsonParsable<T> implements JsonParsable<T> {
final class AnnotationBasedJsonParsable<T> implements JsonParsable<T> {

private static final Class<?> JSON_OBJECT_PARAMETER = JsonObject.class;
private static final Class<?> DITTO_HEADERS_PARAMETER = DittoHeaders.class;
@@ -36,6 +38,12 @@
try {
this.parseMethod =
parsedClass.getMethod(parsingMethodName, JSON_OBJECT_PARAMETER, DITTO_HEADERS_PARAMETER);
final Class<?> returnType = parseMethod.getReturnType();
if (!parsedClass.isAssignableFrom(returnType)) {
throw new IllegalArgumentException(
String.format("Parse method is invalid. Return type <%s> of parse method must be assignable " +
"to parsed class: <%s>.", returnType.getSimpleName(), parsedClass.getSimpleName()));
}
} catch (final NoSuchMethodException e) {
throw new DeserializationStrategyNotFoundError(parsedClass, e);
}
@@ -50,28 +58,31 @@ public String getV1FallbackKey() {
}


@SuppressWarnings("unchecked") //suppressed because returned type is ensured in constructor
@Override
public T parse(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
try {
return (T) parseMethod.invoke(null, jsonObject, dittoHeaders);
} catch (final ClassCastException | IllegalAccessException e) {
throw buildJsonTypeNotParsableException(e, dittoHeaders);
throw buildDittoJsonException(e, jsonObject, dittoHeaders);
} catch (final InvocationTargetException e) {
final Throwable targetException = e.getTargetException();

if (targetException instanceof RuntimeException) {
throw (RuntimeException) targetException;
}

throw buildJsonTypeNotParsableException(targetException, dittoHeaders);
throw buildDittoJsonException(targetException, jsonObject, dittoHeaders);
}
}

private JsonTypeNotParsableException buildJsonTypeNotParsableException(final Throwable cause,
private DittoJsonException buildDittoJsonException(final Throwable cause,
final JsonObject jsonObject,
final DittoHeaders dittoHeaders) {
//TODO: throw better exception.
throw JsonParseException.newBuilder()
.message(cause.getMessage())
.build();

return new DittoJsonException(JsonParseException.newBuilder()
.message(String.format("Error during parsing json: <%s>", jsonObject.toString()))
.cause(cause).build(),
dittoHeaders);
}
}
@@ -42,7 +42,7 @@ private GlobalErrorRegistry(final DittoJsonExceptionRegistry dittoJsonExceptionR
super(
DittoRuntimeException.class,
JsonParsableException.class,
new ExceptionParsingStrategyBuilder(),
new ExceptionParsingStrategyFactory(),
dittoJsonExceptionRegistry.getDittoJsonParseRegistries()
);
}
@@ -110,10 +110,10 @@ protected String resolveType(final JsonObject jsonObject) {
* Contains all strategies to deserialize {@link DittoRuntimeException} annotated with {@link JsonParsableException}
* from a combination of {@link JsonObject} and {@link DittoHeaders}.
*/
private static final class ExceptionParsingStrategyBuilder
extends AbstractAnnotationBasedJsonParsableBuilder<DittoRuntimeException, JsonParsableException> {
private static final class ExceptionParsingStrategyFactory
extends AbstractAnnotationBasedJsonParsableFactory<DittoRuntimeException, JsonParsableException> {

private ExceptionParsingStrategyBuilder() {}
private ExceptionParsingStrategyFactory() {}

@Override
protected String getV1FallbackKeyFor(final JsonParsableException annotation) {
@@ -20,7 +20,7 @@
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.JsonParsableCommand;
import org.eclipse.ditto.signals.base.AbstractAnnotationBasedJsonParsableBuilder;
import org.eclipse.ditto.signals.base.AbstractAnnotationBasedJsonParsableFactory;
import org.eclipse.ditto.signals.base.AbstractGlobalJsonParsableRegistry;

/**
@@ -35,7 +35,7 @@
private static final GlobalCommandRegistry INSTANCE = new GlobalCommandRegistry();

private GlobalCommandRegistry() {
super(Command.class, JsonParsableCommand.class, new CommandParsingStrategyBuilder());
super(Command.class, JsonParsableCommand.class, new CommandParsingStrategyFactory());
}

/**
@@ -67,10 +67,10 @@ protected String resolveType(final JsonObject jsonObject) {
* Contains all strategies to deserialize {@link Command} annotated with {@link JsonParsableCommand}
* from a combination of {@link JsonObject} and {@link DittoHeaders}.
*/
private static final class CommandParsingStrategyBuilder
extends AbstractAnnotationBasedJsonParsableBuilder<Command, JsonParsableCommand> {
private static final class CommandParsingStrategyFactory
extends AbstractAnnotationBasedJsonParsableFactory<Command, JsonParsableCommand> {

private CommandParsingStrategyBuilder() {}
private CommandParsingStrategyFactory() {}

@Override
protected String getV1FallbackKeyFor(final JsonParsableCommand annotation) {
@@ -17,7 +17,7 @@
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.JsonParsableCommandResponse;
import org.eclipse.ditto.signals.base.AbstractAnnotationBasedJsonParsableBuilder;
import org.eclipse.ditto.signals.base.AbstractAnnotationBasedJsonParsableFactory;
import org.eclipse.ditto.signals.base.AbstractGlobalJsonParsableRegistry;

/**
@@ -32,7 +32,7 @@
private static final GlobalCommandResponseRegistry INSTANCE = new GlobalCommandResponseRegistry();

private GlobalCommandResponseRegistry() {
super(CommandResponse.class, JsonParsableCommandResponse.class, new CommandResponseParsingStrategyBuilder());
super(CommandResponse.class, JsonParsableCommandResponse.class, new CommandResponseParsingStrategyFactory());
}

/**
@@ -53,10 +53,10 @@ protected String resolveType(final JsonObject jsonObject) {
* Contains all strategies to deserialize {@link CommandResponse} annotated with {@link JsonParsableCommandResponse}
* from a combination of {@link JsonObject} and {@link DittoHeaders}.
*/
private static final class CommandResponseParsingStrategyBuilder
extends AbstractAnnotationBasedJsonParsableBuilder<CommandResponse, JsonParsableCommandResponse> {
private static final class CommandResponseParsingStrategyFactory
extends AbstractAnnotationBasedJsonParsableFactory<CommandResponse, JsonParsableCommandResponse> {

private CommandResponseParsingStrategyBuilder() {}
private CommandResponseParsingStrategyFactory() {}

@Override
protected String getV1FallbackKeyFor(final JsonParsableCommandResponse annotation) {
@@ -21,7 +21,7 @@
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.base.json.JsonParsableEvent;
import org.eclipse.ditto.signals.base.AbstractAnnotationBasedJsonParsableBuilder;
import org.eclipse.ditto.signals.base.AbstractAnnotationBasedJsonParsableFactory;
import org.eclipse.ditto.signals.base.AbstractGlobalJsonParsableRegistry;
import org.eclipse.ditto.signals.base.JsonParsable;

@@ -37,7 +37,7 @@
private static final GlobalEventRegistry INSTANCE = new GlobalEventRegistry();

private GlobalEventRegistry() {
super(Event.class, JsonParsableEvent.class, new EventParsingStrategyBuilder());
super(Event.class, JsonParsableEvent.class, new EventParsingStrategyFactory());
}

/**
@@ -80,10 +80,10 @@ protected String resolveType(final JsonObject jsonObject) {
* Contains all strategies to deserialize {@link Event} annotated with {@link JsonParsableEvent}
* from a combination of {@link JsonObject} and {@link DittoHeaders}.
*/
private static final class EventParsingStrategyBuilder extends
AbstractAnnotationBasedJsonParsableBuilder<Event, JsonParsableEvent> {
private static final class EventParsingStrategyFactory extends
AbstractAnnotationBasedJsonParsableFactory<Event, JsonParsableEvent> {

private EventParsingStrategyBuilder() {}
private EventParsingStrategyFactory() {}

@Override
protected String getV1FallbackKeyFor(final JsonParsableEvent annotation) {

0 comments on commit b74f1f6

Please sign in to comment.
You can’t perform that action at this time.