Skip to content

Commit

Permalink
[ADDED] Added NReflectCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
thevpc committed Dec 22, 2024
1 parent e6a6f66 commit 7a908d5
Show file tree
Hide file tree
Showing 12 changed files with 610 additions and 50 deletions.
6 changes: 3 additions & 3 deletions core/nuts-app/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,12 @@
</snapshotRepository>
</distributionManagement>
<properties>
<nuts.categories>/Settings/PackageManager/Nuts</nuts.categories>
<github.global.server>github</github.global.server>
<nuts.api>true</nuts.api>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<nuts.categories>/Settings/PackageManager/Nuts</nuts.categories>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<nuts.genericName>Package Manager</nuts.genericName>
<github.global.server>github</github.global.server>
</properties>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public Object destruct(File src, Type typeOfSrc, NElementFactoryContext context)
@Override
public NElement createElement(File o, Type typeOfSrc, NElementFactoryContext context) {
if (context.isNtf()) {
NSession session = context.getSession();
// NutsText n = ws.text().forStyled(o.toString(), NutsTextStyle.path());
// return ws.elem().forPrimitive().buildNutsString(n);
NText n = NText.ofStyled(o.toString(), NTextStyle.path());
Expand All @@ -33,7 +32,6 @@ public NElement createElement(File o, Type typeOfSrc, NElementFactoryContext con

@Override
public File createObject(NElement o, Type typeOfResult, NElementFactoryContext context) {
NSession session = context.getSession();
return new File(o.asString().get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public NElement createElement(Iterator o, Type typeOfSrc, NElementFactoryContext

@Override
public Iterator createObject(NElement o, Type to, NElementFactoryContext context) {
NSession session = context.getSession();
return o.asArray().get().items().stream().map(x -> context.elementToObject(x, Object.class)).collect(
Collectors.toList()).iterator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class NElementMapperNVersion implements NElementMapper<NVersion> {
@Override
public Object destruct(NVersion src, Type typeOfSrc, NElementFactoryContext context) {
if (context.isNtf()) {
NSession session = context.getSession();
return NFormats.of().ofFormat(src).get().setNtf(true).format();
} else {
return src.toString();
Expand All @@ -31,7 +30,6 @@ public NElement createElement(NVersion o, Type typeOfSrc, NElementFactoryContext

@Override
public NVersion createObject(NElement o, Type typeOfResult, NElementFactoryContext context) {
NSession session = context.getSession();
return NVersion.get(o.asString().get()).get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public Object createObject(NElement o, Type typeOfResult, NElementFactoryContext
if (property.isWrite()) {
NElement v = eobj.get(property.getName()).orNull();
if (v != null) {
property.write(instance, context.elementToObject(v, property.getPropertyType()));
property.write(instance, context.elementToObject(v, property.getPropertyType().getJavaType()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ public abstract class AbstractReflectProperty implements NReflectProperty {

private String name;
private Object cleanInstanceValue;
private Type propertyType;
private NReflectType type;
private NReflectType propertyType;
private NReflectType declaringType;
private NReflectPropertyDefaultValueStrategy defaultValueStrategy;

protected final void init(String name, NReflectType type, Object cleanInstance, Type propertyType, NReflectPropertyDefaultValueStrategy defaultValueStrategy) {
protected final void init(String name, NReflectType declaringType, Object cleanInstance, Type propertyType, NReflectPropertyDefaultValueStrategy defaultValueStrategy) {
this.name = name;
this.cleanInstanceValue = cleanInstance == null ? ReflectUtils.getDefaultValue(propertyType) : read(cleanInstance);
this.type = type;
NReflectType nReflectType = type.getRepository().getType(propertyType)
.replaceVars(t -> type.getActualTypeArgument(t).orElse(t));
this.declaringType = declaringType;
NReflectType nReflectType = declaringType.getRepository().getType(propertyType)
.replaceVars(t -> declaringType.getActualTypeArgument(t).orElse(t));
this.defaultValueStrategy = defaultValueStrategy;
this.propertyType = nReflectType.getJavaType();
this.propertyType = nReflectType;
}

@Override
Expand All @@ -61,12 +61,12 @@ public String getName() {
}

@Override
public NReflectType getType() {
return type;
public NReflectType getDeclaringType() {
return declaringType;
}

@Override
public Type getPropertyType() {
public NReflectType getPropertyType() {
return propertyType;
}

Expand Down Expand Up @@ -128,7 +128,7 @@ public boolean isDefaultValue(Object value, NReflectPropertyDefaultValueStrategy
}
}
case TYPE_DEFAULT: {
return ReflectUtils.isDefaultValue(getPropertyType(), value);
return getPropertyType().isDefaultValue(value);
}
}
return Objects.equals(cleanInstanceValue, value);
Expand All @@ -142,19 +142,19 @@ public boolean isDefaultValue(Object o

@Override
public String toString() {
return String.valueOf(type)+"."+name;
return String.valueOf(declaringType)+"."+name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AbstractReflectProperty that = (AbstractReflectProperty) o;
return Objects.equals(name, that.name) && Objects.equals(cleanInstanceValue, that.cleanInstanceValue) && Objects.equals(propertyType, that.propertyType) && Objects.equals(type, that.type) && defaultValueStrategy == that.defaultValueStrategy;
return Objects.equals(name, that.name) && Objects.equals(cleanInstanceValue, that.cleanInstanceValue) && Objects.equals(propertyType, that.propertyType) && Objects.equals(declaringType, that.declaringType) && defaultValueStrategy == that.defaultValueStrategy;
}

@Override
public int hashCode() {
return Objects.hash(name, cleanInstanceValue, propertyType, type, defaultValueStrategy);
return Objects.hash(name, cleanInstanceValue, propertyType, declaringType, defaultValueStrategy);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package net.thevpc.nuts.runtime.standalone.util.reflect;

import net.thevpc.nuts.reflect.NReflectCopy;
import net.thevpc.nuts.reflect.NReflectType;
import net.thevpc.nuts.util.NEnum;

import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.util.Collection;

class DefaultConvertersByType implements NReflectCopy.Converter {
@Override
public Object convert(Object value, NReflectType fromType, NReflectType toType, NReflectCopy.Context context) {
if (value == null) {
return toType.getDefaultValue();
}
fromType = fromType.getBoxedType().get();
toType = toType.getBoxedType().get();


Type tojType = toType.getJavaType();
if (
toType.getName().equals(fromType.getName())
|| toType.isAssignableFrom(fromType)
) {
// immutable / value objects
switch (fromType.getName()) {
case "java.lang.Boolean":
case "java.lang.Byte":
case "java.lang.Character":
case "java.lang.Short":
case "java.lang.Integer":
case "java.lang.Long":
case "java.lang.Float":
case "java.lang.Double":
case "java.lang.String":
return value;
}
}

//fall through

if (toType.getName().equals("java.lang.String")) {
return String.valueOf(value);
}else if (fromType.getName().equals("java.lang.String")) {
if (tojType instanceof NEnum) {
return NEnum.parse((Class<? extends NEnum>) tojType, (String) value).get();
}
if (tojType instanceof Enum) {
return Enum.valueOf((Class<? extends Enum>) tojType, (String) value);
}
switch (toType.getName()) {
case "java.lang.Boolean":return Boolean.parseBoolean((String) value);
case "java.lang.Character":return ((String) value).charAt(0);
case "java.lang.Byte":return Byte.parseByte((String) value);
case "java.lang.Short":return Short.parseShort((String) value);
case "java.lang.Integer":return Integer.parseInt((String) value);
case "java.lang.Long":return Long.parseLong((String) value);
case "java.lang.Float":return Float.parseFloat((String) value);
case "java.lang.Double":return Double.parseDouble((String) value);
}
} else if (context.getReflectRepository().getType(Number.class).isAssignableFrom(fromType)) {
if (tojType instanceof Enum) {
return ((Class<? extends Enum>) tojType).getEnumConstants()[((Number) value).intValue()];
}else if (tojType instanceof Class && Number.class.isAssignableFrom((Class<?>) tojType)) {
switch (toType.getName()) {
case "java.lang.Byte":return ((Number) value).byteValue();
case "java.lang.Short":return ((Number) value).shortValue();
case "java.lang.Integer":return ((Number) value).intValue();
case "java.lang.Long":return ((Number) value).longValue();
case "java.lang.Float":return ((Number) value).floatValue();
case "java.lang.Double":return ((Number) value).doubleValue();
}
}
} else if (fromType.isArrayType()) {
if (toType.isArrayType()) {
int len = Array.getLength(value);
Object newArr = Array.newInstance((Class<?>) toType.getJavaType(), len);
for (int i = 0; i < len; i++) {
Array.set(newArr, i, Array.get(value, i));
}
return newArr;
} else if (tojType instanceof Class) {
Class<?> cTojType = (Class<?>) tojType;
if (java.util.Collection.class.isAssignableFrom(cTojType)) {
if (cTojType.equals(java.util.Collection.class)) {
cTojType = java.util.ArrayList.class;
} else if (cTojType.equals(java.util.List.class)) {
cTojType = java.util.ArrayList.class;
} else if (cTojType.equals(java.util.Set.class)) {
cTojType = java.util.HashSet.class;
}
Collection li = (Collection) context.getReflectRepository().getType(cTojType).newInstance();
int len = Array.getLength(value);
for (int i = 0; i < len; i++) {
li.add(Array.get(value, i));
}
return li;
}
}
} else if (fromType.getJavaType() instanceof Class && java.util.Collection.class.isAssignableFrom((Class<?>) fromType.getJavaType())) {
if (toType.isArrayType()) {
Collection coll = (Collection) value;
int len = coll.size();
Object newArr = Array.newInstance((Class<?>) toType.getJavaType(), len);
int i=0;
for (Object o : coll) {
Array.set(newArr, i++, o);
}
return newArr;
} else if (tojType instanceof Class) {
Class<?> cTojType = (Class<?>) tojType;
if (java.util.Collection.class.isAssignableFrom(cTojType)) {
if (cTojType.equals(java.util.Collection.class)) {
cTojType = java.util.ArrayList.class;
} else if (cTojType.equals(java.util.List.class)) {
cTojType = java.util.ArrayList.class;
} else if (cTojType.equals(java.util.Set.class)) {
cTojType = java.util.HashSet.class;
}
Collection li = (Collection) context.getReflectRepository().getType(cTojType).newInstance();
Collection coll = (Collection) value;
li.addAll(coll);
return li;
}
}
}
return null;
}
}
Loading

0 comments on commit 7a908d5

Please sign in to comment.