Java Code Examples for com.intellij.psi.util.PsiTreeUtil#getChildOfType()

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#getChildOfType() . These examples are extracted from open source projects. 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 Project: litho   File: PsiAnnotationProxyUtils.java    License: Apache License 2.0 6 votes vote down vote up
private Object invoke(Method method, Object[] args) throws IllegalAccessException {
  Class<?> returnType = method.getReturnType();
  if (returnType.isEnum()) {
    PsiAnnotation currentAnnotation =
        AnnotationUtil.findAnnotationInHierarchy(
            mListOwner, Collections.singleton(mAnnotationClass.getCanonicalName()));
    PsiReferenceExpression declaredValue =
        (PsiReferenceExpression) currentAnnotation.findAttributeValue(method.getName());
    if (declaredValue == null) {
      return method.getDefaultValue();
    }
    PsiIdentifier identifier = PsiTreeUtil.getChildOfType(declaredValue, PsiIdentifier.class);
    return Enum.valueOf((Class<Enum>) returnType, identifier.getText());
  }

  try {
    if (args == null) {
      return method.invoke(mStubbed);
    }
    return method.invoke(mStubbed, args);
  } catch (InvocationTargetException e) {
    return method.getDefaultValue();
  }
}
 
Example 2
@Override
public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
{
	DotNetType element = myPointer.getElement();
	if(element == null)
	{
		return;
	}

	CSharpFragmentFileImpl typeFragment = CSharpFragmentFactory.createTypeFragment(project, myTypeText, element);

	DotNetType newType = PsiTreeUtil.getChildOfType(typeFragment, DotNetType.class);
	if(newType == null)
	{
		return;
	}

	element.replace(newType);
}
 
Example 3
Source Project: intellij-haxe   File: HaxePsiFieldImpl.java    License: Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public PsiType getType() {
  PsiType psiType = null;
  final HaxeTypeTag tag = PsiTreeUtil.getChildOfType(this, HaxeTypeTag.class);
  if (tag != null) {
    final HaxeTypeOrAnonymous toa = tag.getTypeOrAnonymous();
    final HaxeType type = (toa != null) ? toa.getType() : null;
    psiType = (type != null) ? type.getPsiType() : null;
  }
  return psiType != null ? psiType : HaxePsiTypeAdapter.DYNAMIC;
}
 
Example 4
default PsiElement argumentsToken() {
    if (this instanceof CypherFunctionInvocation) {
        return PsiTreeUtil.getChildOfType(this, CypherArgumentList.class);
    }
    if (this instanceof CypherProcedureInvocation) {
        return PsiTreeUtil.getChildOfType(this, CypherArgumentList.class);
    } else {
        return this;
    }

}
 
Example 5
@Override
public List<ThriftDeclaration> findSubDeclarations() {
  ThriftDeclarationBody body = PsiTreeUtil.getChildOfType(this, ThriftDeclarationBody.class);
  if (body == null) {
    return Collections.emptyList();
  }
  return PsiTreeUtil.getChildrenOfTypeAsList(body, ThriftDeclaration.class);
}
 
Example 6
@Nullable
private static PsiElement findTestSuiteReference(PsiElement element) {
  if (element instanceof JSVarStatement) {
    // variable assignment might be
    // testSuite = goog.require('goog.testing.testSuite')
    JSVariable variable = PsiTreeUtil.getChildOfType(element, JSVariable.class);
    if (variable != null && isImportingTestSuite(variable)) {
      return variable;
    }
  } else if (element instanceof JSExpressionStatement) {
    // expression statement might be
    // goog.require('goog.testing.testSuite')
    if (isImportingTestSuite(element)) {
      JSLiteralExpression literal =
          PsiTreeUtil.findChildOfType(element, JSLiteralExpression.class);
      // this should be 'goog.testing.testSuite'
      if (literal == null) {
        return null;
      }
      for (PsiReference reference : literal.getReferences()) {
        if (reference instanceof JSGclModuleReference) {
          // this should be testSuite, and should resolve to the function
          return reference.resolve();
        }
      }
    }
  }
  return null;
}
 
Example 7
@NotNull
public SQFCodeBlock getBlock() {
	SQFCodeBlock block = PsiTreeUtil.getChildOfType(this, SQFCodeBlock.class);
	if (block == null) {
		throw new IllegalStateException("the block returned shouldn't be null");
	}
	return block;
}
 
Example 8
/**
 * fo<caret>o:
 *   class: foo
 *   arguments: []
 */
@Nullable
public static ServiceYamlContainer create(@NotNull YAMLKeyValue yamlServiceKeyValue) {
    YAMLMapping childOfType = PsiTreeUtil.getChildOfType(yamlServiceKeyValue, YAMLMapping.class);
    if(childOfType == null) {
        return null;
    }

    String serviceClass = null;

    YAMLKeyValue aClass = childOfType.getKeyValueByKey("class");
    if(aClass != null) {
        // service:
        //  class: Foobar
        YAMLValue value = aClass.getValue();
        if(value instanceof YAMLScalar) {
            serviceClass =  ((YAMLScalar) value).getTextValue();
        }
    } else {
        // Foobar:
        //  argument: ~
        String keyText = yamlServiceKeyValue.getKeyText();

        if(StringUtils.isNotBlank(keyText) && YamlHelper.isClassServiceId(keyText)) {
            serviceClass = keyText;
        }
    }

    if (StringUtils.isBlank(serviceClass)) {
        return null;
    }

    return new ServiceYamlContainer(yamlServiceKeyValue, childOfType.getKeyValueByKey("arguments"), serviceClass);
}
 
Example 9
@Override
@Nullable
public HaskellConsym getConsym() {
  return PsiTreeUtil.getChildOfType(this, HaskellConsym.class);
}
 
