List of usage examples for java.lang.reflect Field setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:alluxio.util.LogUtils.java
/** * Gets a logger's level with specify name, if the level argument is not null, it will set to * specify level first.// w w w.ja va 2 s. com * @param logName logger's name * @param level logger's level * @return an entity object about the log info * @throws IOException if an I/O error occurs */ public static LogInfo setLogLevel(String logName, String level) throws IOException { LogInfo result = new LogInfo(); if (StringUtils.isNotBlank(logName)) { result.setLogName(logName); Log log = LogFactory.getLog(logName); Logger logger = LoggerFactory.getLogger(logName); if (log instanceof Log4JLogger) { process(((Log4JLogger) log).getLogger(), level, result); } else if (log instanceof Jdk14Logger) { process(((Jdk14Logger) log).getLogger(), level, result); } else if (logger instanceof Log4jLoggerAdapter) { try { Field field = Log4jLoggerAdapter.class.getDeclaredField("logger"); field.setAccessible(true); org.apache.log4j.Logger log4jLogger = (org.apache.log4j.Logger) field.get(logger); process(log4jLogger, level, result); } catch (NoSuchFieldException | IllegalAccessException e) { result.setMessage(e.getMessage()); } } else { result.setMessage("Sorry, " + log.getClass() + " not supported."); } } else { result.setMessage("Please specify a correct logName."); } return result; }
From source file:api_proto3.TestElf.java
public static void setConfigUnitTest(boolean unitTest) { try {//from ww w. ja v a 2 s.com Field field = HikariConfig.class.getDeclaredField("unitTest"); field.setAccessible(true); field.setBoolean(null, unitTest); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.luna.common.utils.ReflectUtils.java
public static Object getFieldValue(Object target, String fieldName, boolean isForce) throws NoSuchFieldException, IllegalAccessException { Field field = target.getClass().getDeclaredField(fieldName); field.setAccessible(isForce); return field.get(target); }
From source file:com.seajas.search.utilities.spring.security.model.ExtendedCrowdUserDetails.java
/** * Illegally extract the required SOAPPrincipal. * * @param details//from w w w. jav a 2 s . c om * @return SOAPPrincipal */ private static SOAPPrincipal extractPrincipal(final CrowdUserDetails details) { UserWithAttributes attributes = details.getRemotePrincipal(); try { Field principalField = attributes.getClass().getDeclaredField("principal"); principalField.setAccessible(true); return (SOAPPrincipal) principalField.get(attributes); } catch (NoSuchFieldException e) { throw new IllegalArgumentException("Didn't find user details which holds a SOAPPrincipal", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("Didn't find user details which holds a SOAPPrincipal", e); } }
From source file:shiver.me.timbers.spring.security.WrappedUsernamePasswordAuthenticationFilterTest.java
private static Object extractFiledValue(Class type, Object object, String fieldName) throws IllegalAccessException { if (Object.class.equals(type)) { return null; }// ww w.java2 s . c om try { final Field field = type.getDeclaredField(fieldName); field.setAccessible(true); return field.get(object); } catch (NoSuchFieldException e) { return extractFiledValue(type.getSuperclass(), object, fieldName); } }
From source file:com.seleniumtests.GenericTest.java
/** * Generate a ITestResult from scratch/*from ww w . j a va2 s. c o m*/ * @param testNGCtx * @return * @throws NoSuchMethodException * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static ITestResult generateResult(final ITestContext testNGCtx, final Class<?> clazz) throws NoSuchMethodException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { ITestResult testResult = new TestResult(); testResult.setParameters(new String[] { "foo", "bar" }); XmlSuite suite = new XmlSuite(); suite.setName("TmpSuite"); XmlTest test = new XmlTest(suite); test.setName("myTestNg"); ITestNGMethod testMethod = new TestNGMethod(clazz.getMethod("myTest"), new JDK15AnnotationFinder(new DefaultAnnotationTransformer()), test, null); Field methodField = TestResult.class.getDeclaredField("m_method"); methodField.setAccessible(true); methodField.set(testResult, testMethod); Field contextField = TestResult.class.getDeclaredField("m_context"); contextField.setAccessible(true); contextField.set(testResult, testNGCtx); return testResult; }
From source file:com.isthari.spring.cloud.config.cassandra.CassandraEnvironmentRepositoryTest.java
@BeforeClass public static void startCassandra() throws Exception { String embedded = System.getProperty("isthari.cassandra.test.embedded"); useEmbeddedCassandra = embedded == null || "true".equals(useEmbeddedCassandra); if (useEmbeddedCassandra) { cleanUp();// www . j a va2s . co m EmbeddedCassandraService cassandra = new EmbeddedCassandraService(); cassandra.start(); } repository = new CassandraEnvironmentRepository(null, "127.0.0.1", null, null, true); // LOAD TEST DATASET Class<?> clazz = repository.getClass(); Field sessionField = clazz.getDeclaredField("session"); sessionField.setAccessible(true); session = (Session) sessionField.get(repository); stmtApplicationLabelProfile = session.prepare( "insert into application_label_version (application, label , profile, version ) VALUES (?,?,?,?)"); stmtApplicationSnapshot = session .prepare("insert into configuration_snapshot (application, version, parameters) values (?,?,?)"); createSnapshot("application", "master", "", new String[] { "param4" }, new String[] { "value4" }); }
From source file:org.apache.hyracks.http.test.HttpServerTest.java
public static void setPrivateField(Object obj, String filedName, Object value) throws Exception { Field f = obj.getClass().getDeclaredField(filedName); f.setAccessible(true); f.set(obj, value);/*from ww w .ja va 2 s .co m*/ }
From source file:Main.java
/** * Called internally by installGtkPopupBugWorkaround to fix the thickness * of a GTK style field by setting it to a minimum value of 1. * //from ww w .java2 s . c om * @param style * The GTK style object. * @param fieldName * The field name. * @throws Exception * When reflection fails. */ private static void fixGtkThickness(Object style, String fieldName) throws Exception { Field field = style.getClass().getDeclaredField(fieldName); boolean accessible = field.isAccessible(); field.setAccessible(true); field.setInt(style, Math.max(1, field.getInt(style))); field.setAccessible(accessible); }
From source file:Main.java
private static Object getFieldValueSafely(Field field, Object classInstance) throws IllegalArgumentException, IllegalAccessException { boolean oldAccessibleValue = field.isAccessible(); field.setAccessible(true); Object result = field.get(classInstance); field.setAccessible(oldAccessibleValue); return result; }