Search in sources :

Example 71 with Field

use of java.lang.reflect.Field in project dbeaver by serge-rider.

the class AbstractObjectCache method deepCopyCachedObject.

/**
     * Performs a deep copy of srcObject into dstObject.
     * Copies all fields (recursively) and clears all nested caches
     */
protected void deepCopyCachedObject(@NotNull Object srcObject, @NotNull Object dstObject) {
    if (srcObject.getClass() != dstObject.getClass()) {
        log.error("Can't make object opy: src class " + srcObject.getClass().getName() + "' != dest class '" + dstObject.getClass().getName() + "'");
        return;
    }
    try {
        for (Class<?> theClass = srcObject.getClass(); theClass != Object.class; theClass = theClass.getSuperclass()) {
            final Field[] fields = theClass.getDeclaredFields();
            for (Field field : fields) {
                final int modifiers = field.getModifiers();
                if (Modifier.isStatic(modifiers)) {
                    continue;
                }
                field.setAccessible(true);
                final Object srcValue = field.get(srcObject);
                final Object dstValue = field.get(dstObject);
                if (DBSObjectCache.class.isAssignableFrom(field.getType())) {
                    if (dstValue != null) {
                        ((DBSObjectCache) dstValue).clearCache();
                    }
                } else {
                    if (isPropertyGroupField(field)) {
                        // Just in case check that values not null and have the same type
                        if (dstValue != null && srcValue != null && dstValue.getClass() == srcValue.getClass()) {
                            deepCopyCachedObject(srcValue, dstValue);
                        }
                    } else if (Modifier.isFinal(modifiers)) {
                    // Ignore final fields
                    } else {
                        // Just copy value
                        field.set(dstObject, srcValue);
                    }
                }
            }
        }
    } catch (Throwable e) {
        log.error("Error copying object state", e);
    }
}
Also used : Field(java.lang.reflect.Field) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Example 72 with Field

use of java.lang.reflect.Field in project lombok by rzwitserloot.

the class EclipseLoaderPatcherTransplants method overrideLoadResult.

public static Class overrideLoadResult(ClassLoader original, String name, boolean resolve) throws ClassNotFoundException {
    try {
        Field shadowLoaderField = original.getClass().getField("lombok$shadowLoader");
        ClassLoader shadowLoader = (ClassLoader) shadowLoaderField.get(original);
        if (shadowLoader == null) {
            synchronized ("lombok$shadowLoader$globalLock".intern()) {
                shadowLoader = (ClassLoader) shadowLoaderField.get(original);
                if (shadowLoader == null) {
                    Class shadowClassLoaderClass = (Class) original.getClass().getField("lombok$shadowLoaderClass").get(null);
                    Class classLoaderClass = Class.forName("java.lang.ClassLoader");
                    String jarLoc = (String) original.getClass().getField("lombok$location").get(null);
                    if (shadowClassLoaderClass == null) {
                        JarFile jf = new JarFile(jarLoc);
                        InputStream in = null;
                        try {
                            ZipEntry entry = jf.getEntry("lombok/launch/ShadowClassLoader.class");
                            in = jf.getInputStream(entry);
                            byte[] bytes = new byte[65536];
                            int len = 0;
                            while (true) {
                                int r = in.read(bytes, len, bytes.length - len);
                                if (r == -1)
                                    break;
                                len += r;
                                if (len == bytes.length)
                                    throw new IllegalStateException("lombok.launch.ShadowClassLoader too large.");
                            }
                            in.close();
                            {
                                Class[] paramTypes = new Class[4];
                                paramTypes[0] = "".getClass();
                                paramTypes[1] = new byte[0].getClass();
                                paramTypes[2] = Integer.TYPE;
                                paramTypes[3] = paramTypes[2];
                                Method defineClassMethod = classLoaderClass.getDeclaredMethod("defineClass", paramTypes);
                                defineClassMethod.setAccessible(true);
                                shadowClassLoaderClass = (Class) defineClassMethod.invoke(original, new Object[] { "lombok.launch.ShadowClassLoader", bytes, new Integer(0), new Integer(len) });
                                original.getClass().getField("lombok$shadowLoaderClass").set(null, shadowClassLoaderClass);
                            }
                        } finally {
                            if (in != null)
                                in.close();
                            jf.close();
                        }
                    }
                    Class[] paramTypes = new Class[5];
                    paramTypes[0] = classLoaderClass;
                    paramTypes[1] = "".getClass();
                    paramTypes[2] = paramTypes[1];
                    paramTypes[3] = Class.forName("java.util.List");
                    paramTypes[4] = paramTypes[3];
                    Constructor constructor = shadowClassLoaderClass.getDeclaredConstructor(paramTypes);
                    constructor.setAccessible(true);
                    shadowLoader = (ClassLoader) constructor.newInstance(new Object[] { original, "lombok", jarLoc, Arrays.asList(new Object[] { "lombok." }), Arrays.asList(new Object[] { "lombok.patcher.Symbols" }) });
                    shadowLoaderField.set(original, shadowLoader);
                }
            }
        }
        if (resolve) {
            Class[] paramTypes = new Class[2];
            paramTypes[0] = "".getClass();
            paramTypes[1] = Boolean.TYPE;
            Method m = shadowLoader.getClass().getDeclaredMethod("loadClass", new Class[] { String.class, boolean.class });
            m.setAccessible(true);
            return (Class) m.invoke(shadowLoader, new Object[] { name, Boolean.TRUE });
        } else {
            return shadowLoader.loadClass(name);
        }
    } catch (Exception ex) {
        Throwable t = ex;
        if (t instanceof InvocationTargetException)
            t = t.getCause();
        if (t instanceof RuntimeException)
            throw (RuntimeException) t;
        if (t instanceof Error)
            throw (Error) t;
        throw new RuntimeException(t);
    }
}
Also used : InputStream(java.io.InputStream) Constructor(java.lang.reflect.Constructor) ZipEntry(java.util.zip.ZipEntry) Method(java.lang.reflect.Method) JarFile(java.util.jar.JarFile) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field)

