checkpointing
This commit is contained in:
@@ -12,4 +12,12 @@
|
||||
<artifactId>daxi.moe.main</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>daxi.moe</groupId>
|
||||
<artifactId>daxi.moe.api</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.app;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Specifies the {@link Converter} implementation used to convert a field to and from
|
||||
* {@link daxi.moe.model.AnyData}.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ConvertUsing {
|
||||
|
||||
/**
|
||||
* Returns the converter implementation to use for the annotated field.
|
||||
*
|
||||
* @return the converter class
|
||||
*/
|
||||
Class<? extends Converter> value();
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.app;
|
||||
|
||||
public class DecodeException extends Exception {
|
||||
|
||||
private final String type;
|
||||
private final String method;
|
||||
|
||||
public DecodeException(String typeName, String methodName, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.type = typeName;
|
||||
this.method = methodName;
|
||||
}
|
||||
|
||||
public DecodeException(String message, Throwable cause) {
|
||||
this(null, null, message, cause);
|
||||
}
|
||||
|
||||
public DecodeException(Throwable cause) {
|
||||
this(null, null, null, cause);
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.app;
|
||||
|
||||
import daxi.moe.model.AnyData;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Context for external data sources, such as a byte array or an inputstream. The data is loaded
|
||||
* from these sources, and then converted into Java entities.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public interface External {
|
||||
|
||||
External from(InputStream in);
|
||||
|
||||
External from(byte[] data);
|
||||
|
||||
boolean hasPath(String path);
|
||||
|
||||
AnyData getModel();
|
||||
|
||||
<T> T convert(Class<T> type);
|
||||
|
||||
<T> T convert(String path, Class<T> type);
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.app;
|
||||
|
||||
import daxi.moe.model.AnyData;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public interface Internal {
|
||||
|
||||
Internal from(Object obj);
|
||||
|
||||
AnyData getModel();
|
||||
|
||||
byte[] convert() throws IOException;
|
||||
|
||||
byte[] convert(boolean pretty) throws IOException;
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.app;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MapFrom {
|
||||
|
||||
String value();
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.app;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MapTo {
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.app;
|
||||
|
||||
import java.util.ServiceConfigurationError;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public abstract class Moe {
|
||||
|
||||
private static final Logger L = Logger.getLogger(Moe.class.getName());
|
||||
|
||||
protected abstract Moe options(Options... opts);
|
||||
|
||||
public abstract External external(String format);
|
||||
|
||||
public abstract Internal internal(String format);
|
||||
|
||||
public static Moe create() {
|
||||
ServiceLoader<Moe> loader = ServiceLoader.load(Moe.class);
|
||||
try {
|
||||
java.util.Optional<Moe> moeopt = loader.findFirst();
|
||||
if (moeopt.isEmpty()) {
|
||||
if(L.isLoggable(Level.SEVERE)) {
|
||||
L.log(Level.SEVERE, "moe implementation NOT found");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
Moe moe = moeopt.get();
|
||||
if(L.isLoggable(Level.CONFIG)) {
|
||||
L.config(String.format("moe implementation found: %s",
|
||||
moe.getClass().getName()));
|
||||
}
|
||||
return moe;
|
||||
}
|
||||
}
|
||||
catch (ServiceConfigurationError err) {
|
||||
if(L.isLoggable(Level.SEVERE)) {
|
||||
L.log(Level.SEVERE, "moe implementation NOT found", err);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Moe create(Options... opts) {
|
||||
return Moe.create().options(opts);
|
||||
}
|
||||
|
||||
public static enum Options {
|
||||
ANYCASE,
|
||||
DECODE_IGNORE_MISSING
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.codec;
|
||||
|
||||
/**
|
||||
* Supplies the format identifier and format-specific codec components for a supported external data
|
||||
* format.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public interface MoeCodec {
|
||||
|
||||
/**
|
||||
* Returns the identifier of the format supported by this provider (e.g., "JSON", "XML").
|
||||
*
|
||||
* @return the supported format identifier
|
||||
*/
|
||||
|
||||
String getFormat();
|
||||
|
||||
/**
|
||||
* Returns a decoder for the format supported by this provider.
|
||||
*
|
||||
* @return a decoder for the supported format
|
||||
*/
|
||||
|
||||
MoeDecoder decoder();
|
||||
|
||||
/**
|
||||
* Returns an encoder for the format supported by this provider.
|
||||
*
|
||||
* @return an encoder for the supported format
|
||||
*/
|
||||
|
||||
MoeEncoder encoder();
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.codec;
|
||||
|
||||
import daxi.moe.model.AnyData;
|
||||
|
||||
/**
|
||||
* Encodes data into the byte representation of a supported format.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
public interface MoeEncoder {
|
||||
|
||||
/**
|
||||
* Encodes the supplied data using the default formatting.
|
||||
*
|
||||
* @param data the data to encode
|
||||
* @return the encoded data as a byte array
|
||||
*/
|
||||
byte[] convert(AnyData data);
|
||||
|
||||
/**
|
||||
* Encodes the supplied data, optionally formatting the output for readability.
|
||||
*
|
||||
* @param data the data to encode
|
||||
* @param pretty whether to format the output for readability
|
||||
* @return the encoded data as a byte array
|
||||
*/
|
||||
byte[] convert(AnyData data, boolean pretty);
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
package daxi.moe.codec.mem;
|
||||
|
||||
import daxi.moe.app.CodecException;
|
||||
import daxi.moe.app.Moe;
|
||||
import daxi.moe.codec.MoeDecoder;
|
||||
import daxi.moe.model.AnyData;
|
||||
@@ -21,20 +22,24 @@ import daxi.moe.model.AnyData;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class MemDecoder implements MoeDecoder {
|
||||
|
||||
@Override
|
||||
public void setSource(byte[] data) {
|
||||
public void setSource(byte[] data) throws CodecException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSource(InputStream input) {
|
||||
public void setSource(InputStream input) throws CodecException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnyData decode(List<Moe.Options> options) {
|
||||
public AnyData decode(List<Moe.Options> options) throws CodecException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,12 +18,21 @@ import daxi.moe.app.External;
|
||||
import daxi.moe.app.Internal;
|
||||
import daxi.moe.app.Moe;
|
||||
import daxi.moe.codec.MoeCodec;
|
||||
import daxi.moe.lib.mod2obj.ExternalImpl;
|
||||
import daxi.moe.lib.obj2mod.InternalImpl;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* The only implementation of {@link Moe}.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
@@ -79,4 +88,4 @@ public class MoeImpl extends Moe {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.m2j;
|
||||
|
||||
import daxi.moe.lib.transform.M2JGlobals;
|
||||
import daxi.moe.lib.transform.M2JLinear;
|
||||
import daxi.moe.lib.transform.M2JStatus;
|
||||
import daxi.moe.model.MappedData;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* The only implementation of the {@link M2JLinear} interface.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class M2JLinearImpl implements M2JLinear {
|
||||
|
||||
private final M2JGlobals globals;
|
||||
|
||||
public M2JLinearImpl(M2JGlobals globals) {
|
||||
this.globals = globals;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface M2JLinear
|
||||
|
||||
@Override
|
||||
public M2JStatus convert(MappedData data, Object targetObj, Method targetMthd) {
|
||||
return M2JStatus.NOT_VALID;
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.m2j;
|
||||
|
||||
import daxi.moe.lib.transform.M2JGlobals;
|
||||
import daxi.moe.lib.transform.M2JMapped;
|
||||
import daxi.moe.lib.transform.M2JStatus;
|
||||
import daxi.moe.model.MappedData;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* The only implementation of the {@link M2JMapped} interface.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class M2JMappedImpl implements M2JMapped {
|
||||
|
||||
private final M2JGlobals globals;
|
||||
|
||||
public M2JMappedImpl(M2JGlobals globals) {
|
||||
this.globals = globals;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface M2JMapped
|
||||
|
||||
@Override
|
||||
public M2JStatus convert(MappedData data, Object targetObj, Method targetMthd) {
|
||||
return M2JStatus.NOT_VALID;
|
||||
}
|
||||
}
|
||||
@@ -1,206 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.m2j;
|
||||
|
||||
import daxi.moe.app.MapFrom;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
|
||||
/**
|
||||
* Contains information about a method on the target Java object. The method and the information
|
||||
* can be used to transform some portions of internal data model and populate the containing object.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class M2JMethod {
|
||||
|
||||
private boolean valid = false;
|
||||
private boolean setter = false;
|
||||
private boolean linAdder = false;
|
||||
private boolean mapAdder = false;
|
||||
private String mapFromKey = null;
|
||||
private Class<?> paramType = null;
|
||||
|
||||
/**
|
||||
* Constructs the object around transforming method . The contained method may or may not be valid
|
||||
* for transformation, depending on the method's signature and annotations.
|
||||
*
|
||||
* @param method the method to be encapsulated
|
||||
*/
|
||||
|
||||
public M2JMethod(Method method) {
|
||||
|
||||
MapFrom mapFrom = method.getAnnotation(MapFrom.class);
|
||||
if(mapFrom == null) {
|
||||
// If the method does not have the @MapFrom annotation, it is not valid for
|
||||
// transformation
|
||||
return;
|
||||
}
|
||||
// populate the mapFromKey field with the value of the @MapFrom annotation
|
||||
mapFromKey = mapFrom.value();
|
||||
if(mapFromKey == null || mapFromKey.isBlank()) {
|
||||
// If the @MapFrom annotation does not have a valid key, it is not valid for
|
||||
// transformation
|
||||
return;
|
||||
}
|
||||
|
||||
int mod = method.getModifiers();
|
||||
if(Modifier.isAbstract(mod) || Modifier.isStatic(mod) || !Modifier.isPrivate(mod)) {
|
||||
// If the method is abstract, static, or not private, it is not valid for transformation
|
||||
return;
|
||||
}
|
||||
if(method.getReturnType() != Void.TYPE) {
|
||||
// If the method does not return void, it is not valid for transformation
|
||||
return;
|
||||
}
|
||||
|
||||
Parameter[] params = method.getParameters();
|
||||
|
||||
// Check if the method name starts with "_set" and has more than 4 characters
|
||||
if(method.getName().startsWith("_set") && method.getName().length() > 4) {
|
||||
if(params.length != 1) {
|
||||
// If the method name starts with "_set" but does not have exactly one parameter, it
|
||||
// is not valid for transformation
|
||||
return;
|
||||
}
|
||||
|
||||
Type type = params[0].getParameterizedType();
|
||||
if(type instanceof TypeVariable<?> || type instanceof ParameterizedType) {
|
||||
// If the parameter type is a type variable or a parameterized type, it is not valid
|
||||
// for transformation
|
||||
return;
|
||||
}
|
||||
paramType = params[0].getType();
|
||||
if(paramType.isArray()) {
|
||||
// If the parameter type is an array, it is not valid for transformation
|
||||
return;
|
||||
}
|
||||
|
||||
valid = true;
|
||||
setter = true;
|
||||
}
|
||||
// Check if the method name starts with "_add" and has more than 4 characters
|
||||
else if(method.getName().startsWith("_add") && method.getName().length() > 4) {
|
||||
if(params.length ==1 ) {
|
||||
Type type = params[0].getParameterizedType();
|
||||
if(type instanceof TypeVariable<?> || type instanceof ParameterizedType) {
|
||||
// If the parameter type is a type variable or a parameterized type, it is not
|
||||
// valid for transformation
|
||||
return;
|
||||
}
|
||||
paramType = params[0].getType();
|
||||
if(paramType.isArray()) {
|
||||
// If the parameter type is an array, it is not valid for transformation
|
||||
return;
|
||||
}
|
||||
valid = true;
|
||||
linAdder = true;
|
||||
}
|
||||
else if(params.length == 2) {
|
||||
if(params[0].getType() != String.class) {
|
||||
// If the first parameter type is not String, it is not valid for transformation
|
||||
return;
|
||||
}
|
||||
Type type = params[1].getParameterizedType();
|
||||
if(type instanceof TypeVariable<?> || type instanceof ParameterizedType) {
|
||||
// If the second parameter type is a type variable or a parameterized type, it
|
||||
// is not valid for transformation
|
||||
return;
|
||||
}
|
||||
paramType = params[1].getType();
|
||||
if(paramType.isArray()) {
|
||||
// If the second parameter type is an array, it is not valid for transformation
|
||||
return;
|
||||
}
|
||||
valid = true;
|
||||
mapAdder = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the method is valid for transformation.
|
||||
*
|
||||
* @return true if the method is valid for transformation, false otherwise.
|
||||
*/
|
||||
|
||||
boolean isValid() {
|
||||
return valid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the method is a setter. A setter is a method that takes a single parameter and
|
||||
* sets it to the containing object.
|
||||
*
|
||||
* @return true if the method is a setter, false otherwise
|
||||
*/
|
||||
|
||||
boolean isSetter() {
|
||||
return setter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the method is a linear adder. A linear adder is a method that takes a single
|
||||
* parameter and adds it to the containing object. The adder method is called multiple times, once
|
||||
* for each value to be added.
|
||||
*
|
||||
* @return true if the method is a linear adder, false otherwise
|
||||
*/
|
||||
|
||||
boolean isLinearAdder() {
|
||||
return linAdder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the method is a mapped adder. A mapped adder is a method that takes two
|
||||
* parameters, the first being a String key and the second being the value to be added. The library
|
||||
* will use the key to determine which portion of the data model is mapped to the method. The adder
|
||||
* method is called multiple times, once for each key-value pair to be added.
|
||||
*
|
||||
* @return true if the method is a mapped adder, false otherwise
|
||||
*/
|
||||
|
||||
boolean isMappedAdder() {
|
||||
return mapAdder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the key from the @MapFrom annotation. The library will use this key to determine which
|
||||
* portion of the data model is mapped to the method.
|
||||
*
|
||||
* @return the key from the @MapFrom annotation
|
||||
*/
|
||||
|
||||
String getMapFromKey() {
|
||||
return mapFromKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parameter type of the method. The library will use this type to determine how to
|
||||
* convert the data model item into the appropriate Java type.
|
||||
*
|
||||
* @return the parameter type of the method
|
||||
*/
|
||||
|
||||
Class<?> getParamType() {
|
||||
return paramType;
|
||||
}
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.m2j;
|
||||
|
||||
import daxi.moe.app.MapFrom;
|
||||
import daxi.moe.lib.transform.M2JGlobals;
|
||||
import daxi.moe.lib.transform.M2JLinear;
|
||||
import daxi.moe.lib.transform.M2JMapped;
|
||||
import daxi.moe.lib.transform.M2JObject;
|
||||
import daxi.moe.lib.transform.M2JSimple;
|
||||
import daxi.moe.lib.transform.M2JStatus;
|
||||
import daxi.moe.model.MappedData;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* The only implementation of the {@link M2JObject} interface.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class M2JObjectImpl implements M2JObject {
|
||||
|
||||
private static final Logger L = Logger.getLogger(M2JObjectImpl.class.getName());
|
||||
|
||||
private final M2JGlobals globals;
|
||||
|
||||
public M2JObjectImpl(M2JGlobals globals) {
|
||||
this.globals = globals;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface M2JObject
|
||||
|
||||
@Override
|
||||
public M2JStatus convert(MappedData data, Object targetObj, Method targetMthd) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T convert(MappedData data, Class<T> targetType) {
|
||||
if(data == null || targetType == null) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning("Invalid arguments: data=" + data + ", targetType=" + targetType);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
List<Method> methods = new LinkedList<>();
|
||||
collateAllMethods(targetType, methods);
|
||||
if(methods.isEmpty()) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning("No suitable methods found in targetType: " + targetType);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
T target;
|
||||
try {
|
||||
Constructor<T> ctor = targetType.getConstructor();
|
||||
target = ctor.newInstance();
|
||||
}
|
||||
catch(Exception exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.log(Level.WARNING, exep.getMessage(), exep);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
for(Method method : methods) {
|
||||
M2JStatus m2jStat = transformImpl(data, target, method);
|
||||
if(m2jStat == M2JStatus.ERROR) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helper methods
|
||||
|
||||
private void collateAllMethods(Class<?> type, List<Method> methodList) {
|
||||
if(type.getClassLoader() == null) {
|
||||
// Skip system classes
|
||||
return;
|
||||
}
|
||||
|
||||
for (Method method : type.getDeclaredMethods()) {
|
||||
if (!Modifier.isStatic(method.getModifiers())) {
|
||||
MapFrom ann = method.getAnnotation(MapFrom.class);
|
||||
if (ann != null && !ann.value().isBlank()) {
|
||||
methodList.add(method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type.getSuperclass() != null) {
|
||||
collateAllMethods(type.getSuperclass(), methodList);
|
||||
}
|
||||
}
|
||||
|
||||
private M2JStatus transformImpl(MappedData data, Object targetObj, Method targetMthd) {
|
||||
M2JSimple m2jSim = globals.getM2JSimple();
|
||||
M2JStatus m2jStat = m2jSim.convert(data, targetObj, targetMthd);
|
||||
if(m2jStat != M2JStatus.NOT_VALID) {
|
||||
// here: m2jStat == M2JStatus.SUCCESS or m2jStat == M2JStatus.ERROR
|
||||
return m2jStat;
|
||||
}
|
||||
|
||||
M2JLinear m2jLin = globals.getM2JLinear();
|
||||
m2jStat = m2jLin.convert(data, targetObj, targetMthd);
|
||||
if(m2jStat != M2JStatus.NOT_VALID) {
|
||||
// here: m2jStat == M2JStatus.SUCCESS or m2jStat == M2JStatus.ERROR
|
||||
return m2jStat;
|
||||
}
|
||||
|
||||
M2JMapped m2jMap = globals.getM2JMapped();
|
||||
m2jStat = m2jMap.convert(data, targetObj, targetMthd);
|
||||
if(m2jStat != M2JStatus.NOT_VALID) {
|
||||
// here: m2jStat == M2JStatus.SUCCESS or m2jStat == M2JStatus.ERROR
|
||||
return m2jStat;
|
||||
}
|
||||
|
||||
// here: m2jStat == M2JStatus.NOT_VALID
|
||||
return convert(data, targetObj, targetMthd);
|
||||
}
|
||||
}
|
||||
@@ -1,337 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.m2j;
|
||||
|
||||
import daxi.moe.lib.transform.M2JGlobals;
|
||||
import daxi.moe.lib.transform.M2JSimple;
|
||||
import daxi.moe.lib.transform.M2JStatus;
|
||||
import daxi.moe.model.AnyData;
|
||||
import daxi.moe.model.MappedData;
|
||||
import daxi.moe.model.SimpleData;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* The only implementation of the {@link daxi.moe.lib.transform.M2JSimple} interface.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class M2JSimpleImpl implements M2JSimple {
|
||||
|
||||
private static final Logger L = Logger.getLogger(M2JSimpleImpl.class.getName());
|
||||
|
||||
private final M2JGlobals globals;
|
||||
|
||||
public M2JSimpleImpl(M2JGlobals globals) {
|
||||
this.globals = globals;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface M2JSimple
|
||||
|
||||
@Override
|
||||
public M2JStatus convert(MappedData data, Object target, Method method) {
|
||||
|
||||
M2JMethod m2jMthd = new M2JMethod(method);
|
||||
if(!m2jMthd.isValid() || !m2jMthd.isSetter()) {
|
||||
return M2JStatus.NOT_VALID;
|
||||
}
|
||||
|
||||
Class<?> paramType = m2jMthd.getParamType();
|
||||
if(!isSupportedParamType(paramType)) {
|
||||
return M2JStatus.NOT_VALID;
|
||||
}
|
||||
|
||||
String key = m2jMthd.getMapFromKey();
|
||||
if(!data.contains(key)) {
|
||||
return M2JStatus.NOT_VALID;
|
||||
}
|
||||
|
||||
AnyData adata = data.get(key);
|
||||
SimpleData sdata = adata.asSimple();
|
||||
if(sdata == null) {
|
||||
return M2JStatus.NOT_VALID;
|
||||
}
|
||||
|
||||
Object paramVal = null;
|
||||
|
||||
if(paramType == String.class) {
|
||||
paramVal = sdata.getString();
|
||||
}
|
||||
else if(paramType == BigInteger.class) {
|
||||
paramVal = getBigIntegerValue(sdata);
|
||||
}
|
||||
else if(paramType == BigDecimal.class) {
|
||||
paramVal = getBigDecimalValue(sdata);
|
||||
}
|
||||
else if(paramType == byte.class || paramType == Byte.class) {
|
||||
paramVal = getByteValue(sdata);
|
||||
}
|
||||
else if(paramType == short.class || paramType == Short.class) {
|
||||
paramVal = getShortValue(sdata);
|
||||
}
|
||||
else if(paramType == int.class || paramType == Integer.class) {
|
||||
paramVal = getIntegerValue(sdata);
|
||||
}
|
||||
else if(paramType == long.class || paramType == Long.class) {
|
||||
paramVal = getLongValue(sdata);
|
||||
}
|
||||
else if(paramType == float.class || paramType == Float.class) {
|
||||
paramVal = getFloatValue(sdata);
|
||||
}
|
||||
else if(paramType == double.class || paramType == Double.class) {
|
||||
paramVal = getDoubleValue(sdata);
|
||||
}
|
||||
else if(paramType == boolean.class || paramType == Boolean.class) {
|
||||
paramVal = getBooleanValue(sdata);
|
||||
}
|
||||
|
||||
if(paramVal == null) {
|
||||
return M2JStatus.NOT_VALID;
|
||||
}
|
||||
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
method.invoke(target, paramVal);
|
||||
method.setAccessible(false);
|
||||
}
|
||||
catch (ReflectiveOperationException exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.log(Level.WARNING, exep.getMessage(), exep);
|
||||
}
|
||||
return M2JStatus.ERROR;
|
||||
}
|
||||
return M2JStatus.SUCCESS;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helper methods
|
||||
|
||||
private boolean isSupportedParamType(Class<?> paramType) {
|
||||
return paramType == String.class
|
||||
|| paramType == BigInteger.class
|
||||
|| paramType == BigDecimal.class
|
||||
|| Number.class.isAssignableFrom(paramType)
|
||||
|| paramType == byte.class
|
||||
|| paramType == short.class
|
||||
|| paramType == int.class
|
||||
|| paramType == long.class
|
||||
|| paramType == float.class
|
||||
|| paramType == double.class
|
||||
|| paramType == boolean.class
|
||||
|| paramType == Boolean.class;
|
||||
}
|
||||
|
||||
private BigInteger getBigIntegerValue(SimpleData data) {
|
||||
BigInteger paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = new BigInteger(Long.toString(nval.longValue()));
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = new BigInteger(sval);
|
||||
}
|
||||
catch(Exception exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning("Unable to convert string [" + sval + "] to BigInteger");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private BigDecimal getBigDecimalValue(SimpleData data) {
|
||||
BigDecimal paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = new BigDecimal(nval.toString());
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = new BigDecimal(sval);
|
||||
}
|
||||
catch(Exception exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning("Unable to convert string [" + sval + "] to BigDecimal");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Byte getByteValue(SimpleData data) {
|
||||
Byte paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.byteValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Byte.parseByte(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning("Unable to convert string [" + sval + "] to byte");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Short getShortValue(SimpleData data) {
|
||||
Short paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.shortValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Short.parseShort(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning("Unable to convert string [" + sval + "] to short");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Integer getIntegerValue(SimpleData data) {
|
||||
Integer paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.intValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Integer.parseInt(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning("Unable to convert string [" + sval + "] to integer");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Long getLongValue(SimpleData data) {
|
||||
Long paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.longValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Long.parseLong(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning("Unable to convert string [" + sval + "] to long");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Float getFloatValue(SimpleData data) {
|
||||
Float paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.floatValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Float.parseFloat(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning("Unable to convert string [" + sval + "] to float");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Double getDoubleValue(SimpleData data) {
|
||||
Double paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.doubleValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Double.parseDouble(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning("Unable to convert string [" + sval + "] to double");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Boolean getBooleanValue(SimpleData data) {
|
||||
Boolean paramVal = data.getBoolean();
|
||||
if(paramVal != null) {
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.intValue() != 0;
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
paramVal = Boolean.parseBoolean(sval.toLowerCase(Locale.getDefault()));
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
|
||||
/**
|
||||
* A BlankLinear is a Setter method that has a blank data key. It is used to set the value of a
|
||||
* property on an object, but does not have a specific data key associated with it.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class BlankLinear extends FromMethod {
|
||||
|
||||
private final Class<?> inType;
|
||||
|
||||
public BlankLinear(Method method) throws IllegalArgumentException {
|
||||
super(method);
|
||||
|
||||
if(!getDataKey().isBlank()) {
|
||||
// If the data key is not blank, it is not a blank linear method
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
Parameter[] params = method.getParameters();
|
||||
if(params.length != 1) {
|
||||
// If the method does not have exactly one parameter, it is not valid for conversion
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
Type type = params[0].getParameterizedType();
|
||||
if(type instanceof TypeVariable<?> || type instanceof ParameterizedType) {
|
||||
// If the parameter type is a type variable or a parameterized type, it is not valid
|
||||
// for conversion
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
inType = params[0].getType();
|
||||
if(inType.isArray()) {
|
||||
// If the parameter type is an array, it is not valid for conversion
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of base class FromMethod
|
||||
|
||||
@Override
|
||||
public Class<?> getInputType() {
|
||||
return inType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s(%s)", getMethod().getName(), inType.getName());
|
||||
}
|
||||
|
||||
public static BlankLinear of(Method method) {
|
||||
try {
|
||||
return new BlankLinear(method);
|
||||
}
|
||||
catch (IllegalArgumentException exep) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
|
||||
public class BlankMapped extends FromMethod {
|
||||
|
||||
private final Class<?> inType;
|
||||
|
||||
public BlankMapped(Method method) throws IllegalArgumentException {
|
||||
super(method);
|
||||
|
||||
if(!getDataKey().isBlank()) {
|
||||
// If the data key is not blank, it is not a blank mapped method
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
Parameter[] params = method.getParameters();
|
||||
if(params.length != 2) {
|
||||
// If the method does not have exactly two parameters, it is not valid for conversion
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
if(params[0].getType() != String.class) {
|
||||
// If the first parameter is not of type String, it is not valid for conversion
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
Type type = params[1].getParameterizedType();
|
||||
if(type instanceof TypeVariable<?> || type instanceof ParameterizedType) {
|
||||
// If the second parameter type is a type variable or a parameterized type, it
|
||||
// is not valid for transformation
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
inType = params[1].getType();
|
||||
if(inType.isArray()) {
|
||||
// If the parameter type is an array, it is not valid for conversion
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of base class FromMethod
|
||||
|
||||
@Override
|
||||
public Class<?> getInputType() {
|
||||
return inType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s(%s)", getMethod().getName(), inType.getName());
|
||||
}
|
||||
|
||||
public static BlankMapped of(Method method) {
|
||||
try {
|
||||
return new BlankMapped(method);
|
||||
}
|
||||
catch (IllegalArgumentException exep) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+12
-34
@@ -10,30 +10,20 @@
|
||||
* This file 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.
|
||||
*
|
||||
*/
|
||||
|
||||
package daxi.moe.lib;
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
import daxi.moe.app.External;
|
||||
import daxi.moe.app.Moe;
|
||||
import daxi.moe.app.MoeException;
|
||||
import daxi.moe.codec.MoeCodec;
|
||||
import daxi.moe.codec.MoeDecoder;
|
||||
import daxi.moe.lib.m2j.M2JLinearImpl;
|
||||
import daxi.moe.lib.m2j.M2JMappedImpl;
|
||||
import daxi.moe.lib.m2j.M2JObjectImpl;
|
||||
import daxi.moe.lib.m2j.M2JSimpleImpl;
|
||||
import daxi.moe.lib.transform.M2JGlobals;
|
||||
import daxi.moe.lib.transform.M2JLinear;
|
||||
import daxi.moe.lib.transform.M2JMapped;
|
||||
import daxi.moe.lib.transform.M2JObject;
|
||||
import daxi.moe.lib.transform.M2JSimple;
|
||||
import daxi.moe.model.AnyData;
|
||||
import daxi.moe.model.MappedData;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
@@ -46,24 +36,13 @@ public class ExternalImpl implements External {
|
||||
|
||||
private final MoeDecoder decoder;
|
||||
private final List<Moe.Options> options;
|
||||
private final M2JGlobals globals;
|
||||
private final Populators populators;
|
||||
private AnyData model;
|
||||
|
||||
ExternalImpl(MoeCodec codec, List<Moe.Options> options) {
|
||||
public ExternalImpl(MoeCodec codec, List<Moe.Options> options) {
|
||||
decoder = codec.decoder();
|
||||
|
||||
this.options = options;
|
||||
|
||||
globals = new M2JGlobals(options);
|
||||
M2JSimple m2jSim = new M2JSimpleImpl(globals);
|
||||
globals.setM2J(m2jSim);
|
||||
M2JLinear m2jLin = new M2JLinearImpl(globals);
|
||||
globals.setM2J(m2jLin);
|
||||
M2JMapped m2jMap = new M2JMappedImpl(globals);
|
||||
globals.setM2J(m2jMap);
|
||||
M2JObject m2jObj = new M2JObjectImpl(globals);
|
||||
globals.setM2J(m2jObj);
|
||||
|
||||
populators = new Populators(options);
|
||||
model = new AnyData(new MappedData());
|
||||
}
|
||||
|
||||
@@ -71,7 +50,7 @@ public class ExternalImpl implements External {
|
||||
// Methods of interface External
|
||||
|
||||
@Override
|
||||
public External from(InputStream in) {
|
||||
public External from(InputStream in) throws MoeException {
|
||||
decoder.setSource(in);
|
||||
model = decoder.decode(options);
|
||||
if(model == null) {
|
||||
@@ -81,7 +60,7 @@ public class ExternalImpl implements External {
|
||||
}
|
||||
|
||||
@Override
|
||||
public External from(byte[] data) {
|
||||
public External from(byte[] data) throws MoeException {
|
||||
decoder.setSource(data);
|
||||
model = decoder.decode(options);
|
||||
if(model == null) {
|
||||
@@ -101,19 +80,18 @@ public class ExternalImpl implements External {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T convert(Class<T> type) {
|
||||
public <T> T convert(Class<T> type) throws MoeException {
|
||||
MappedData data = model.asMapped();
|
||||
if(data == null) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning("input data model does not contain MappedData as root element");
|
||||
}
|
||||
L.warning(() -> "input data model does not contain MappedData as root element");
|
||||
return null;
|
||||
}
|
||||
return globals.getM2JObject().convert(data, type);
|
||||
Object retObj = populators.getMapped().create(new AnyData(data), type);
|
||||
return type.cast(retObj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T convert(String path, Class<T> type) {
|
||||
public <T> T convert(String path, Class<T> type) throws MoeException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
import daxi.moe.app.MapFrom;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
/**
|
||||
* Represents a method that is used to convert a nested data model item into a Java object. The
|
||||
* method must be private, non-static, and return void. The method must also be annotated with the
|
||||
* {@link MapFrom} annotation, which specifies the data key that is used to identify the nested data
|
||||
* in the model.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public abstract class FromMethod {
|
||||
|
||||
private final Method method;
|
||||
private final String dataKey;
|
||||
|
||||
protected FromMethod(Method method) {
|
||||
this.method = method;
|
||||
MapFrom mapping = method.getAnnotation(MapFrom.class);
|
||||
if(mapping == null) {
|
||||
// If the method does not have the @Mapping annotation, it is not valid for conversion
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
// populate the dataKey field with the value of the @Mapping annotation
|
||||
dataKey = mapping.value().trim();
|
||||
|
||||
if(method.getReturnType() != Void.TYPE) {
|
||||
// If the method does not return void, it is not valid for conversion
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
int mod = method.getModifiers();
|
||||
// If the method is abstract, static, or not private, it is not valid for conversion
|
||||
if(Modifier.isAbstract(mod) || Modifier.isStatic(mod) || !Modifier.isPrivate(mod)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the data key associated with this method. The data key is used to identify the nested
|
||||
* data in the model that will be converted.
|
||||
*
|
||||
* @return the data key
|
||||
*/
|
||||
|
||||
public final String getDataKey() {
|
||||
return dataKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the method that this FromMethod instance represents. The method is used to set the
|
||||
* value of the converted data in the target object.
|
||||
*
|
||||
* @return the method that is used to convert the data
|
||||
*/
|
||||
|
||||
public final Method getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the input type that the method accepts. The library will use this type to determine how
|
||||
* to convert the data model item into the appropriate (Java) input type.
|
||||
*
|
||||
* @return the input type of the method
|
||||
*/
|
||||
|
||||
public abstract Class<?> getInputType();
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
import daxi.moe.app.ConvertException;
|
||||
import daxi.moe.model.AnyData;
|
||||
import daxi.moe.model.LinearData;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class LinearPopulator implements Populator {
|
||||
|
||||
private static final Logger L = Logger.getLogger(LinearPopulator.class.getName());
|
||||
|
||||
private final Populators populators;
|
||||
|
||||
public LinearPopulator(Populators populators) {
|
||||
this.populators = populators;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface Populator
|
||||
|
||||
@Override
|
||||
public Object create(AnyData data, Class<?> targetType) throws ConvertException {
|
||||
LinearData ldata = data.asLinear();
|
||||
if(ldata == null || targetType == null) {
|
||||
L.warning(() -> "Invalid arguments: data=" + ldata + ", targetType=" + targetType);
|
||||
return null;
|
||||
}
|
||||
|
||||
TargetEntity entity = new TargetEntity(targetType);
|
||||
L.finest(() -> entity.dumpInfo());
|
||||
|
||||
if(entity.hasSetters()) {
|
||||
L.warning(() -> "Target type has setters, need only blank linear: " + targetType);
|
||||
return null;
|
||||
}
|
||||
|
||||
BlankLinear blinear = entity.getBlankLinear();
|
||||
if(blinear == null) {
|
||||
L.warning(() -> "Target type does not have a blank linear method: " + targetType);
|
||||
return null;
|
||||
}
|
||||
|
||||
Object target;
|
||||
try {
|
||||
// Attempt to create a new instance of the target type using its public and default
|
||||
// constructor
|
||||
Constructor<?> ctor = targetType.getConstructor();
|
||||
target = ctor.newInstance();
|
||||
}
|
||||
catch(NoSuchMethodException exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.log(Level.WARNING, exep.getMessage(), exep);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
catch(Exception exep) {
|
||||
throw new ConvertException(targetType.getName(), "<init>",
|
||||
"Error invoking default constructor - " + exep.getMessage(), exep);
|
||||
}
|
||||
|
||||
Status status = populateImpl(data, target, blinear.getMethod(), blinear.getInputType());
|
||||
|
||||
if(status != Status.SUCCESS) {
|
||||
L.warning(() -> "Failed to populate target object: " + targetType);
|
||||
return null;
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Status populate(AnyData data, String dataKey,
|
||||
TargetEntity entity, Object targetObj) throws ConvertException {
|
||||
// note: the check for linear data is done in the populateImpl method, so we don't need to
|
||||
// do it here
|
||||
|
||||
// for each item in the linear data, we will try to populate the target object using the
|
||||
// corresponding setter method with the given data key.
|
||||
|
||||
Setter setter = entity.getSetter(dataKey);
|
||||
if(setter == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
|
||||
return populateImpl(data, targetObj, setter.getMethod(), setter.getInputType());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helper methods
|
||||
|
||||
private Status populateImpl(AnyData data,
|
||||
Object targetObj, Method method,
|
||||
Class<?> popType) throws ConvertException {
|
||||
LinearData ldata = data.asLinear();
|
||||
if(ldata == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
|
||||
Status retstat = Status.NOT_VALID;
|
||||
for(int pos=0; pos<ldata.size(); pos++) {
|
||||
AnyData itemData = ldata.get(pos);
|
||||
if (itemData == null) {
|
||||
continue; // this should not happen, but just in case
|
||||
}
|
||||
|
||||
Object setterVal = null;
|
||||
if(itemData.asSimple() != null) {
|
||||
setterVal = populators.getSimple().create(itemData, popType);
|
||||
}
|
||||
else if(itemData.asLinear() != null) {
|
||||
setterVal = populators.getLinear().create(itemData, popType);
|
||||
}
|
||||
else if(itemData.asMapped() != null) {
|
||||
setterVal = populators.getMapped().create(itemData, popType);
|
||||
}
|
||||
|
||||
if(setterVal == null) {
|
||||
L.info(() -> String.format("Cannot set using: %s:%s",
|
||||
targetObj.getClass().getName(), method.getName()));
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
method.invoke(targetObj, setterVal);
|
||||
method.setAccessible(false);
|
||||
retstat = Status.SUCCESS;
|
||||
}
|
||||
catch (ReflectiveOperationException exep) {
|
||||
if (L.isLoggable(Level.WARNING)) {
|
||||
L.log(Level.WARNING, exep.getMessage(), exep);
|
||||
}
|
||||
throw new ConvertException(targetObj.getClass().getName(), method.getName(),
|
||||
"Error invoking setter for linear data item - " + exep.getMessage(), exep);
|
||||
}
|
||||
}
|
||||
return retstat;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
import daxi.moe.app.ConvertException;
|
||||
import daxi.moe.model.AnyData;
|
||||
import daxi.moe.model.MappedData;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class MappedPopulator implements Populator {
|
||||
|
||||
private static final Logger L = Logger.getLogger(MappedPopulator.class.getName());
|
||||
|
||||
private final Populators populators;
|
||||
|
||||
public MappedPopulator(Populators populators) {
|
||||
this.populators = populators;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface Populator
|
||||
|
||||
@Override
|
||||
public Object create(AnyData data, Class<?> targetType) throws ConvertException {
|
||||
MappedData mdata = data.asMapped();
|
||||
if(mdata == null || targetType == null) {
|
||||
L.warning(() -> "Invalid arguments: data=" + mdata + ", targetType=" + targetType);
|
||||
return null;
|
||||
}
|
||||
|
||||
TargetEntity entity = new TargetEntity(targetType);
|
||||
if (L.isLoggable(Level.FINEST)) {
|
||||
L.finest(entity.dumpInfo());
|
||||
}
|
||||
|
||||
if(entity.isInvalid()) {
|
||||
if (L.isLoggable(Level.WARNING)) {
|
||||
L.warning("Target type is not valid: " + targetType);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Object target;
|
||||
try {
|
||||
// Attempt to create a new instance of the target type using its public and default
|
||||
// constructor
|
||||
Constructor<?> ctor = targetType.getConstructor();
|
||||
target = ctor.newInstance();
|
||||
}
|
||||
catch(NoSuchMethodException exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.log(Level.WARNING, exep.getMessage(), exep);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
catch(Exception exep) {
|
||||
throw new ConvertException(targetType.getName(), "<init>",
|
||||
"Error invoking default constructor - " + exep.getMessage(), exep);
|
||||
}
|
||||
|
||||
for(String dataKey : mdata.getKeys()) {
|
||||
AnyData nestedData = mdata.get(dataKey);
|
||||
if (nestedData == null) {
|
||||
continue; // this should not happen, but just in case
|
||||
}
|
||||
|
||||
Status status = Status.NOT_VALID;
|
||||
|
||||
if(nestedData.asSimple() != null) {
|
||||
status = populators.getSimple().populate(nestedData, dataKey, entity, target);
|
||||
}
|
||||
else if(nestedData.asLinear() != null) {
|
||||
status = populators.getLinear().populate(nestedData, dataKey, entity, target);
|
||||
}
|
||||
else if(nestedData.asMapped() != null) {
|
||||
status = populate(nestedData, dataKey, entity, target);
|
||||
}
|
||||
|
||||
if(status != Status.SUCCESS) {
|
||||
L.info(() -> String.format("key = %s, type = %s Cannot set or add", dataKey,
|
||||
target.getClass().getName()));
|
||||
}
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Status populate(AnyData data, String dataKey,
|
||||
TargetEntity entity, Object targetObj) throws ConvertException {
|
||||
MappedData mdata = data.asMapped();
|
||||
if(mdata == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
|
||||
if(entity.hasSetters()) {
|
||||
return doSetter(data, dataKey, entity, targetObj);
|
||||
}
|
||||
else {
|
||||
return doBlankMap(data, dataKey, entity, targetObj);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helper methods
|
||||
|
||||
private Status doSetter(AnyData data, String dataKey,
|
||||
TargetEntity entity, Object targetObj) throws ConvertException {
|
||||
Setter setter = entity.getSetter(dataKey);
|
||||
if(setter == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
|
||||
Object paramVal = create(data, setter.getInputType());
|
||||
if(paramVal == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
|
||||
Method method = setter.getMethod();
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
method.invoke(targetObj, paramVal);
|
||||
method.setAccessible(false);
|
||||
}
|
||||
catch (ReflectiveOperationException exep) {
|
||||
if (L.isLoggable(Level.WARNING)) {
|
||||
L.log(Level.WARNING, exep.getMessage(), exep);
|
||||
}
|
||||
throw new ConvertException(targetObj.getClass().getName(), method.getName(),
|
||||
"Error invoking setter - " + exep.getMessage(), exep);
|
||||
}
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
|
||||
private Status doBlankMap(AnyData data, String dataKey,
|
||||
TargetEntity entity, Object targetObj) throws ConvertException {
|
||||
|
||||
BlankMapped bmapped = entity.getBlankMapped();
|
||||
if(bmapped == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
|
||||
Object paramVal = create(data, bmapped.getInputType());
|
||||
if(paramVal == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
|
||||
Method method = bmapped.getMethod();
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
method.invoke(targetObj, dataKey, paramVal);
|
||||
method.setAccessible(false);
|
||||
}
|
||||
catch (ReflectiveOperationException exep) {
|
||||
if (L.isLoggable(Level.WARNING)) {
|
||||
L.log(Level.WARNING, exep.getMessage(), exep);
|
||||
}
|
||||
throw new ConvertException(targetObj.getClass().getName(), method.getName(),
|
||||
"Error invoking blank mapped method - " + exep.getMessage(), exep);
|
||||
}
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
import daxi.moe.app.ConvertException;
|
||||
import daxi.moe.model.AnyData;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public interface Populator {
|
||||
|
||||
/**
|
||||
* Converts an {@link AnyData} to an object of the specified target type. The anydata encapsulates
|
||||
* any one of a simple, linear or mapped data.
|
||||
*
|
||||
* @param data one of a simple, linear or mapped data to convert
|
||||
* @param targetType the target type to convert to
|
||||
* @return an object of the specified target type
|
||||
* @throws ConvertException if the conversion fails
|
||||
*/
|
||||
|
||||
Object create(AnyData data, Class<?> targetType) throws ConvertException;
|
||||
|
||||
/**
|
||||
* Populates a target Java object using any data by invoking the provided method on the object. The
|
||||
* anydata encapsulates any one of a simple, linear or mapped data.
|
||||
*
|
||||
* @param data one of a simple, linear or mapped data
|
||||
* @param dataKey The key associated with the any data.
|
||||
* @param entity the target entity that encapsulates the target object and the method to invoke
|
||||
* @param targetObj the target Java object to populate
|
||||
* @return a {@link Status} indicating the result of the population operation
|
||||
* @throws ConvertException if the population operation fails
|
||||
*/
|
||||
|
||||
Status populate(AnyData data, String dataKey,
|
||||
TargetEntity entity, Object targetObj) throws ConvertException;
|
||||
}
|
||||
+21
-22
@@ -12,45 +12,44 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package daxi.moe.model;
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
import daxi.moe.app.Moe;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class AnyData {
|
||||
public class Populators {
|
||||
|
||||
private final SimpleData simple;
|
||||
private final LinearData linear;
|
||||
private final MappedData mapped;
|
||||
private final List<Moe.Options> options;
|
||||
|
||||
public AnyData(SimpleData simple) {
|
||||
this.simple = simple;
|
||||
this.linear = null;
|
||||
this.mapped = null;
|
||||
private final Populator simple;
|
||||
private final Populator linear;
|
||||
private final Populator mapped;
|
||||
|
||||
public Populators(List<Moe.Options> options) {
|
||||
this.options = options;
|
||||
simple = new SimplePopulator();
|
||||
linear = new LinearPopulator(this);
|
||||
mapped = new MappedPopulator(this);
|
||||
}
|
||||
|
||||
public AnyData(LinearData linear) {
|
||||
this.simple = null;
|
||||
this.linear = linear;
|
||||
this.mapped = null;
|
||||
public boolean hasOption(Moe.Options opt) {
|
||||
return options.contains(opt);
|
||||
}
|
||||
|
||||
public AnyData(MappedData mapped) {
|
||||
this.simple = null;
|
||||
this.linear = null;
|
||||
this.mapped = mapped;
|
||||
}
|
||||
|
||||
public SimpleData asSimple() {
|
||||
public Populator getSimple() {
|
||||
return simple;
|
||||
}
|
||||
|
||||
public LinearData asLinear() {
|
||||
public Populator getLinear() {
|
||||
return linear;
|
||||
}
|
||||
|
||||
public MappedData asMapped() {
|
||||
public Populator getMapped() {
|
||||
return mapped;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
|
||||
/**
|
||||
* A Setter is a method that takes a single input parameter and returns void. It is used to set the
|
||||
* value of a property on an object.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class Setter extends FromMethod {
|
||||
|
||||
private final Class<?> inType;
|
||||
|
||||
protected Setter(Method method) throws IllegalArgumentException {
|
||||
super(method);
|
||||
|
||||
if(getDataKey().isBlank()) {
|
||||
// If the data key is blank, it is not a setter method
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
Parameter[] params = method.getParameters();
|
||||
if(params.length != 1) {
|
||||
// If the method does not have exactly one parameter, it is not valid for conversion
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
Type type = params[0].getParameterizedType();
|
||||
if(type instanceof TypeVariable<?> || type instanceof ParameterizedType) {
|
||||
// If the parameter type is a type variable or a parameterized type, it is not valid
|
||||
// for conversion
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
inType = params[0].getType();
|
||||
if(inType.isArray()) {
|
||||
// If the parameter type is an array, it is not valid for conversion
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of base class FromMethod
|
||||
|
||||
@Override
|
||||
public Class<?> getInputType() {
|
||||
return inType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s(%s)", getMethod().getName(), inType.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Setter object if the provided method is valid for conversion, or null if it is
|
||||
* not valid.
|
||||
*
|
||||
* @param method the method to be checked
|
||||
* @return the Setter instance or null if the method does not meet valid setter semantics
|
||||
*/
|
||||
|
||||
public static Setter of(Method method) {
|
||||
try {
|
||||
return new Setter(method);
|
||||
}
|
||||
catch (IllegalArgumentException exep) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,347 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
import daxi.moe.app.ConvertException;
|
||||
import daxi.moe.model.AnyData;
|
||||
import daxi.moe.model.SimpleData;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class SimplePopulator implements Populator {
|
||||
|
||||
private static final Logger L = Logger.getLogger(SimplePopulator.class.getName());
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface Populator
|
||||
|
||||
@Override
|
||||
public Object create(AnyData data, Class<?> targetType) throws ConvertException {
|
||||
SimpleData sdata = data.asSimple();
|
||||
if(sdata == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Object targetObj = null;
|
||||
if(targetType == String.class) {
|
||||
targetObj = sdata.getString();
|
||||
}
|
||||
|
||||
if(targetType == BigInteger.class) {
|
||||
targetObj = getBigIntegerValue(sdata);
|
||||
}
|
||||
|
||||
if(targetType == BigDecimal.class) {
|
||||
targetObj = getBigDecimalValue(sdata);
|
||||
}
|
||||
|
||||
if(targetType == byte.class || targetType == Byte.class) {
|
||||
targetObj = getByteValue(sdata);
|
||||
}
|
||||
|
||||
if(targetType == short.class || targetType == Short.class) {
|
||||
targetObj = getShortValue(sdata);
|
||||
}
|
||||
|
||||
if(targetType == int.class || targetType == Integer.class) {
|
||||
targetObj = getIntegerValue(sdata);
|
||||
}
|
||||
|
||||
if(targetType == long.class || targetType == Long.class) {
|
||||
targetObj = getLongValue(sdata);
|
||||
}
|
||||
|
||||
if(targetType == float.class || targetType == Float.class) {
|
||||
targetObj = getFloatValue(sdata);
|
||||
}
|
||||
|
||||
if(targetType == double.class || targetType == Double.class) {
|
||||
targetObj = getDoubleValue(sdata);
|
||||
}
|
||||
|
||||
if(targetType == boolean.class || targetType == Boolean.class) {
|
||||
targetObj = getBooleanValue(sdata);
|
||||
}
|
||||
|
||||
return targetObj;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Status populate(AnyData data, String dataKey,
|
||||
TargetEntity entity, Object targetObj) throws ConvertException {
|
||||
SimpleData sdata = data.asSimple();
|
||||
if(sdata == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
if(entity.hasSetters()) {
|
||||
return doSetter(data, dataKey, entity, targetObj);
|
||||
}
|
||||
else {
|
||||
return doBlankMap(data, dataKey, entity, targetObj);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helper methods
|
||||
|
||||
private BigInteger getBigIntegerValue(SimpleData data) {
|
||||
String strVal = data.getString();
|
||||
if(strVal == null) {
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
strVal = nval.toString();
|
||||
}
|
||||
}
|
||||
if(strVal != null) {
|
||||
strVal = strVal.split("\\.")[0];
|
||||
try {
|
||||
return new BigInteger(strVal);
|
||||
}
|
||||
catch(Exception exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning(() -> "Unable to convert [" + data + "] to BigInteger");
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private BigDecimal getBigDecimalValue(SimpleData data) {
|
||||
String strVal = data.getString();
|
||||
if(strVal == null) {
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
strVal = nval.toString();
|
||||
}
|
||||
}
|
||||
if(strVal != null) {
|
||||
try {
|
||||
return new BigDecimal(strVal);
|
||||
}
|
||||
catch(Exception exep) {
|
||||
if(L.isLoggable(Level.WARNING)) {
|
||||
L.warning(() -> "Unable to convert [" + data + "] to BigDecimal");
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Byte getByteValue(SimpleData data) {
|
||||
Byte paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.byteValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Byte.parseByte(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
L.warning(() -> "Unable to convert string [" + sval + "] to byte");
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Short getShortValue(SimpleData data) {
|
||||
Short paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.shortValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Short.parseShort(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
L.warning(() -> "Unable to convert string [" + sval + "] to short");
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Integer getIntegerValue(SimpleData data) {
|
||||
Integer paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.intValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Integer.parseInt(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
L.warning(() -> "Unable to convert string [" + sval + "] to integer");
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Long getLongValue(SimpleData data) {
|
||||
Long paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.longValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Long.parseLong(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
L.warning(() -> "Unable to convert string [" + sval + "] to long");
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Float getFloatValue(SimpleData data) {
|
||||
Float paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.floatValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Float.parseFloat(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
L.warning(() -> "Unable to convert string [" + sval + "] to float");
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Double getDoubleValue(SimpleData data) {
|
||||
Double paramVal = null;
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.doubleValue();
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
try {
|
||||
paramVal = Double.parseDouble(sval);
|
||||
}
|
||||
catch(NumberFormatException exep) {
|
||||
L.warning(() -> "Unable to convert string [" + sval + "] to double");
|
||||
}
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Boolean getBooleanValue(SimpleData data) {
|
||||
Boolean paramVal = data.getBoolean();
|
||||
if(paramVal != null) {
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
Number nval = data.getNumber();
|
||||
if(nval != null) {
|
||||
paramVal = nval.intValue() != 0;
|
||||
}
|
||||
else {
|
||||
String sval = data.getString();
|
||||
if(sval != null) {
|
||||
paramVal = Boolean.parseBoolean(sval.toLowerCase(Locale.getDefault()));
|
||||
}
|
||||
}
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
private Status doSetter(AnyData data, String dataKey,
|
||||
TargetEntity entity, Object targetObj) throws ConvertException {
|
||||
Setter setter = entity.getSetter(dataKey);
|
||||
if(setter == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
|
||||
Object paramVal = create(data, setter.getInputType());
|
||||
|
||||
if(paramVal == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
|
||||
Method method = setter.getMethod();
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
method.invoke(targetObj, paramVal);
|
||||
method.setAccessible(false);
|
||||
}
|
||||
catch (ReflectiveOperationException exep) {
|
||||
if (L.isLoggable(Level.WARNING)) {
|
||||
L.log(Level.WARNING, exep.getMessage(), exep);
|
||||
}
|
||||
throw new ConvertException(targetObj.getClass().getName(), method.getName(),
|
||||
"Error invoking setter - " + exep.getMessage(), exep);
|
||||
}
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
|
||||
private Status doBlankMap(AnyData data, String dataKey,
|
||||
TargetEntity entity, Object targetObj) throws ConvertException {
|
||||
|
||||
BlankMapped bmapped = entity.getBlankMapped();
|
||||
if(bmapped == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
|
||||
Object paramVal = create(data, bmapped.getInputType());
|
||||
if(paramVal == null) {
|
||||
return Status.NOT_VALID;
|
||||
}
|
||||
|
||||
Method method = bmapped.getMethod();
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
method.invoke(targetObj, dataKey, paramVal);
|
||||
method.setAccessible(false);
|
||||
}
|
||||
catch (ReflectiveOperationException exep) {
|
||||
if (L.isLoggable(Level.WARNING)) {
|
||||
L.log(Level.WARNING, exep.getMessage(), exep);
|
||||
}
|
||||
throw new ConvertException(targetObj.getClass().getName(), method.getName(),
|
||||
"Error invoking blank mapped method - " + exep.getMessage(), exep);
|
||||
}
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
+14
-6
@@ -12,17 +12,25 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.transform;
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
/**
|
||||
* Defines the possible statuses for a transformation. The transformation can be from the model data
|
||||
* to Java objects or vice versa.
|
||||
* Reports whether a converter could handle the supplied data and whether conversion succeeded.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public enum M2JStatus {
|
||||
public enum Status {
|
||||
|
||||
/**
|
||||
* The converter did not handle the supplied data.
|
||||
*/
|
||||
|
||||
NOT_VALID,
|
||||
SUCCESS,
|
||||
ERROR
|
||||
|
||||
/**
|
||||
* The conversion completed successfully.
|
||||
*/
|
||||
|
||||
SUCCESS
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.mod2obj;
|
||||
|
||||
import daxi.moe.app.ConvertException;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* A TargetEntity is a representation of a target class that can be populated with data from a
|
||||
* model. It contains information about the setters and blank methods available in the target class.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class TargetEntity {
|
||||
|
||||
private static final Logger L = Logger.getLogger(TargetEntity.class.getName());
|
||||
|
||||
private final Class<?> targetType;
|
||||
private final Map<String, Setter> setters;
|
||||
private BlankLinear blankLinear;
|
||||
private BlankMapped blankMapped;
|
||||
|
||||
public TargetEntity(Class<?> targetType) throws ConvertException {
|
||||
this.targetType = targetType;
|
||||
setters = new HashMap<>();
|
||||
collateAllMethods(targetType);
|
||||
}
|
||||
|
||||
//TODO: remove dead code
|
||||
/*
|
||||
public TargetEntity(Object targetObj) throws ConvertException {
|
||||
this(targetObj.getClass());
|
||||
}
|
||||
*/
|
||||
|
||||
public boolean isInvalid() {
|
||||
return setters.isEmpty() && blankLinear == null && blankMapped == null;
|
||||
}
|
||||
|
||||
public boolean hasSetters() {
|
||||
return !setters.isEmpty();
|
||||
}
|
||||
|
||||
public Setter getSetter(String dataKey) {
|
||||
return setters.get(dataKey);
|
||||
}
|
||||
|
||||
public BlankLinear getBlankLinear() {
|
||||
return blankLinear;
|
||||
}
|
||||
|
||||
public BlankMapped getBlankMapped() {
|
||||
return blankMapped;
|
||||
}
|
||||
|
||||
public String dumpInfo() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(String.format("TargetEntity: %s\n", targetType.getName()));
|
||||
sb.append("=== START DUMP ===\n");
|
||||
sb.append("Setters:\n");
|
||||
for (Map.Entry<String, Setter> entry : setters.entrySet()) {
|
||||
sb.append(" ").append(entry.getKey()).append(" -> ").append(entry.getValue()).append("\n");
|
||||
}
|
||||
sb.append("BlankLinear: ").append(blankLinear).append("\n");
|
||||
sb.append("BlankMapped: ").append(blankMapped).append("\n");
|
||||
sb.append("=== END DUMP ===\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helper methods
|
||||
|
||||
private void collateAllMethods(Class<?> type) throws ConvertException {
|
||||
|
||||
if(type.getClassLoader() == null) {
|
||||
// Skip system classes
|
||||
return;
|
||||
}
|
||||
|
||||
for (Method method : type.getDeclaredMethods()) {
|
||||
Setter sm = Setter.of(method);
|
||||
if(sm != null) {
|
||||
if(setters.containsKey(sm.getDataKey())) {
|
||||
throw new ConvertException(type.getName(), sm.getMethod().getName(),
|
||||
"Duplicate setter for data key: " + sm.getDataKey(), null);
|
||||
}
|
||||
setters.put(sm.getDataKey(), sm);
|
||||
continue;
|
||||
}
|
||||
|
||||
//else
|
||||
BlankLinear bkl = BlankLinear.of(method);
|
||||
if(bkl != null) {
|
||||
if(blankLinear == null) {
|
||||
blankLinear = bkl;
|
||||
}
|
||||
else {
|
||||
throw new ConvertException(type.getName(), bkl.getMethod().getName(),
|
||||
"Duplicate blank linear method: " + bkl.getMethod().getName(), null);
|
||||
}
|
||||
}
|
||||
|
||||
//else
|
||||
BlankMapped bkm = BlankMapped.of(method);
|
||||
if(bkm != null) {
|
||||
if(blankMapped == null) {
|
||||
blankMapped = bkm;
|
||||
}
|
||||
else {
|
||||
throw new ConvertException(type.getName(), bkm.getMethod().getName(),
|
||||
"Duplicate blank mapped method: " + bkm.getMethod().getName(), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type.getSuperclass() != null) {
|
||||
collateAllMethods(type.getSuperclass());
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
-15
@@ -10,24 +10,22 @@
|
||||
* This file 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.
|
||||
*
|
||||
*/
|
||||
|
||||
package daxi.moe.lib;
|
||||
package daxi.moe.lib.obj2mod;
|
||||
|
||||
import daxi.moe.app.Internal;
|
||||
import daxi.moe.app.Moe;
|
||||
import daxi.moe.app.MoeException;
|
||||
import daxi.moe.codec.MoeCodec;
|
||||
import daxi.moe.codec.MoeEncoder;
|
||||
import daxi.moe.model.AnyData;
|
||||
import daxi.moe.model.MappedData;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
@@ -38,21 +36,26 @@ public class InternalImpl implements Internal {
|
||||
private final MoeEncoder encoder;
|
||||
private final List<Moe.Options> options;
|
||||
|
||||
//private final M2JGlobals globals;
|
||||
private final Retrievers retrievers;
|
||||
|
||||
private AnyData model;
|
||||
|
||||
InternalImpl(MoeCodec provider, List<Moe.Options> options) {
|
||||
encoder = provider.encoder();
|
||||
public InternalImpl(MoeCodec codec, List<Moe.Options> options) {
|
||||
encoder = codec.encoder();
|
||||
this.options = options;
|
||||
|
||||
//globals = new M2JGlobals(options);
|
||||
|
||||
retrievers = new Retrievers(options);
|
||||
model = new AnyData(new MappedData());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface Internal
|
||||
|
||||
@Override
|
||||
public Internal from(Object obj) {
|
||||
public Internal from(Object obj) throws MoeException {
|
||||
Retriever retriever = retrievers.getRetriever(obj);
|
||||
if(retriever != null) {
|
||||
model = retriever.transform(obj);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -62,12 +65,12 @@ public class InternalImpl implements Internal {
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] convert() throws IOException {
|
||||
return new byte[0];
|
||||
public byte[] convert() throws MoeException {
|
||||
return encoder.convert(model, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] convert(boolean pretty) throws IOException {
|
||||
return new byte[0];
|
||||
public byte[] convert(boolean pretty) throws MoeException {
|
||||
return encoder.convert(model, pretty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.obj2mod;
|
||||
|
||||
import daxi.moe.app.ConvertException;
|
||||
import daxi.moe.model.AnyData;
|
||||
import daxi.moe.model.LinearData;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class LinearRetriever implements Retriever {
|
||||
|
||||
private static final Logger L = Logger.getLogger(LinearRetriever.class.getName());
|
||||
|
||||
private final Retrievers retrievers;
|
||||
|
||||
public LinearRetriever(Retrievers retrievers) {
|
||||
this.retrievers = retrievers;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface Retriever
|
||||
|
||||
@Override
|
||||
public boolean supports(Object targetObj) {
|
||||
Class<?> tcls = targetObj.getClass();
|
||||
return Collection.class.isAssignableFrom(tcls) || tcls.isArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnyData transform(Object targetObj) throws ConvertException {
|
||||
Class<?> tcls = targetObj.getClass();
|
||||
|
||||
LinearData ldata = new LinearData();
|
||||
if(Collection.class.isAssignableFrom(tcls)) {
|
||||
for (Object item : (Collection<?>) targetObj) {
|
||||
AnyData idata = transformItem(item);
|
||||
if (idata != null) {
|
||||
ldata.add(idata);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(tcls.isArray()) {
|
||||
Class<?> compType = tcls.getComponentType();
|
||||
int length = Array.getLength(targetObj);
|
||||
for(int i = 0; i < length; i++) {
|
||||
AnyData idata = transformItem(Array.get(targetObj, i));
|
||||
if(idata != null) {
|
||||
ldata.add(idata);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new AnyData(ldata);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helper methods
|
||||
|
||||
private AnyData transformItem(Object item) throws ConvertException {
|
||||
Retriever retriever = retrievers.getRetriever(item);
|
||||
if(retriever != null) {
|
||||
return retriever.transform(item);
|
||||
}
|
||||
else {
|
||||
L.warning(() -> "Unsupported item - " + item.getClass().getName());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.obj2mod;
|
||||
|
||||
import daxi.moe.app.ConvertException;
|
||||
import daxi.moe.model.AnyData;
|
||||
import daxi.moe.model.MappedData;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class MappedRetriever implements Retriever {
|
||||
|
||||
private static final Logger L = Logger.getLogger(MappedRetriever.class.getName());
|
||||
|
||||
private final Retrievers retrievers;
|
||||
|
||||
public MappedRetriever(Retrievers retrievers) {
|
||||
this.retrievers = retrievers;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface Retriever
|
||||
|
||||
@Override
|
||||
public boolean supports(Object targetObj) {
|
||||
Class<?> tcls = targetObj.getClass();
|
||||
if(tcls.getClassLoader() == null) {
|
||||
// This is a system class, we don't support it.
|
||||
return false;
|
||||
}
|
||||
/*
|
||||
if(targetObj instanceof String
|
||||
|| targetObj instanceof Number || targetObj instanceof Boolean) {
|
||||
// This is a String, Number, or Boolean, we don't support it.
|
||||
return false;
|
||||
}
|
||||
if(Collection.class.isAssignableFrom(tcls)) {
|
||||
// This is a Collection, we don't support it.
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
if(tcls.isArray()) {
|
||||
// This is an array, we don't support it.
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnyData transform(Object targetObj) throws ConvertException {
|
||||
|
||||
SourceEntity sent = new SourceEntity(targetObj);
|
||||
Set<String> dataKeys = sent.getDataKeys();
|
||||
if(dataKeys == null || dataKeys.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
MappedData mdata = new MappedData();
|
||||
|
||||
for(String dataKey : dataKeys) {
|
||||
ToMethod tm = sent.getToMethod(dataKey);
|
||||
Method mthd = tm.getMethod();
|
||||
Object retVal = null;
|
||||
final Class<?> rtnType = tm.getReturnType();
|
||||
try {
|
||||
mthd.setAccessible(true);
|
||||
retVal = mthd.invoke(targetObj);
|
||||
mthd.setAccessible(false);
|
||||
}
|
||||
catch (ReflectiveOperationException exep) {
|
||||
if (L.isLoggable(Level.WARNING)) {
|
||||
L.log(Level.WARNING, exep.getMessage(), exep);
|
||||
}
|
||||
throw new ConvertException(targetObj.getClass().getName(), mthd.getName(),
|
||||
"Error invoking getter - " + exep.getMessage(), exep);
|
||||
}
|
||||
|
||||
Retriever retriever = retrievers.getRetriever(retVal);
|
||||
if(retriever != null) {
|
||||
AnyData adata = retriever.transform(retVal);
|
||||
if(adata != null) {
|
||||
mdata.put(dataKey, adata);
|
||||
}
|
||||
}
|
||||
else {
|
||||
L.warning(() -> "Unsupported return type - " + rtnType.getName());
|
||||
}
|
||||
}
|
||||
return new AnyData(mdata);
|
||||
}
|
||||
}
|
||||
+5
-6
@@ -12,19 +12,18 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package daxi.moe.app;
|
||||
package daxi.moe.lib.obj2mod;
|
||||
|
||||
import daxi.moe.app.ConvertException;
|
||||
import daxi.moe.model.AnyData;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public interface Converter {
|
||||
public interface Retriever {
|
||||
|
||||
<T> T decode(AnyData ad, Class<T> type);
|
||||
boolean supports(Object targetObj);
|
||||
|
||||
Optional<AnyData> encode(Object obj);
|
||||
AnyData transform(Object targetObj) throws ConvertException;
|
||||
}
|
||||
+22
-7
@@ -12,23 +12,38 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package daxi.moe.codec;
|
||||
package daxi.moe.lib.obj2mod;
|
||||
|
||||
import daxi.moe.app.Moe;
|
||||
import daxi.moe.model.AnyData;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public interface MoeDecoder {
|
||||
public class Retrievers {
|
||||
|
||||
void setSource(byte[] data);
|
||||
private final Retriever simple;
|
||||
private final Retriever linear;
|
||||
private final Retriever mapped;
|
||||
|
||||
void setSource(InputStream input);
|
||||
public Retrievers(List<Moe.Options> options) {
|
||||
simple = new SimpleRetriever();
|
||||
linear = new LinearRetriever(this);
|
||||
mapped = new MappedRetriever(this);
|
||||
}
|
||||
|
||||
AnyData decode(List<Moe.Options> options);
|
||||
public Retriever getRetriever(Object targetObj) {
|
||||
if(simple.supports(targetObj)) {
|
||||
return simple;
|
||||
}
|
||||
else if(linear.supports(targetObj)) {
|
||||
return linear;
|
||||
}
|
||||
else if(mapped.supports(targetObj)) {
|
||||
return mapped;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.obj2mod;
|
||||
|
||||
import daxi.moe.app.ConvertException;
|
||||
import daxi.moe.model.AnyData;
|
||||
import daxi.moe.model.SimpleData;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class SimpleRetriever implements Retriever {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Methods of interface Retriever
|
||||
|
||||
@Override
|
||||
public boolean supports(Object targetObj) {
|
||||
return targetObj instanceof String || targetObj instanceof StringBuilder
|
||||
|| targetObj instanceof StringBuffer || targetObj instanceof Number
|
||||
|| targetObj instanceof Boolean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnyData transform(Object targetObj) throws ConvertException {
|
||||
SimpleData sdata = null;
|
||||
if(targetObj instanceof CharSequence) {
|
||||
sdata = new SimpleData(targetObj.toString());
|
||||
}
|
||||
else if(targetObj instanceof Number) {
|
||||
sdata = new SimpleData((Number) targetObj);
|
||||
}
|
||||
else if(targetObj instanceof Boolean) {
|
||||
sdata = new SimpleData((Boolean) targetObj);
|
||||
}
|
||||
return new AnyData(sdata);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.obj2mod;
|
||||
|
||||
import daxi.moe.app.ConvertException;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class SourceEntity {
|
||||
|
||||
private static final Logger L = Logger.getLogger(SourceEntity.class.getName());
|
||||
|
||||
private final Class<?> sourceType;
|
||||
private final Map<String, ToMethod> toMethods;
|
||||
|
||||
public SourceEntity(Class<?> sourceType) throws ConvertException {
|
||||
this.sourceType = sourceType;
|
||||
toMethods = new HashMap<>();
|
||||
collateAllMethods(sourceType);
|
||||
}
|
||||
|
||||
public SourceEntity(Object srcObj) throws ConvertException {
|
||||
this(srcObj.getClass());
|
||||
}
|
||||
|
||||
public Class<?> getSourceType() {
|
||||
return sourceType;
|
||||
}
|
||||
|
||||
public Set<String> getDataKeys() {
|
||||
return toMethods.keySet();
|
||||
}
|
||||
|
||||
public ToMethod getToMethod(String dataKey) {
|
||||
return toMethods.get(dataKey);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Helper methods
|
||||
|
||||
private void collateAllMethods(Class<?> type) throws ConvertException {
|
||||
|
||||
if(type.getClassLoader() == null) {
|
||||
// Skip system classes
|
||||
return;
|
||||
}
|
||||
|
||||
for (Method method : type.getDeclaredMethods()) {
|
||||
ToMethod tm = ToMethod.of(method);
|
||||
if(tm != null) {
|
||||
if(toMethods.containsKey(tm.getDataKey())) {
|
||||
throw new ConvertException(type.getName(), tm.getMethod().getName(),
|
||||
"Duplicate getters for data key: " + tm.getDataKey(), null);
|
||||
}
|
||||
toMethods.put(tm.getDataKey(), tm);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (type.getSuperclass() != null) {
|
||||
collateAllMethods(type.getSuperclass());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.obj2mod;
|
||||
|
||||
import daxi.moe.app.MapTo;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Parameter;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class ToMethod {
|
||||
|
||||
private final Method method;
|
||||
private final String dataKey;
|
||||
private final Class<?> returnType;
|
||||
|
||||
public ToMethod(Method method) {
|
||||
this.method = method;
|
||||
MapTo mapping = method.getAnnotation(MapTo.class);
|
||||
if(mapping == null) {
|
||||
// If the method does not have the @MapTo annotation, it is not valid for retrieval
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
// populate the dataKey field with the value of the @MapTo annotation
|
||||
dataKey = mapping.value().trim();
|
||||
if(dataKey.isBlank()) {
|
||||
// If the data key is blank, it is not a getter method
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
int mod = method.getModifiers();
|
||||
// If the method is abstract, static, or not private, it is not valid for retrieval
|
||||
if(Modifier.isAbstract(mod) || Modifier.isStatic(mod) || !Modifier.isPrivate(mod)) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
Parameter[] params = method.getParameters();
|
||||
if(params.length > 0) {
|
||||
// If the method takes one or more parameters, it is not valid for retrieval
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
returnType = method.getReturnType();
|
||||
if(returnType == Void.TYPE) {
|
||||
// If the method returns void, it is not valid for retrieval
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
public final String getDataKey() {
|
||||
return dataKey;
|
||||
}
|
||||
|
||||
public final Method getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public final Class<?> getReturnType() {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
public static ToMethod of(Method method) {
|
||||
try {
|
||||
return new ToMethod(method);
|
||||
}
|
||||
catch (IllegalArgumentException exep) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.transform;
|
||||
|
||||
import daxi.moe.app.Moe;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A registry for model-to-Java transformers as singletons. It also stores options that were set
|
||||
* during instatiation of MOE.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class M2JGlobals {
|
||||
|
||||
private final List<Moe.Options> options;
|
||||
|
||||
private M2JSimple simM2j;
|
||||
private M2JLinear linM2j;
|
||||
private M2JMapped mapM2j;
|
||||
private M2JObject objM2j;
|
||||
|
||||
public M2JGlobals(List<Moe.Options> options) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
public void setM2J(M2JSimple m2j) {
|
||||
simM2j = m2j;
|
||||
}
|
||||
|
||||
public void setM2J(M2JLinear m2j) {
|
||||
linM2j = m2j;
|
||||
}
|
||||
|
||||
public void setM2J(M2JMapped m2j) {
|
||||
mapM2j = m2j;
|
||||
}
|
||||
|
||||
public void setM2J(M2JObject m2j) {
|
||||
objM2j = m2j;
|
||||
}
|
||||
|
||||
public boolean hasOption(Moe.Options opt) {
|
||||
return options.contains(opt);
|
||||
}
|
||||
|
||||
public M2JSimple getM2JSimple() {
|
||||
return simM2j;
|
||||
}
|
||||
|
||||
public M2JLinear getM2JLinear() {
|
||||
return linM2j;
|
||||
}
|
||||
|
||||
public M2JMapped getM2JMapped() {
|
||||
return mapM2j;
|
||||
}
|
||||
|
||||
public M2JObject getM2JObject() {
|
||||
return objM2j;
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.transform;
|
||||
|
||||
import daxi.moe.model.MappedData;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Defines a marker interface for classes that can be used to populate portions of a Java object
|
||||
* with {@link daxi.moe.model.LinearData}.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public interface M2JLinear {
|
||||
|
||||
/**
|
||||
* Converts a linear data from the given mapped data into the target object using the specified
|
||||
* method.
|
||||
*
|
||||
* @param data The mapped data that contains the linear data to convert.
|
||||
* @param targetObj The target object to populate with the converted data.
|
||||
* @param targetMthd The method to use for populating the target object.
|
||||
* @return The status of the conversion.
|
||||
*/
|
||||
|
||||
M2JStatus convert(MappedData data, Object targetObj, Method targetMthd);
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.transform;
|
||||
|
||||
import daxi.moe.model.MappedData;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Defines a marker interface for classes that can be used to populate portions of a Java object
|
||||
* with {@link daxi.moe.model.MappedData}.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public interface M2JMapped {
|
||||
|
||||
/**
|
||||
* Converts a nested mapped data contained within a given mapped data into the target object using
|
||||
* the specified method.
|
||||
*
|
||||
* @param data The mapped data that contains the nested mapped data to convert.
|
||||
* @param targetObj The target object to populate with the converted data.
|
||||
* @param targetMthd The method to use for populating the target object.
|
||||
* @return The status of the conversion.
|
||||
*/
|
||||
|
||||
M2JStatus convert(MappedData data, Object targetObj, Method targetMthd);
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.transform;
|
||||
|
||||
import daxi.moe.model.MappedData;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Defines a marker interface for classes that can be used to convert from
|
||||
* {@link daxi.moe.model.MappedData} to a Java object.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public interface M2JObject {
|
||||
|
||||
/**
|
||||
* Populates a Java object with data from a {@link MappedData} instance using the specified method.
|
||||
* @param data the {@link MappedData} instance containing the data to populate the Java object
|
||||
* @param targetObj the Java object to populate
|
||||
* @param targetMthd the method to use for populating the Java object
|
||||
* @return the status of the operation
|
||||
*/
|
||||
|
||||
M2JStatus convert(MappedData data, Object targetObj, Method targetMthd);
|
||||
|
||||
/**
|
||||
* Constructs a Java object of the specified type from the given {@link MappedData} instance.
|
||||
* @param data the {@link MappedData} instance containing the data to populate the Java object
|
||||
* @param targetType the class of the Java object to construct
|
||||
* @param <T> the type of the Java object to construct
|
||||
* @return the constructed Java object of the specified type
|
||||
*/
|
||||
|
||||
<T> T convert(MappedData data, Class<T> targetType);
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.lib.transform;
|
||||
|
||||
import daxi.moe.model.MappedData;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Defines a marker interface for classes that can be used to populate portions of a Java object
|
||||
* with {@link daxi.moe.model.SimpleData}.
|
||||
*
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public interface M2JSimple {
|
||||
|
||||
/**
|
||||
* Converts a linear data from the given mapped data into the target object using the specified
|
||||
* method.
|
||||
*
|
||||
* @param data the mapped data that contains the simple data to convert
|
||||
* @param targetObj the object to populate
|
||||
* @param targetMthd the method to invoke on the target object to set the value
|
||||
* @return The status of the conversion.
|
||||
*/
|
||||
|
||||
M2JStatus convert(MappedData data, Object targetObj, Method targetMthd);
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.model;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class LinearData {
|
||||
|
||||
private final List<AnyData> dataList;
|
||||
|
||||
public LinearData() {
|
||||
dataList = new LinkedList<>();
|
||||
}
|
||||
|
||||
public void add(AnyData value) {
|
||||
dataList.add(value);
|
||||
}
|
||||
|
||||
public AnyData get(int position) {
|
||||
return dataList.get(position);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return dataList.size();
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.model;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class MappedData {
|
||||
|
||||
private final Map<String, AnyData> dataMap;
|
||||
|
||||
public MappedData() {
|
||||
dataMap = new HashMap<>();
|
||||
}
|
||||
|
||||
public List<String> getKeys() {
|
||||
return dataMap.keySet().stream().toList();
|
||||
}
|
||||
|
||||
public boolean contains(String key) {
|
||||
return dataMap.containsKey(key);
|
||||
}
|
||||
|
||||
public AnyData get(String key) {
|
||||
return dataMap.get(key);
|
||||
}
|
||||
|
||||
public void put(String key, AnyData value) {
|
||||
dataMap.put(key, value);
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) DuxTechnica
|
||||
*
|
||||
* Licensed under the DuxTechnica Limited Use Source Code License Agreement (DT-LUSCL); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of the License by
|
||||
* contacting:
|
||||
*
|
||||
* Dux Technica, [email protected]
|
||||
*
|
||||
* This file 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.
|
||||
*/
|
||||
|
||||
package daxi.moe.model;
|
||||
|
||||
/**
|
||||
* @author indroneel
|
||||
*/
|
||||
|
||||
public class SimpleData {
|
||||
|
||||
private final String strVal;
|
||||
private final Number numVal;
|
||||
private final Boolean boolVal;
|
||||
|
||||
public SimpleData(String value) {
|
||||
strVal = value;
|
||||
numVal = null;
|
||||
boolVal = null;
|
||||
}
|
||||
|
||||
public SimpleData(Number value) {
|
||||
strVal = null;
|
||||
numVal = value;
|
||||
boolVal = null;
|
||||
}
|
||||
|
||||
public SimpleData(Boolean value) {
|
||||
strVal = null;
|
||||
numVal = null;
|
||||
boolVal = value;
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return strVal;
|
||||
}
|
||||
|
||||
public Boolean getBoolean() {
|
||||
return boolVal;
|
||||
}
|
||||
|
||||
public Number getNumber() {
|
||||
return numVal;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user