Java Code Examples for com.intellij.util.ReflectionUtil#isAssignable()

The following examples show how to use com.intellij.util.ReflectionUtil#isAssignable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiplePsiFilesPerDocumentFileViewProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nullable
public PsiElement findElementAt(int offset, @Nonnull 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;
}
 
Example 2
Source File: AbstractElementSignatureProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
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 (ReflectionUtil.isAssignable(hisClass, child.getClass())) {
      T namedChild = hisClass.cast(child);
      final String childName = namedChild.getName();

      if (Comparing.equal(name, childName)) {
        if (namedChild.equals(element)) {
          return index;
        }
        index++;
      }
    }
  }

  return index;
}
 
Example 3
Source File: AbstractElementSignatureProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
protected static <T extends PsiNamedElement> T restoreElementInternal(@Nonnull PsiElement parent,
                                                                      String name,
                                                                      int index,
                                                                      @Nonnull Class<T> hisClass)
{
  PsiElement[] children = parent.getChildren();

  for (PsiElement child : children) {
    if (ReflectionUtil.isAssignable(hisClass, child.getClass())) {
      T namedChild = hisClass.cast(child);
      final String childName = namedChild.getName();

      if (Comparing.equal(name, childName)) {
        if (index == 0) {
          return namedChild;
        }
        index--;
      }
    }
  }

  return null;
}
 
Example 4
Source File: StructureViewFactoryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<StructureViewExtension> getAllExtensions(Class<? extends PsiElement> type) {
  Collection<StructureViewExtension> result = myImplExtensions.get(type);
  if (result == null) {
    MultiValuesMap<Class<? extends PsiElement>, StructureViewExtension> map = myExtensions.getValue();
    for (Class<? extends PsiElement> registeredType : map.keySet()) {
      if (ReflectionUtil.isAssignable(registeredType, type)) {
        final Collection<StructureViewExtension> extensions = map.get(registeredType);
        for (StructureViewExtension extension : extensions) {
          myImplExtensions.put(type, extension);
        }
      }
    }
    result = myImplExtensions.get(type);
    if (result == null) return Collections.emptyList();
  }
  return result;
}
 
Example 5
Source File: SlingModuleFacet.java    From aem-ide-tooling-4-intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private static <T> T getRootElement(final PsiFile file, final Class<T> domClass, final Module module) {
    if(!(file instanceof XmlFile)) return null;
    final DomManager domManager = DomManager.getDomManager(file.getProject());
    final DomFileElement<DomElement> element = domManager.getFileElement((XmlFile) file, DomElement.class);
    if(element == null) return null;
    final DomElement root = element.getRootElement();
    if(!ReflectionUtil.isAssignable(domClass, root.getClass())) return null;
    return (T) root;
}
 
Example 6
Source File: ComponentTreeWatcher.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean shouldBeIgnored(Object object) {
  if (object instanceof CellRendererPane) return true;
  if (object == null) {
    return true;
  }
  for (Class aClass : myControlsToIgnore) {
    if (ReflectionUtil.isAssignable(aClass, object.getClass())) {
      return true;
    }
  }
  return false;
}
 
Example 7
Source File: TextEditorBasedStructureViewModel.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected boolean isSuitable(final PsiElement element) {
  if (element == null) return false;
  final Class[] suitableClasses = getSuitableClasses();
  for (Class suitableClass : suitableClasses) {
    if (ReflectionUtil.isAssignable(suitableClass, element.getClass())) return true;
  }
  return false;
}
 
Example 8
Source File: CodeInsightUtilCore.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
private static <T extends PsiElement> T findElementInRange(@Nonnull PsiFile file,
                                                           int startOffset,
                                                           int endOffset,
                                                           @Nonnull Class<T> klass,
                                                           @Nonnull Language language,
                                                           @Nullable PsiElement initialElement) {
  PsiElement element1 = file.getViewProvider().findElementAt(startOffset, language);
  PsiElement element2 = file.getViewProvider().findElementAt(endOffset - 1, language);
  if (element1 instanceof PsiWhiteSpace) {
    startOffset = element1.getTextRange().getEndOffset();
    element1 = file.getViewProvider().findElementAt(startOffset, language);
  }
  if (element2 instanceof PsiWhiteSpace) {
    endOffset = element2.getTextRange().getStartOffset();
    element2 = file.getViewProvider().findElementAt(endOffset - 1, language);
  }
  if (element2 == null || element1 == null) return null;
  final PsiElement commonParent = PsiTreeUtil.findCommonParent(element1, element2);
  final T element =
          ReflectionUtil.isAssignable(klass, commonParent.getClass())
          ? (T)commonParent : PsiTreeUtil.getParentOfType(commonParent, klass);

  if (element == initialElement) {
    return element;
  }

  if (element == null || element.getTextRange().getStartOffset() != startOffset || element.getTextRange().getEndOffset() != endOffset) {
    return null;
  }
  return element;
}
 
Example 9
Source File: CompletionVariant.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean isScopeClassFinal(Class scopeClass){
  for (final Object myScopeClass : myScopeClasses) {
    Scope scope = (Scope)myScopeClass;
    if (ReflectionUtil.isAssignable(scope.myClass, scopeClass) && scope.myIsFinalScope) {
      return true;
    }
  }
  return false;
}
 
Example 10
Source File: CompletionVariant.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean isScopeClassAcceptable(Class scopeClass){
  boolean ret = false;

  for (final Object myScopeClass : myScopeClasses) {
    final Class aClass = ((Scope)myScopeClass).myClass;
    if (ReflectionUtil.isAssignable(aClass, scopeClass)) {
      ret = true;
      break;
    }
  }

  return ret;
}
 
Example 11
Source File: UnresolvedReferenceQuickFixProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T extends PsiReference> void registerReferenceFixes(T ref, QuickFixActionRegistrar registrar) {

    final boolean dumb = DumbService.getInstance(ref.getElement().getProject()).isDumb();
    UnresolvedReferenceQuickFixProvider[] fixProviders = Extensions.getExtensions(EXTENSION_NAME);
    Class<? extends PsiReference> referenceClass = ref.getClass();
    for (UnresolvedReferenceQuickFixProvider each : fixProviders) {
      if (dumb && !DumbService.isDumbAware(each)) {
        continue;
      }
      if (ReflectionUtil.isAssignable(each.getReferenceClass(), referenceClass)) {
        each.registerFixes(ref, registrar);
      }
    }
  }
 
Example 12
Source File: SingleRootFileViewProvider.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public PsiElement findElementAt(int offset, @Nonnull Class<? extends Language> lang) {
  if (!ReflectionUtil.isAssignable(lang, getBaseLanguage().getClass())) return null;
  return findElementAt(offset);
}
 
Example 13
Source File: ClassFilter.java    From consulo with Apache License 2.0 4 votes vote down vote up
private boolean filterMatches(final Class hintClass) {
  return ReflectionUtil.isAssignable(myFilter, hintClass);
}