use of com.intellij.psi.PsiNewExpression in project kotlin by JetBrains.
the class ConstantEvaluator method isArrayLiteral.
/**
* Returns true if the node is pointing to a an array literal
*/
public static boolean isArrayLiteral(@Nullable PsiElement node) {
if (node instanceof PsiReference) {
PsiElement resolved = ((PsiReference) node).resolve();
if (resolved instanceof PsiField) {
PsiField field = (PsiField) resolved;
if (field.getInitializer() != null) {
return isArrayLiteral(field.getInitializer());
}
} else if (resolved instanceof PsiLocalVariable) {
PsiLocalVariable variable = (PsiLocalVariable) resolved;
PsiStatement statement = PsiTreeUtil.getParentOfType(node, PsiStatement.class, false);
if (statement != null) {
PsiStatement prev = PsiTreeUtil.getPrevSiblingOfType(statement, PsiStatement.class);
String targetName = variable.getName();
if (targetName == null) {
return false;
}
while (prev != null) {
if (prev instanceof PsiDeclarationStatement) {
for (PsiElement element : ((PsiDeclarationStatement) prev).getDeclaredElements()) {
if (variable.equals(element)) {
return isArrayLiteral(variable.getInitializer());
}
}
} else if (prev instanceof PsiExpressionStatement) {
PsiExpression expression = ((PsiExpressionStatement) prev).getExpression();
if (expression instanceof PsiAssignmentExpression) {
PsiAssignmentExpression assign = (PsiAssignmentExpression) expression;
PsiExpression lhs = assign.getLExpression();
if (lhs instanceof PsiReferenceExpression) {
PsiReferenceExpression reference = (PsiReferenceExpression) lhs;
if (targetName.equals(reference.getReferenceName()) && reference.getQualifier() == null) {
return isArrayLiteral(assign.getRExpression());
}
}
}
}
prev = PsiTreeUtil.getPrevSiblingOfType(prev, PsiStatement.class);
}
}
}
} else if (node instanceof PsiNewExpression) {
PsiNewExpression creation = (PsiNewExpression) node;
if (creation.getArrayInitializer() != null) {
return true;
}
PsiType type = creation.getType();
if (type instanceof PsiArrayType) {
return true;
}
} else if (node instanceof PsiParenthesizedExpression) {
PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression) node;
PsiExpression expression = parenthesizedExpression.getExpression();
if (expression != null) {
return isArrayLiteral(expression);
}
} else if (node instanceof PsiTypeCastExpression) {
PsiTypeCastExpression castExpression = (PsiTypeCastExpression) node;
PsiExpression operand = castExpression.getOperand();
if (operand != null) {
return isArrayLiteral(operand);
}
}
return false;
}
use of com.intellij.psi.PsiNewExpression in project intellij-community by JetBrains.
the class MissingArrayConstructorBracketFixer method apply.
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
if (!(psiElement instanceof PsiNewExpression))
return;
PsiNewExpression expr = (PsiNewExpression) psiElement;
int count = 0;
for (PsiElement element : expr.getChildren()) {
if (element.getNode().getElementType() == JavaTokenType.LBRACKET) {
count++;
} else if (element.getNode().getElementType() == JavaTokenType.RBRACKET) {
count--;
}
}
if (count > 0) {
editor.getDocument().insertString(psiElement.getTextRange().getEndOffset(), "]");
}
}
use of com.intellij.psi.PsiNewExpression in project intellij-community by JetBrains.
the class AnonymousToInnerAction method isAvailableOnElementInEditorAndFile.
protected boolean isAvailableOnElementInEditorAndFile(@NotNull final PsiElement element, @NotNull final Editor editor, @NotNull PsiFile file, @NotNull DataContext context) {
final PsiElement targetElement = file.findElementAt(editor.getCaretModel().getOffset());
if (PsiTreeUtil.getParentOfType(targetElement, PsiAnonymousClass.class) != null) {
return true;
}
if (PsiTreeUtil.getParentOfType(element, PsiAnonymousClass.class) != null) {
return true;
}
final PsiNewExpression newExpression = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class);
return newExpression != null && newExpression.getAnonymousClass() != null;
}
use of com.intellij.psi.PsiNewExpression in project oxy-template-support-plugin by mutant-industries.
the class LiteralJsMacroReferenceContributor method registerReferenceProviders.
@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiLiteralExpression.class), new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
PsiMethodCallExpression callExpression;
PsiNewExpression newMacroCallExpression;
if (!(literalExpression.getValue() instanceof String)) {
return PsiReference.EMPTY_ARRAY;
}
if ((newMacroCallExpression = PsiTreeUtil.getParentOfType(literalExpression, PsiNewExpressionImpl.class)) != null && newMacroCallExpression.getClassReference() != null) {
if (MacroCall.class.getName().equals(newMacroCallExpression.getClassReference().getQualifiedName())) {
return new MacroReferenceSet(literalExpression).getAllReferences();
}
} else if ((callExpression = PsiTreeUtil.getParentOfType(literalExpression, PsiMethodCallExpression.class)) != null) {
PsiExpression[] parameters = callExpression.getArgumentList().getExpressions();
PsiReferenceExpression expression = callExpression.getMethodExpression();
String callText = expression.getText();
if (// TODO type check...
callText.contains("ageUpdater.update")) {
if (parameters.length > 0 && literalExpression.isEquivalentTo(parameters[0])) {
// TODO template path reference
} else if (parameters.length > 1 && literalExpression.isEquivalentTo(parameters[1])) {
return new MacroReferenceSet(literalExpression).getAllReferences();
}
}
}
return PsiReference.EMPTY_ARRAY;
}
});
}
use of com.intellij.psi.PsiNewExpression in project oxy-template-support-plugin by mutant-industries.
the class ExtenderProvider method getExtenderFromProvider.
@Nullable
private static PsiClass getExtenderFromProvider(@NotNull PsiClass extenderProvider) {
PsiMethod[] provideMethods = extenderProvider.findMethodsByName(EXTENDER_GETTER_METHOD_MANE, true);
if (provideMethods.length < 2) {
return null;
}
/**
* return is always followed by a new expression, which contains class reference or anonymous class
*/
PsiNewExpression newExpression = PsiTreeUtil.findChildOfType(provideMethods[0], PsiNewExpression.class);
if (newExpression == null) {
return null;
}
PsiJavaCodeReferenceElement extenderClassReference = PsiTreeUtil.getNextSiblingOfType(newExpression.getFirstChild(), PsiJavaCodeReferenceElement.class);
PsiElement result;
if (extenderClassReference != null && (result = extenderClassReference.resolve()) instanceof PsiClass) {
return (PsiClass) result;
}
PsiAnonymousClass anonymousClass = PsiTreeUtil.getNextSiblingOfType(newExpression.getFirstChild(), PsiAnonymousClass.class);
if (anonymousClass != null) {
return anonymousClass;
}
return null;
}
Aggregations