@Nullable public static Type resolveVariable( TypeVariable variable, final Class classType, boolean resolveInInterfacesOnly) { final Class aClass = getRawType(classType); int index = ArrayUtilRt.find(ReflectionCache.getTypeParameters(aClass), variable); if (index >= 0) { return variable; } final Class[] classes = ReflectionCache.getInterfaces(aClass); final Type[] genericInterfaces = ReflectionCache.getGenericInterfaces(aClass); for (int i = 0; i <= classes.length; i++) { Class anInterface; if (i < classes.length) { anInterface = classes[i]; } else { anInterface = ReflectionCache.getSuperClass(aClass); if (resolveInInterfacesOnly || anInterface == null) { continue; } } final Type resolved = resolveVariable(variable, anInterface); if (resolved instanceof Class || resolved instanceof ParameterizedType) { return resolved; } if (resolved instanceof TypeVariable) { final TypeVariable typeVariable = (TypeVariable) resolved; index = ArrayUtilRt.find(ReflectionCache.getTypeParameters(anInterface), typeVariable); if (index < 0) { LOG.error( "Cannot resolve type variable:\n" + "typeVariable = " + typeVariable + "\n" + "genericDeclaration = " + declarationToString(typeVariable.getGenericDeclaration()) + "\n" + "searching in " + declarationToString(anInterface)); } final Type type = i < genericInterfaces.length ? genericInterfaces[i] : aClass.getGenericSuperclass(); if (type instanceof Class) { return Object.class; } if (type instanceof ParameterizedType) { return getActualTypeArguments((ParameterizedType) type)[index]; } throw new AssertionError("Invalid type: " + type); } } return null; }
@Nullable protected <T> T findChildByClass(Class<T> aClass) { for (PsiElement cur = getFirstChild(); cur != null; cur = cur.getNextSibling()) { if (ReflectionCache.isInstance(cur, aClass)) return (T) cur; } return null; }
@NotNull protected <T> T[] findChildrenByClass(Class<T> aClass) { List<T> result = new ArrayList<T>(); for (PsiElement cur = getFirstChild(); cur != null; cur = cur.getNextSibling()) { if (ReflectionCache.isInstance(cur, aClass)) result.add((T) cur); } return result.toArray((T[]) Array.newInstance(aClass, result.size())); }
public <T> T getCurrentWriteAction(@Nullable Class<T> actionClass) { assertCanRunWriteAction(); for (int i = myWriteActionsStack.size() - 1; i >= 0; i--) { Runnable action = myWriteActionsStack.get(i); if (actionClass == null || ReflectionCache.isAssignable(actionClass, action.getClass())) return (T) action; } return null; }
protected boolean isScopeFinal(Class scopeClass) { if (myFinalScopes.contains(scopeClass)) return true; for (final Class myFinalScope : myFinalScopes) { if (ReflectionCache.isAssignable(myFinalScope, scopeClass)) { return true; } } return false; }
public boolean hasWriteAction(@Nullable Class<?> actionClass) { assertCanRunWriteAction(); for (int i = myWriteActionsStack.size() - 1; i >= 0; i--) { Class action = myWriteActionsStack.get(i); if (actionClass == action || action != null && ReflectionCache.isAssignable(actionClass, action)) return true; } return false; }
public static Type resolveVariableInHierarchy(final TypeVariable variable, final Class aClass) { Type type; Class current = aClass; while ((type = resolveVariable(variable, current, false)) == null) { current = ReflectionCache.getSuperClass(current); if (current == null) { return null; } } if (type instanceof TypeVariable) { return resolveVariableInHierarchy((TypeVariable) type, aClass); } return type; }
@Nullable public PsiElement findElementAt(int offset, Class<? extends Language> lang) { final PsiFile mainRoot = getPsi(getBaseLanguage()); PsiElement ret = null; for (final Language language : getLanguages()) { if (!ReflectionCache.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; }
@Nullable protected static <T extends PsiNamedElement> T restoreElementInternal( @NotNull PsiElement parent, String name, int index, Class<T> hisClass) { PsiElement[] children = parent.getChildren(); for (PsiElement child : children) { if (ReflectionCache.isAssignable(hisClass, child.getClass())) { T namedChild = (T) child; final String childName = namedChild.getName(); if (Comparing.equal(name, childName)) { if (index == 0) { return namedChild; } index--; } } } return null; }
protected static <T extends PsiNamedElement> int getChildIndex( T element, PsiElement parent, String name, Class<T> hisClass) { PsiElement[] children = parent.getChildren(); int index = 0; for (PsiElement child : children) { if (ReflectionCache.isAssignable(hisClass, child.getClass())) { T namedChild = (T) child; final String childName = namedChild.getName(); if (Comparing.equal(name, childName)) { if (namedChild.equals(element)) { return index; } index++; } } } return index; }
@Nullable public static Class<?> substituteGenericType(final Type genericType, final Type classType) { if (genericType instanceof TypeVariable) { final Class<?> aClass = getRawType(classType); final Type type = resolveVariable((TypeVariable) genericType, aClass); if (type instanceof Class) { return (Class) type; } if (type instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) type).getRawType(); } if (type instanceof TypeVariable && classType instanceof ParameterizedType) { final int index = ArrayUtilRt.find(ReflectionCache.getTypeParameters(aClass), type); if (index >= 0) { return getRawType(getActualTypeArguments((ParameterizedType) classType)[index]); } } } else { return getRawType(genericType); } return null; }
@Override public boolean isClassAcceptable(Class hintClass) { return ReflectionCache.isAssignable(hintClass, PsiTypeCodeFragment.class); }
@Override public PsiElement findElementAt(int offset, Class<? extends Language> lang) { if (!ReflectionCache.isAssignable(lang, getBaseLanguage().getClass())) return null; return findElementAt(offset); }
@Nullable public static Method getMethod( @NotNull Class aClass, @NonNls @NotNull String name, Class... parameters) { return findMethod(ReflectionCache.getMethods(aClass), name, parameters); }
public static Type[] getActualTypeArguments(final ParameterizedType parameterizedType) { return ReflectionCache.getActualTypeArguments(parameterizedType); }