List of usage examples for java.lang.reflect Field set
@CallerSensitive @ForceInline public void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException
From source file:Main.java
public static void loadContext(android.content.Context context, Field field, Object object) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {/*from w w w . j a v a 2 s.c o m*/ field.setAccessible(true); Class fType = (Class) field.getGenericType(); Constructor constructor = fType.getConstructor(android.content.Context.class); if (constructor != null) { field.set(object, constructor.newInstance(context)); } }
From source file:org.assertj.db.common.AbstractTest.java
/** * Returns an instance of a {@code Changes}. * * @param changesList The list of changes. * @return An instance.//from w w w . j a v a2s.co m * @throws Exception Exception */ protected static Changes getChanges(List<Change> changesList) throws Exception { Constructor<Changes> constructor = Changes.class.getDeclaredConstructor(); constructor.setAccessible(true); Changes changes = constructor.newInstance(); Field field = Changes.class.getDeclaredField("changesList"); field.setAccessible(true); field.set(changes, changesList); return changes; }
From source file:com.sailing.hrm.common.util.ReflectionUtils.java
/** * , private/protected, ??setter.//from w ww . ja v a2 s . co m */ public static void setFieldValue(final Object obj, final String fieldName, final Object value) { Field field = getAccessibleField(obj, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]"); } try { field.set(obj, value); } catch (IllegalAccessException e) { logger.error("??:{}", e); } }
From source file:Debug.java
public static void setDebugLevel(Class cls, Debug level) throws NoSuchFieldException { try {/*from ww w . ja va2 s .c o m*/ Field fld = cls.getField("debug"); if (fld.getType() != Debug.class || !Modifier.isStatic(fld.getModifiers())) throw new NoSuchFieldException(); fld.set(null, level); } catch (IllegalArgumentException e) { throw new NoSuchFieldException(); } catch (IllegalAccessException e) { throw new NoSuchFieldException(); } catch (SecurityException e) { throw new NoSuchFieldException(); } }
From source file:com.alvermont.terraj.fracplanet.io.JarLibraryLoader.java
/** * Adds our extraction path to the system library path. * * @throws NoSuchFieldException if there is a reflection error * @throws IllegalAccessException if there is an access error during reflection */// www .j a v a2 s. c om public static void setupPath() throws NoSuchFieldException, IllegalAccessException { String dir = System.getProperty("user.home"); dir = new File(dir).getPath(); String newLibPath = dir + File.pathSeparator + System.getProperty("java.library.path"); System.setProperty("java.library.path", newLibPath); Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); fieldSysPath.setAccessible(true); if (fieldSysPath != null) { fieldSysPath.set(System.class.getClassLoader(), null); } log.debug("Lib path: " + newLibPath); }
From source file:com.wavemaker.tools.ws.XJCCompiler.java
@SuppressWarnings("deprecation") public static S2JJAXBModel createSchemaModel(Map<String, Element> schemas, List<com.wavemaker.tools.io.File> bindingFiles, String packageName, Set<String> auxiliaryClasses, WebServiceType type) throws GenerationException { if (schemas == null || schemas.isEmpty()) { return null; }//from w w w .j a va 2 s. c o m SchemaCompiler sc = XJC.createSchemaCompiler(); if (type == WebServiceType.SOAP) { // mimic what JAXWS's WsimportTool would do for SEI class name and // JAXB class name collision. ClassNameAllocator allocator = new ClassNameAllocatorImpl( new SimpleClassNameCollector(auxiliaryClasses)); sc.setClassNameAllocator(allocator); } JAXBCompilerErrorListener listener = new JAXBCompilerErrorListener(); sc.setErrorListener(listener); try { Field ncc = sc.getClass().getDeclaredField("NO_CORRECTNESS_CHECK"); ncc.setAccessible(true); ncc.set(sc, true); } catch (Exception e) { throw new GenerationException(e); } if (packageName != null) { sc.setDefaultPackageName(packageName); } for (Entry<String, Element> entry : schemas.entrySet()) { Element schema = entry.getValue(); // need to remove xsd:import or you will get element/type already // defined error during sc.bind() Element updatedSchema = removeImportElement(schema); sc.parseSchema(entry.getKey(), updatedSchema); } if (bindingFiles != null) { for (com.wavemaker.tools.io.File file : bindingFiles) { try { InputSource inputSource = new InputSource(file.getContent().asInputStream()); // cftempfix - if binding files are NOT ALWAYS local files (eg. mongo DB file), we may need to // correctly implement // logic for none-local file case. if (file instanceof LocalFile) { File f = ((LocalFile) file).getLocalFile(); inputSource.setSystemId(f.toURI().toString()); } else { inputSource.setSystemId(ResourceURL.get(file).toURI().toString()); } sc.parseSchema(inputSource); } catch (MalformedURLException e) { throw new GenerationException(e); } catch (URISyntaxException e) { throw new GenerationException(e); } } } Options options = sc.getOptions(); options.target = SpecVersion.V2_1; // suppress generation of package level annotations options.packageLevelAnnotations = false; // generate setter methods for Collection based properties options.activePlugins.add(new com.sun.tools.xjc.addon.collection_setter_injector.PluginImpl()); // replace isXXX with getXXX for Boolean type properties options.activePlugins.add(new com.wavemaker.tools.ws.jaxb.boolean_getter.PluginImpl()); S2JJAXBModel model = sc.bind(); if (listener.hasError) { throw listener.getException(); } return model; }
From source file:com.frostwire.android.gui.UniversalScanner.java
private static Uri nativeScanFile(Context context, String path) { try {/*from www . j ava 2s .c om*/ File f = new File(path); Class<?> clazz = Class.forName("android.media.MediaScanner"); Constructor<?> mediaScannerC = clazz.getDeclaredConstructor(Context.class); Object scanner = mediaScannerC.newInstance(context); try { Method setLocaleM = clazz.getDeclaredMethod("setLocale", String.class); setLocaleM.invoke(scanner, Locale.US.toString()); } catch (Throwable e) { e.printStackTrace(); } Field mClientF = clazz.getDeclaredField("mClient"); mClientF.setAccessible(true); Object mClient = mClientF.get(scanner); Method scanSingleFileM = clazz.getDeclaredMethod("scanSingleFile", String.class, String.class, String.class); Uri fileUri = (Uri) scanSingleFileM.invoke(scanner, f.getAbsolutePath(), "external", "data/raw"); int n = context.getContentResolver().delete(fileUri, null, null); if (n > 0) { LOG.debug("Deleted from Files provider: " + fileUri); } Field mNoMediaF = mClient.getClass().getDeclaredField("mNoMedia"); mNoMediaF.setAccessible(true); mNoMediaF.setBoolean(mClient, false); // This is only for HTC (tested only on HTC One M8) try { Field mFileCacheF = clazz.getDeclaredField("mFileCache"); mFileCacheF.setAccessible(true); mFileCacheF.set(scanner, new HashMap<String, Object>()); } catch (Throwable e) { // no an HTC, I need some time to refactor this hack } try { Field mFileCacheF = clazz.getDeclaredField("mNoMediaPaths"); mFileCacheF.setAccessible(true); mFileCacheF.set(scanner, new HashMap<String, String>()); } catch (Throwable e) { e.printStackTrace(); } Method doScanFileM = mClient.getClass().getDeclaredMethod("doScanFile", String.class, String.class, long.class, long.class, boolean.class, boolean.class, boolean.class); Uri mediaUri = (Uri) doScanFileM.invoke(mClient, f.getAbsolutePath(), null, f.lastModified(), f.length(), false, true, false); Method releaseM = clazz.getDeclaredMethod("release"); releaseM.invoke(scanner); return mediaUri; } catch (Throwable e) { e.printStackTrace(); return null; } }
From source file:com.ms.commons.test.common.ReflectUtil.java
public static void setObject(Object bean, String field, Object value) { Class<?> clazz = bean.getClass(); try {//w w w . ja v a2s . c o m Field f = getDeclaredField(clazz, field); f.setAccessible(true); try { f.set(bean, value); } catch (IllegalArgumentException e) { throw new UnknowException(e); } catch (IllegalAccessException e) { throw new UnknowException(e); } } catch (SecurityException e) { throw new UnknowException(e); } catch (NoSuchFieldException e) { throw new JavaFieldNotFoundException(clazz, field); } }
From source file:com.amazonaws.retry.RetryPolicyTestBase.java
public static void injectMockHttpClient(AmazonHttpClient amazonHttpClient, HttpClient mockHttpClient) { try {/*from ww w . j av a 2s. c o m*/ Field f = AmazonHttpClient.class.getDeclaredField("httpClient"); f.setAccessible(true); f.set(amazonHttpClient, mockHttpClient); } catch (Exception e) { Assert.fail("Cannot inject the mock HttpClient object. " + e.getMessage()); } }
From source file:com.bjwg.back.util.ReflectionUtils.java
/** *//*w ww. j a v a 2 s.c o m*/ public static void setFieldValue(final Object obj, final String fieldName, final Object value) { Field field = getAccessibleField(obj, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]"); } try { field.set(obj, value); } catch (IllegalAccessException e) { // } }