Example 73 with Field

use of java.lang.reflect.Field in project antlr4 by antlr.

the class BaseRuntimeTestDescriptor method rawGetGrammar.

private String rawGetGrammar() {
    String grammar = null;
    try {
        Field f = this.getClass().getField("grammar");
        grammar = (String) f.get(this);
    } catch (Exception nsfe) {
        System.err.println("No start rule specified for test " + getTestName());
    }
    grammar = stringIndentation(grammar);
    return grammar;
}
Also used : Field(java.lang.reflect.Field)

Example 74 with Field

use of java.lang.reflect.Field in project platform_frameworks_base by android.

the class Looper_Accessor method cleanupThread.

public static void cleanupThread() {
    // clean up the looper
    Looper.sThreadLocal.remove();
    try {
        Field sMainLooper = Looper.class.getDeclaredField("sMainLooper");
        sMainLooper.setAccessible(true);
        sMainLooper.set(null, null);
    } catch (SecurityException e) {
        catchReflectionException();
    } catch (IllegalArgumentException e) {
        catchReflectionException();
    } catch (NoSuchFieldException e) {
        catchReflectionException();
    } catch (IllegalAccessException e) {
        catchReflectionException();
    }
}
Also used : Field(java.lang.reflect.Field)

Example 75 with Field

use of java.lang.reflect.Field in project nutz by nutzam.

the class Number2Enum method cast.

@Override
public Enum cast(Number src, Class<?> toType, String... args) throws FailToCastObjectException {
    int v = src.intValue();
    Enum o = null;
    // 首先试图用采用该类型的 fromInt 的静态方法
    try {
        Method m = toType.getMethod("fromInt", int.class);
        if (Modifier.isStatic(m.getModifiers()) && toType.isAssignableFrom(m.getReturnType())) {
            o = (Enum) m.invoke(null, v);
        }
    } catch (Exception e) {
    }
    // 搞不定,则试图根据顺序号获取
    if (null == o)
        try {
            for (Field field : toType.getFields()) {
                if (field.getType() == toType) {
                    Enum em = (Enum) field.get(null);
                    if (em.ordinal() == v)
                        return em;
                }
            }
            throw Lang.makeThrow(FailToCastObjectException.class, "Can NO find enum value in [%s] by int value '%d'", toType.getName(), src.intValue());
        } catch (Exception e2) {
            throw Lang.wrapThrow(e2, FailToCastObjectException.class);
        }
    return o;
}
Also used : Field(java.lang.reflect.Field) FailToCastObjectException(org.nutz.castor.FailToCastObjectException) Method(java.lang.reflect.Method) FailToCastObjectException(org.nutz.castor.FailToCastObjectException)

Aggregations

Field (java.lang.reflect.Field)5144 Method (java.lang.reflect.Method)613 Test (org.junit.Test)538 ArrayList (java.util.ArrayList)490 IOException (java.io.IOException)318 HashMap (java.util.HashMap)318 Map (java.util.Map)296 InvocationTargetException (java.lang.reflect.InvocationTargetException)176 List (java.util.List)171 Pattern (java.util.regex.Pattern)122 Matcher (java.util.regex.Matcher)117 HashSet (java.util.HashSet)109 File (java.io.File)107 Annotation (java.lang.annotation.Annotation)98 Support_Field (tests.support.Support_Field)82 Collection (java.util.Collection)81 Test (org.testng.annotations.Test)68 Type (java.lang.reflect.Type)67 SienaException (siena.SienaException)65 Before (org.junit.Before)62