List of usage examples for java.lang.reflect Method setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java
public static void mappedTest(File source, File target) throws Exception { FileInputStream fis = null;//from ww w . ja v a 2 s.c om FileOutputStream fos = null; MappedByteBuffer mapbuffer = null; try { long fileSize = source.length(); final byte[] outputData = new byte[(int) fileSize]; fis = new FileInputStream(source); fos = new FileOutputStream(target); FileChannel sChannel = fis.getChannel(); target.createNewFile(); mapbuffer = sChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize); for (int i = 0; i < fileSize; i++) { outputData[i] = mapbuffer.get(); } mapbuffer.clear(); fos.write(outputData); fos.flush(); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); if (mapbuffer == null) { return; } final Object buffer = mapbuffer; AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { Method clean = buffer.getClass().getMethod("cleaner", new Class[0]); if (clean == null) { return null; } clean.setAccessible(true); sun.misc.Cleaner cleaner = (sun.misc.Cleaner) clean.invoke(buffer, new Object[0]); cleaner.clean(); } catch (Throwable ex) { } return null; } }); } }
From source file:com.atlassian.connector.eclipse.internal.crucible.ui.operations.CrucibleFileInfoCompareEditorInput.java
private static Viewer findContentViewer(Viewer contentViewer, ICompareInput input, Composite parent, CrucibleCompareAnnotationModel annotationModel) { // FIXME: hack if (contentViewer instanceof TextMergeViewer) { TextMergeViewer textMergeViewer = (TextMergeViewer) contentViewer; try {/* ww w. jav a 2s .c om*/ Class<TextMergeViewer> clazz = TextMergeViewer.class; Field declaredField = clazz.getDeclaredField("fLeft"); declaredField.setAccessible(true); final MergeSourceViewer fLeft = (MergeSourceViewer) declaredField.get(textMergeViewer); declaredField = clazz.getDeclaredField("fRight"); declaredField.setAccessible(true); final MergeSourceViewer fRight = (MergeSourceViewer) declaredField.get(textMergeViewer); annotationModel.attachToViewer(textMergeViewer, fLeft, fRight); annotationModel.focusOnComment(); annotationModel.registerContextMenu(); Method setActiveViewer = clazz.getDeclaredMethod("setActiveViewer", MergeSourceViewer.class, boolean.class); setActiveViewer.setAccessible(true); setActiveViewer.invoke(textMergeViewer, fRight, true); hackGalileo(contentViewer, textMergeViewer, fLeft, fRight); } catch (Throwable t) { StatusHandler.log(new Status(IStatus.WARNING, AtlassianTeamUiPlugin.PLUGIN_ID, "Could not initialize annotation model for " + input.getName(), t)); } } return contentViewer; }
From source file:com.haulmont.bali.util.ReflectionHelper.java
/** * Invokes a method by reflection./*from w w w . j a v a2 s .c o m*/ * @param obj object instance * @param name method name * @param params method arguments * @return method result * @throws NoSuchMethodException if a suitable method not found */ public static <T> T invokeMethod(Object obj, String name, Object... params) throws NoSuchMethodException { Class[] paramTypes = getParamTypes(params); final Class<?> aClass = obj.getClass(); Method method; try { method = aClass.getDeclaredMethod(name, paramTypes); } catch (NoSuchMethodException e) { method = aClass.getMethod(name, paramTypes); } method.setAccessible(true); try { //noinspection unchecked return (T) method.invoke(obj, params); } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); } }
From source file:it.geosolutions.geobatch.action.scripting.ScriptingAction.java
/** * /*from w w w.ja v a 2 s .c o m*/ * @param moduleFile * @throws IOException */ private static void addFile(File moduleFile) throws IOException { URL moduleURL = moduleFile.toURI().toURL(); final Class[] parameters = new Class[] { URL.class }; URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class sysclass = URLClassLoader.class; try { Method method = sysclass.getDeclaredMethod("addURL", parameters); method.setAccessible(true); method.invoke(sysloader, new Object[] { moduleURL }); } catch (Throwable t) { LOGGER.error("Error, could not add URL to system classloader", t); throw new IOException("Error, could not add URL to system classloader"); } }
From source file:com.aionemu.gameserver.model.TribeRelationCheck.java
static void setPositionAsSpawned(WorldPosition position) { try {/*from ww w . j a v a 2s .c o m*/ Method method = WorldPosition.class.getDeclaredMethod("setIsSpawned", new Class<?>[] { boolean.class }); method.setAccessible(true); method.invoke(position, new Object[] { true }); } catch (Exception e) { System.out.println(e); } }
From source file:Main.java
/** * Invokes the given method on the EDT./* w w w . j ava2s . co m*/ * * @param target Target object exposing the specified method. * @param methodName Method to invoke. * @param args Arguments to pass to the method. */ public static void invokeOnEDT(final Object target, final String methodName, final Object... args) { final Class[] types = new Class[args.length]; for (int i = 0; i < types.length; i++) types[i] = args[i].getClass(); final Method m = getMethod(target, methodName, types); if (m == null) throw new RuntimeException("No such method: " + methodName + '(' + Arrays.toString(types) + ") found for target" + "class " + target.getClass().getName()); if (!m.isAccessible()) m.setAccessible(true); SwingUtilities.invokeLater(new Runnable() { public void run() { try { m.invoke(target, args); } catch (Exception e) { throw new RuntimeException(e); } } }); }
From source file:org.shept.util.BeanUtilsExtended.java
/** * Merge the property values of the given source bean into the given target bean. * <p>Note: Only not-null values are merged into the given target bean. * Note: The source and target classes do not have to match or even be derived * from each other, as long as the properties match. Any bean properties that the * source bean exposes but the target bean does not will silently be ignored. * @param source the source bean/*from www. ja va2 s . c om*/ * @param target the target bean * @param editable the class (or interface) to restrict property setting to * @param ignoreProperties array of property names to ignore * @throws BeansException if the copying failed * @see BeanWrapper */ private static void mergeProperties(Object source, Object target, Class<?> editable, String[] ignoreProperties) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); if (editable != null) { if (!editable.isInstance(target)) { throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); } actualEditable = editable; } PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null; for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (value != null) { Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } catch (Throwable ex) { throw new FatalBeanException("Could not copy properties from source to target", ex); } } } } }
From source file:com.mawujun.utils.bean.BeanUtils.java
/** * ????//from w w w . jav a 2 s . co m * null * @param source * @param target * @throws BeansException * @throws IntrospectionException */ public static void copyExcludeNull(Object source, Object target) throws IntrospectionException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (value == null) {//?? continue; } Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } catch (Throwable ex) { throw new RuntimeException("Could not copy properties from source to target", ex); } } } } }
From source file:com.palantir.ptoss.util.Reflections.java
/** * Looks up an {@link Enum} value by its {@link String} name. * @param enumType {@link Enum} class to query. * @param value {@link String} name for the {@link Enum} value. * @return the actual {@link Enum} value specified by the passed name. * @see Enum#valueOf(Class, String)/*from w w w . j a va 2s . com*/ */ public static Object evalEnum(Class<?> enumType, String value) { try { Method method = enumType.getMethod("valueOf", String.class); method.setAccessible(true); return method.invoke(null, value); } catch (Exception ew) { throw new IllegalArgumentException("could not find enum value: " + value); } }
From source file:com.aw.support.reflection.MethodInvoker.java
public static Object invoke(Object target, String methodName, Object[] parameters) throws Throwable { Object result = null;//from w ww . java 2 s. c o m try { Class cls = target.getClass(); Class[] paramTypes = new Class[parameters.length]; for (int i = 0; i < paramTypes.length; i++) { paramTypes[i] = parameters[i].getClass(); } Method method = cls.getMethod(methodName, paramTypes); method.setAccessible(true); result = method.invoke(target, parameters); } catch (Throwable e) { if (e.getCause() instanceof AWException) { throw ((AWException) e.getCause()); } e.printStackTrace(); throw e; } return result; }