public void testFindField() throws Exception { final Field field = ReflectionUtil.findField(B.class, String.class, "privateA"); assertNotNull(field); assertEquals(String.class, field.getType()); assertEquals("privateA", field.getName()); try { ReflectionUtil.findField(B.class, String.class, "whatever"); } catch (NoSuchFieldException e) { return; } fail(); }
@NotNull public static Icon getIcon(@NonNls @NotNull final String path) { Class callerClass = ReflectionUtil.getGrandCallerClass(); assert callerClass != null : path; return getIcon(path, callerClass); }
public static void clearOwnFields(@NotNull Object object) { final Field[] all = object.getClass().getDeclaredFields(); for (Field each : all) { if ((each.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) > 0) continue; ReflectionUtil.resetField(object, each); } }
public void serializeInto( @NotNull Object o, @NotNull Element element, @NotNull SerializationFilter filter) { for (Binding binding : myPropertyBindings.keySet()) { Accessor accessor = myPropertyBindings.get(binding); if (!filter.accepts(accessor, o)) { continue; } // todo: optimize. Cache it. Property property = accessor.getAnnotation(Property.class); if (property != null && property.filter() != SerializationFilter.class) { try { if (!ReflectionUtil.newInstance(property.filter()).accepts(accessor, o)) { continue; } } catch (RuntimeException e) { throw new XmlSerializationException(e); } } Object node = binding.serialize(o, element, filter); if (node != null) { if (node instanceof org.jdom.Attribute) { element.setAttribute((org.jdom.Attribute) node); } else { JDOMUtil.addContent(element, node); } } } }
@Override @Nullable public BaseControl createCustomControl( final Type type, DomWrapper<String> wrapper, final boolean commitOnEveryChange) { final Function<DomWrapper<String>, BaseControl> factory = myCustomControlCreators.get(ReflectionUtil.getRawType(type)); return factory == null ? null : factory.fun(wrapper); }
public AwtPopupWrapper(Popup popup) { myPopup = popup; if (SystemInfo.isMac && UIUtil.isUnderAquaLookAndFeel()) { final Component c = (Component) ReflectionUtil.getField(Popup.class, myPopup, Component.class, "component"); c.setBackground(UIUtil.getPanelBackground()); } }
public void dispose() { try { myVirtualMachine.dispose(); } catch (UnsupportedOperationException e) { LOG.info(e); } // Memory leak workaround, see IDEA-163334 TargetVM target = ReflectionUtil.getField( myVirtualMachine.getClass(), myVirtualMachine, TargetVM.class, "target"); if (target != null) { Thread controller = ReflectionUtil.getField(target.getClass(), target, Thread.class, "eventController"); if (controller != null) { controller.stop(); } } }
@Nullable /** * Might return null if icon was not found. Use only if you expected null return value, otherwise * see {@link IconLoader#getIcon(java.lang.String)} */ public static Icon findIcon(@NonNls @NotNull String path) { Class callerClass = ReflectionUtil.getGrandCallerClass(); if (callerClass == null) return null; return findIcon(path, callerClass); }
public static boolean isTagValueGetter(final JavaMethod method) { if (!isGetter(method)) { return false; } if (hasTagValueAnnotation(method)) { return true; } if ("getValue".equals(method.getName())) { if (method.getAnnotation(SubTag.class) != null) return false; if (method.getAnnotation(SubTagList.class) != null) return false; if (method.getAnnotation(Convert.class) != null || method.getAnnotation(Resolve.class) != null) { return !ReflectionUtil.isAssignable(GenericDomValue.class, method.getReturnType()); } if (ReflectionUtil.isAssignable(DomElement.class, method.getReturnType())) return false; return true; } return false; }
static { Class<?> aClass = null; Set<String> files = null; try { aClass = Class.forName("java.io.DeleteOnExitHook"); files = ReflectionUtil.getField(aClass, null, Set.class, "files"); } catch (Exception ignored) { } DELETE_ON_EXIT_HOOK_CLASS = aClass; DELETE_ON_EXIT_HOOK_DOT_FILES = files; }
public static void addStringsFromClassToStatics(@NotNull Class aClass) { for (Field field : aClass.getDeclaredFields()) { if ((field.getModifiers() & Modifier.STATIC) == 0) continue; if ((field.getModifiers() & Modifier.PUBLIC) == 0) continue; if (!String.class.equals(field.getType())) continue; String typeName = ReflectionUtil.getStaticFieldValue(aClass, String.class, field.getName()); if (typeName != null) { staticIntern(typeName); } } }
public void hide(boolean dispose) { myPopup.hide(); Window wnd = getWindow(); if (wnd instanceof JWindow) { JRootPane rootPane = ((JWindow) wnd).getRootPane(); if (rootPane != null) { ReflectionUtil.resetField(rootPane, "clientProperties"); } } }
@NotNull protected <T extends NodeDescriptor> List<T> getSelectedNodes(final Class<T> nodeClass) { TreePath[] paths = getSelectionPaths(); if (paths == null) return Collections.emptyList(); final ArrayList<T> result = new ArrayList<T>(); for (TreePath path : paths) { Object lastPathComponent = path.getLastPathComponent(); if (lastPathComponent instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) lastPathComponent; Object userObject = node.getUserObject(); if (userObject != null && ReflectionUtil.isAssignable(nodeClass, userObject.getClass())) { result.add((T) userObject); } } } return result; }
@Nullable private static Icon getAquaMenuInvertedIcon() { if (!UIUtil.isUnderAquaLookAndFeel()) return null; final Icon arrow = (Icon) UIManager.get("Menu.arrowIcon"); if (arrow == null) return null; try { final Method method = ReflectionUtil.getMethod(arrow.getClass(), "getInvertedIcon"); if (method != null) { return (Icon) method.invoke(arrow); } return null; } catch (InvocationTargetException e1) { return null; } catch (IllegalAccessException e1) { return null; } }
@Override @Nullable public PsiElement findElementAt(int offset, @NotNull Class<? extends Language> lang) { final PsiFile mainRoot = getPsi(getBaseLanguage()); PsiElement ret = null; for (final Language language : getLanguages()) { if (!ReflectionUtil.isAssignable(lang, language.getClass())) continue; if (lang.equals(Language.class) && !getLanguages().contains(language)) continue; final PsiFile psiRoot = getPsi(language); final PsiElement psiElement = findElementAt(psiRoot, offset); if (psiElement == null || psiElement instanceof OuterLanguageElement) continue; if (ret == null || psiRoot != mainRoot) { ret = psiElement; } } return ret; }
@NotNull private static ConfigImportSettings getConfigImportSettings() { try { @SuppressWarnings("unchecked") Class<ConfigImportSettings> customProviderClass = (Class<ConfigImportSettings>) Class.forName( "com.intellij.openapi.application." + PlatformUtils.getPlatformPrefix() + "ConfigImportSettings"); if (ConfigImportSettings.class.isAssignableFrom(customProviderClass)) { return ReflectionUtil.newInstance(customProviderClass); } } catch (ClassNotFoundException ignored) { } catch (RuntimeException ignored) { } return new ConfigImportSettings(); }
public void testResetField() throws Exception { final Reset reset = new Reset(); ReflectionUtil.resetField(reset, String.class, "STRING"); assertNull(reset.STRING); ReflectionUtil.resetField(reset, boolean.class, "BOOLEAN"); assertFalse(reset.BOOLEAN); ReflectionUtil.resetField(reset, int.class, "INT"); assertEquals(0, reset.INT); ReflectionUtil.resetField(reset, double.class, "DOUBLE"); assertEquals(0d, reset.DOUBLE); ReflectionUtil.resetField(reset, float.class, "FLOAT"); assertEquals(0f, reset.FLOAT); ReflectionUtil.resetField(Reset.class, String.class, "STATIC_STRING"); assertNull(Reset.STATIC_STRING); }
static void initializePlugins(@Nullable StartupProgress progress) { configureExtensions(); final IdeaPluginDescriptorImpl[] pluginDescriptors = loadDescriptors(progress); final Class callerClass = ReflectionUtil.findCallerClass(1); assert callerClass != null; final ClassLoader parentLoader = callerClass.getClassLoader(); final List<IdeaPluginDescriptorImpl> result = new ArrayList<IdeaPluginDescriptorImpl>(); final HashMap<String, String> disabledPluginNames = new HashMap<String, String>(); for (IdeaPluginDescriptorImpl descriptor : pluginDescriptors) { if (descriptor.getPluginId().getIdString().equals(CORE_PLUGIN_ID)) { final List<String> modules = descriptor.getModules(); if (modules != null) { ourAvailableModules.addAll(modules); } } if (!shouldSkipPlugin(descriptor, pluginDescriptors)) { result.add(descriptor); } else { descriptor.setEnabled(false); disabledPluginNames.put(descriptor.getPluginId().getIdString(), descriptor.getName()); initClassLoader(parentLoader, descriptor); } } prepareLoadingPluginsErrorMessage(filterBadPlugins(result, disabledPluginNames)); final Map<PluginId, IdeaPluginDescriptorImpl> idToDescriptorMap = new HashMap<PluginId, IdeaPluginDescriptorImpl>(); for (final IdeaPluginDescriptorImpl descriptor : result) { idToDescriptorMap.put(descriptor.getPluginId(), descriptor); } final IdeaPluginDescriptor corePluginDescriptor = idToDescriptorMap.get(PluginId.getId(CORE_PLUGIN_ID)); assert corePluginDescriptor != null : CORE_PLUGIN_ID + " not found; platform prefix is " + System.getProperty(PlatformUtilsCore.PLATFORM_PREFIX_KEY); for (IdeaPluginDescriptorImpl descriptor : result) { if (descriptor != corePluginDescriptor) { descriptor.insertDependency(corePluginDescriptor); } } mergeOptionalConfigs(idToDescriptorMap); // sort descriptors according to plugin dependencies Collections.sort(result, getPluginDescriptorComparator(idToDescriptorMap)); for (int i = 0; i < result.size(); i++) { ourId2Index.put(result.get(i).getPluginId(), i); } int i = 0; for (final IdeaPluginDescriptorImpl pluginDescriptor : result) { if (pluginDescriptor.getPluginId().getIdString().equals(CORE_PLUGIN_ID) || pluginDescriptor.isUseCoreClassLoader()) { pluginDescriptor.setLoader(parentLoader, true); } else { final List<File> classPath = pluginDescriptor.getClassPath(); final PluginId[] dependentPluginIds = pluginDescriptor.getDependentPluginIds(); final ClassLoader[] parentLoaders = getParentLoaders(idToDescriptorMap, dependentPluginIds); final ClassLoader pluginClassLoader = createPluginClassLoader( classPath.toArray(new File[classPath.size()]), parentLoaders.length > 0 ? parentLoaders : new ClassLoader[] {parentLoader}, pluginDescriptor); pluginDescriptor.setLoader(pluginClassLoader, true); } pluginDescriptor.registerExtensions(); if (progress != null) { progress.showProgress( "", PLUGINS_PROGRESS_MAX_VALUE + (i++ / (float) result.size()) * 0.35f); } } ourPlugins = pluginDescriptors; }
public Window getWindow() { final Component c = (Component) ReflectionUtil.getField(Popup.class, myPopup, Component.class, "component"); return c instanceof JWindow ? (JWindow) c : null; }
@Override public T createElement() { return ReflectionUtil.newInstance(itemEditor.getItemClass()); }
@Nullable public static Image loadFromResource(@NonNls @NotNull String s) { Class callerClass = ReflectionUtil.getGrandCallerClass(); if (callerClass == null) return null; return loadFromResource(s, callerClass); }
private static void resetPopupTrigger(final MouseEvent e) { ReflectionUtil.setField(MouseEvent.class, e, boolean.class, "popupTrigger", false); }
/** * This method patches event if it concerns side buttons like 4 (Backward) or 5 (Forward) AND it's * not single-click event. We won't support double-click for side buttons. Also some JDK bugs * produce zero-click events for side buttons. * * @return true if event was patched */ public static boolean patchClickCount(final MouseEvent e) { if (e.getClickCount() != 1 && e.getButton() > 3) { ReflectionUtil.setField(MouseEvent.class, e, int.class, "clickCount", 1); } return false; }
@Override @Nullable protected Class create(final Couple<Type> key) { return ReflectionUtil.substituteGenericType(key.first, key.second); }