Example 10
@Nullable
public static HaxePackageStatement createPackageStatementFromPath(Project myProject, String path) {
  final PsiFile dummyFile = createDummyFile(myProject, "package " + path + ";");
  return PsiTreeUtil.getChildOfType(dummyFile, HaxePackageStatement.class);
}
 
Example 11
@Override
@Nullable
public HaskellBkind getBkind() {
  return PsiTreeUtil.getChildOfType(this, HaskellBkind.class);
}
 
Example 12
@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
    if (DumbService.getInstance(psiElement.getProject()).isDumb() || !TYPO3CMSProjectSettings.isEnabled(psiElement)) {
        return null;
    }

    // $GLOBALS['TYPO3_DB']
    if (!(psiElement instanceof ArrayAccessExpression)) {
        return null;
    }

    VariableImpl variable = PsiTreeUtil.getChildOfType(psiElement, VariableImpl.class);
    if (variable == null || !variable.getName().equals("GLOBALS")) {
        return null;
    }

    ArrayIndex arrayIndex = PsiTreeUtil.getChildOfType(psiElement, ArrayIndex.class);
    if (arrayIndex == null) {
        return null;
    }

    StringLiteralExpression arrayIndexName = PsiTreeUtil.getChildOfType(arrayIndex, StringLiteralExpressionImpl.class);
    if (arrayIndexName == null) {
        return null;
    }

    switch (arrayIndexName.getContents()) {
        case "TYPO3_DB":
            return new PhpType().add("#C\\TYPO3\\CMS\\Core\\Database\\DatabaseConnection");
        case "TSFE":
            return new PhpType().add("#C\\TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController");
        case "BE_USER":
            return new PhpType().add("#C\\TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication");
        case "LANG":
            return new PhpType().add("#C\\TYPO3\\CMS\\Lang\\LanguageService");
        case "TYPO3_REQUEST":
            return new PhpType().add("#C\\Psr\\Http\\Message\\ServerRequestInterface");
        default:
            return null;
    }
}
 
Example 13
Source Project: silex-idea-plugin   File: Utils.java    License: MIT License 4 votes vote down vote up
private static Container findContainerForPimpleArrayAccess(ArrayAccessExpression arrayAccessElement, Boolean onlyParentContainers) {

        PsiElement children;
        PsiElement element = arrayAccessElement;
        while ((children = PsiTreeUtil.getChildOfType(element, ArrayAccessExpression.class)) != null) {
            element = children;
        }

        // check if var is pimple container
        Signature signature = new Signature();

        PsiElement signatureElement = PsiTreeUtil.getChildOfAnyType(element, Variable.class, FieldReference.class);
        if (signatureElement == null) {
            return null;
        }

        if (signatureElement instanceof Variable) {
            signature.set(((Variable) signatureElement).getSignature());
        }

        if (signatureElement instanceof FieldReference) {
            signature.set(((FieldReference) signatureElement).getSignature());
        }

        PhpIndex phpIndex = PhpIndex.getInstance(arrayAccessElement.getProject());

        ArrayList<String> parameters = new ArrayList<String>();
        if (!findPimpleContainer(phpIndex, signature.base, parameters)) {
            return null;
        }

        Container container = ContainerResolver.get(arrayAccessElement.getProject());

        // find proper base container from signature
        for (String parameter : parameters) {
            container = container.getContainers().get(getResolvedParameter(phpIndex, parameter));
            if (container == null)
                return null;
        }

        PsiElement lastElement = onlyParentContainers ? arrayAccessElement : arrayAccessElement.getParent();

        // find proper container
        while (!element.isEquivalentTo(lastElement) ) {

            ArrayIndex arrayIndex = ((ArrayAccessExpression)element).getIndex();
            if (arrayIndex == null) {
                return null;
            }

            PsiElement arrayIndexElement = arrayIndex.getValue();
            if (arrayIndexElement == null) {
                return null;
            }

            String containerName;

            if (arrayIndexElement instanceof StringLiteralExpression) {
                containerName = ((StringLiteralExpression) arrayIndexElement).getContents();
            }
            else if (arrayIndexElement instanceof MemberReference) {
                containerName = getResolvedParameter(phpIndex, ((MemberReference) arrayIndexElement).getSignature());
            }
            else return null;

            container = container.getContainers().get(containerName);
            if (container == null) {
                return null;
            }

            element = element.getParent();
        }

        return container;

    }
 
Example 14
@Override
@Nullable
public HaskellVarop getVarop() {
  return PsiTreeUtil.getChildOfType(this, HaskellVarop.class);
}
 
Example 15
@Override
@Nullable
public HaskellGconsym getGconsym() {
  return PsiTreeUtil.getChildOfType(this, HaskellGconsym.class);
}
 
Example 16
/**
 * Retrieve the (first) catch statement that belongs to the given element.
 */
public static HaxeCatchStatement getCatchStatement(PsiElement elem) {
  return PsiTreeUtil.getChildOfType(elem, HaxeCatchStatement.class);
}
 
Example 17
@Override
@Nullable
public HaskellQtyconsym getQtyconsym() {
  return PsiTreeUtil.getChildOfType(this, HaskellQtyconsym.class);
}
 
Example 18
@Override
@Nullable
public HaskellQtyconop getQtyconop() {
  return PsiTreeUtil.getChildOfType(this, HaskellQtyconop.class);
}
 
Example 19
@Override
@Nullable
public HaskellConop getConop() {
  return PsiTreeUtil.getChildOfType(this, HaskellConop.class);
}
 
Example 20
@Override
@Nullable
public HaskellVars getVars() {
  return PsiTreeUtil.getChildOfType(this, HaskellVars.class);
}