All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.azbh111.utils.java.reflect.model.ClassCopyDesc Maven / Gradle / Ivy

The newest version!
package com.github.azbh111.utils.java.reflect.model;

import com.github.azbh111.utils.java.unsafe.UnsafeUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author pyz
 * @date 2019/3/2 11:54 AM
 */
public class ClassCopyDesc {
    private Class clazz;
    private List fields = new ArrayList<>();
    private Constructor constructor;

    private ClassCopyDesc() {
    }

    public static  ClassCopyDesc of(Class clazz) {
        ClassCopyDesc copyer = new ClassCopyDesc();
        copyer.clazz = clazz;
        try {
            copyer.constructor = clazz.getConstructor(new Class[0]);
            copyer.constructor.setAccessible(true);
        } catch (NoSuchMethodException e) {
            // no none parameter constructor
        }
        Class currentClass = clazz;
        while (currentClass != null) {
            for (Field field : currentClass.getDeclaredFields()) {
                if (Modifier.isStatic(field.getModifiers())) {
                    continue;
                }
                UnsafeField uf = UnsafeField.builder()
                        .field(field)
                        .offset(UnsafeUtils.objectFieldOffset(field))
                        .type(field.getType())
                        .build();
                copyer.getFields().add(uf);
            }
            currentClass = currentClass.getSuperclass();
        }
        return copyer;
    }

    public T newInstance() throws IllegalAccessException, InvocationTargetException, InstantiationException {
        if (constructor != null) {
            return (T) constructor.newInstance();
        }
        return (T) UnsafeUtils.allocateInstance(clazz);
    }

    public Class getClazz() {
        return clazz;
    }

    public List getFields() {
        return fields;
    }

    public Constructor getConstructor() {
        return constructor;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("ClassCopyDesc{");
        sb.append("clazz=").append(clazz);
        sb.append(", fields=").append(fields);
        sb.append(", constructor=").append(constructor);
        sb.append('}');
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy