Example usage for java.lang.reflect Field setAccessible

List of usage examples for java.lang.reflect Field setAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Field setAccessible.

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

private static Field getSingletonField(Class<?> hostClas) {
    Field f = null;
    try {//w  w  w  .  ja va  2  s  . co m
        f = hostClas.getDeclaredField("I");
        if (!Modifier.isStatic(f.getModifiers()))
            throw new Exception("'I' field should be static." + f);
        f.setAccessible(true);
    } catch (Throwable ex) {
    }
    return f;
}

From source file:com.github.lothar.security.acl.jpa.query.AclJpaQuery.java

@SuppressWarnings("unchecked")
private static <T> T getField(Class<?> type, Object object, String fieldName) {
    Field field = ReflectionUtils.findField(type, fieldName);
    field.setAccessible(true);
    Object property = ReflectionUtils.getField(field, object);
    return (T) property;
}

From source file:net.sf.beanlib.hibernate3.Hibernate3SequenceGenerator.java

/** Returns the next sequence id from the specified sequence and session. */
public static long nextval(final String sequenceName, final Session session) {
    Object target = session;//from ww w .  j av  a 2  s.  c  o  m

    if (Proxy.isProxyClass(session.getClass())) {
        // Dig out the underlying session.
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(session);

        if (invocationHandler instanceof DtoCentricCloseSuppressingInvocationHandler) {
            // This is faster for we don't need to use reflection.
            DtoCentricCloseSuppressingInvocationHandler dch = (DtoCentricCloseSuppressingInvocationHandler) invocationHandler;
            target = dch.getTarget();
        } else {
            Class<?> invocationHandlerClass = invocationHandler.getClass();
            Class<?> invocationHandlerDeclaringClass = invocationHandlerClass.getDeclaringClass();

            if (invocationHandlerDeclaringClass == HibernateTemplate.class) {
                String className = invocationHandlerClass.getName();

                if (className.endsWith("CloseSuppressingInvocationHandler")) {
                    // Assume this is the private class org.springframework.orm.hibernate3.HibernateTempate$CloseSuppressingInvocationHandler
                    // Dig out the private target.  
                    // I know this is bad, but there doesn't seem to be a better way.  Oh well.
                    try {
                        Field f = invocationHandlerClass.getDeclaredField("target");
                        f.setAccessible(true);
                        target = f.get(invocationHandler);
                    } catch (SecurityException e) {
                        throw new RuntimeException(e);
                    } catch (NoSuchFieldException e) {
                        throw new RuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }

                }
            }

        }
    }
    SessionImpl sessionImpl;

    if (target instanceof SessionImpl)
        sessionImpl = (SessionImpl) target;
    else
        throw new IllegalStateException("Not yet know how to handle the given session!");
    IdentifierGenerator idGenerator = createIdentifierGenerator(sequenceName, session);
    Serializable id = idGenerator.generate(sessionImpl, null);
    return (Long) id;
}

From source file:springobjectmapper.ReflectionHelper.java

public static List<Field> getFields(Class<?> c) {
    List<Field> results = new ArrayList<Field>();
    do {/* w w  w.j  a v  a2s .com*/
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            if (isEntityField(field)) {
                results.add(field);
            }
            field.setAccessible(true);
        }
        c = c.getSuperclass();
    } while (c != null);
    return results;
}

From source file:com.infinira.aerospike.dataaccess.util.Utils.java

public static boolean set(Object object, String fieldName, Object fieldValue) {
    Assert.notNull(object, "Object cannot be null.");
    Assert.notNull(fieldName, "Fieldname cannot be null.");
    Class<?> clazz = object.getClass();
    while (clazz != null) {
        try {//  w  ww . java 2  s  .c  o m
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(object, fieldValue);
            return true;
        } catch (NoSuchFieldException e) {
            clazz = clazz.getSuperclass();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    return false;
}

From source file:com.infinira.aerospike.dataaccess.util.Utils.java

@SuppressWarnings("unchecked")
public static <V> V get(Object object, String fieldName) {
    Assert.notNull(object, "Object cannot be null.");
    Assert.notNull(fieldName, "Fieldname cannot be null.");
    Class<?> clazz = object.getClass();
    while (clazz != null) {
        try {//from   w  ww  .ja v a  2  s.co m
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            return (V) field.get(object);
        } catch (NoSuchFieldException e) {
            clazz = clazz.getSuperclass();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    return null;
}

From source file:jin.collection.util.PropertyUtil.java

static Object getPropertyByField(final Object element, final String property) {
    try {// www . java  2  s  . c o  m
        final Field declaredField = element.getClass().getDeclaredField(property);
        declaredField.setAccessible(true);
        return declaredField.get(element);
    } catch (final Exception e) {
        throw new RuntimeException("", e);
    }
}

From source file:com.thoughtworks.go.config.MagicalGoConfigXmlLoader.java

public static void setMd5(CruiseConfig configForEdit, String md5)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = BasicCruiseConfig.class.getDeclaredField("md5");
    field.setAccessible(true);
    field.set(configForEdit, md5);/* w  w w  . ja v  a 2s  .c  o m*/
}

From source file:com.yimidida.shards.utils.ParameterUtil.java

public static Serializable extractPrimaryKey(Object object) {
    if (object != null) {
        Class<?> clazz = object.getClass();
        Field[] first = clazz.getDeclaredFields();
        Field[] second = clazz.getSuperclass().getDeclaredFields();

        Field[] fields = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, fields, first.length, second.length);

        for (Field field : fields) {
            field.setAccessible(true);

            PrimaryKey primaryKey = field.getAnnotation(PrimaryKey.class);
            if (primaryKey != null) {
                try {
                    //0
                    Object result = field.get(object);
                    if (result != null && "0".equals(result.toString())) {
                        return null;
                    }//  w ww.j  a v  a  2s  . c o  m
                    return (Serializable) result;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }

    return null;
}

From source file:com.yimidida.shards.utils.ParameterUtil.java

public static Object generatePrimaryKey(Object object, Serializable id) {
    if (object != null) {
        Assert.notNull(id, "generated id can not be null.");

        Class<?> clazz = object.getClass();
        Field[] first = clazz.getDeclaredFields();
        Field[] second = clazz.getSuperclass().getDeclaredFields();

        Field[] fields = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, fields, first.length, second.length);

        for (Field field : fields) {
            field.setAccessible(true);

            PrimaryKey primaryKey = field.getAnnotation(PrimaryKey.class);
            if (primaryKey != null) {
                try {
                    //set id
                    field.set(object, id);

                    return object;
                } catch (Exception e) {
                    e.printStackTrace();
                }/*from ww  w  .  j  a  v a  2s  .  c  o m*/
            }
        }

    }

    return null;
}