use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition in project intellij-community by JetBrains.
the class ReplaceAbstractClassInstanceByMapIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement psiElement, @NotNull Project project, Editor editor) throws IncorrectOperationException {
PsiDocumentManager.getInstance(project).commitAllDocuments();
GrCodeReferenceElement ref = (GrCodeReferenceElement) psiElement;
final GrAnonymousClassDefinition anonymous = (GrAnonymousClassDefinition) ref.getParent();
final GrNewExpression newExpr = (GrNewExpression) anonymous.getParent();
final PsiElement resolved = ref.resolve();
// && ((PsiClass)resolved).isInterface();
assert resolved instanceof PsiClass;
GrTypeDefinitionBody body = anonymous.getBody();
assert body != null;
List<Pair<PsiMethod, GrOpenBlock>> methods = new ArrayList<>();
for (GrMethod method : body.getMethods()) {
methods.add(new Pair<>(method, method.getBlock()));
}
final PsiClass iface = (PsiClass) resolved;
final Collection<CandidateInfo> collection = OverrideImplementExploreUtil.getMethodsToOverrideImplement(anonymous, true);
for (CandidateInfo info : collection) {
methods.add(new Pair<>((PsiMethod) info.getElement(), null));
}
StringBuilder buffer = new StringBuilder();
if (methods.size() == 1) {
final Pair<PsiMethod, GrOpenBlock> pair = methods.get(0);
appendClosureTextByMethod(pair.getFirst(), buffer, pair.getSecond(), newExpr);
if (!GroovyConfigUtils.getInstance().isVersionAtLeast(psiElement, GroovyConfigUtils.GROOVY2_2)) {
buffer.append(" as ").append(iface.getQualifiedName());
}
} else {
buffer.append("[");
buffer.append("\n");
for (Pair<PsiMethod, GrOpenBlock> pair : methods) {
final PsiMethod method = pair.getFirst();
final GrOpenBlock block = pair.getSecond();
buffer.append(method.getName()).append(": ");
appendClosureTextByMethod(method, buffer, block, newExpr);
buffer.append(",\n");
}
if (!methods.isEmpty()) {
buffer.delete(buffer.length() - 2, buffer.length());
buffer.append('\n');
}
buffer.append("]");
buffer.append(" as ").append(iface.getQualifiedName());
}
createAndAdjustNewExpression(project, newExpr, buffer);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition in project intellij-community by JetBrains.
the class GroovyConstructorUsagesSearcher method processGroovyConstructorUsages.
private static boolean processGroovyConstructorUsages(GrCodeReferenceElement element, final Processor<GrNewExpression> newExpressionProcessor, final LiteralConstructorSearcher literalProcessor) {
PsiElement parent = element.getParent();
if (parent instanceof GrAnonymousClassDefinition) {
parent = parent.getParent();
}
if (parent instanceof GrNewExpression) {
return newExpressionProcessor.process((GrNewExpression) parent);
}
if (parent instanceof GrTypeElement) {
final GrTypeElement typeElement = (GrTypeElement) parent;
final PsiElement grandpa = typeElement.getParent();
if (grandpa instanceof GrVariableDeclaration) {
final GrVariable[] vars = ((GrVariableDeclaration) grandpa).getVariables();
if (vars.length == 1) {
final GrVariable variable = vars[0];
if (!checkLiteralInstantiation(variable.getInitializerGroovy(), literalProcessor)) {
return false;
}
}
} else if (grandpa instanceof GrMethod) {
final GrMethod method = (GrMethod) grandpa;
if (typeElement == method.getReturnTypeElementGroovy()) {
ControlFlowUtils.visitAllExitPoints(method.getBlock(), new ControlFlowUtils.ExitPointVisitor() {
@Override
public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
if (!checkLiteralInstantiation(returnValue, literalProcessor)) {
return false;
}
return true;
}
});
}
} else if (grandpa instanceof GrTypeCastExpression) {
final GrTypeCastExpression cast = (GrTypeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
} else if (grandpa instanceof GrSafeCastExpression) {
final GrSafeCastExpression cast = (GrSafeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition in project intellij-community by JetBrains.
the class ResolveUtil method getCallVariants.
public static GroovyResolveResult[] getCallVariants(GroovyPsiElement place) {
final PsiElement parent = place.getParent();
GroovyResolveResult[] variants = GroovyResolveResult.EMPTY_ARRAY;
if (parent instanceof GrCallExpression) {
variants = ((GrCallExpression) parent).getCallVariants(place instanceof GrExpression ? (GrExpression) place : null);
} else if (parent instanceof GrConstructorInvocation) {
final PsiClass clazz = ((GrConstructorInvocation) parent).getDelegatedClass();
if (clazz != null) {
final PsiMethod[] constructors = clazz.getConstructors();
variants = getConstructorResolveResult(constructors, place);
}
} else if (parent instanceof GrAnonymousClassDefinition) {
final PsiElement element = ((GrAnonymousClassDefinition) parent).getBaseClassReferenceGroovy().resolve();
if (element instanceof PsiClass) {
final PsiMethod[] constructors = ((PsiClass) element).getConstructors();
variants = getConstructorResolveResult(constructors, place);
}
} else if (place instanceof GrReferenceExpression) {
variants = ((GrReferenceExpression) place).getSameNameVariants();
}
return variants;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition in project intellij-community by JetBrains.
the class SuppressForMemberFix method getContainer.
@Override
@Nullable
public GrDocCommentOwner getContainer(final PsiElement context) {
if (context == null || context instanceof PsiFile) {
return null;
}
GrDocCommentOwner container = null;
GrDocComment docComment = PsiTreeUtil.getParentOfType(context, GrDocComment.class);
if (docComment != null) {
container = docComment.getOwner();
}
if (container == null) {
container = PsiTreeUtil.getParentOfType(context, GrDocCommentOwner.class);
}
while (container instanceof GrAnonymousClassDefinition || container instanceof GrTypeParameter) {
container = PsiTreeUtil.getParentOfType(container, GrDocCommentOwner.class);
if (container == null)
return null;
}
if (myForClass) {
while (container != null) {
final GrTypeDefinition parentClass = PsiTreeUtil.getParentOfType(container, GrTypeDefinition.class);
if (parentClass == null && container instanceof GrTypeDefinition) {
return container;
}
container = parentClass;
}
}
return container;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition in project intellij-community by JetBrains.
the class EachToForIntention method updateReturnStatements.
private static GrForStatement updateReturnStatements(GrForStatement forStatement) {
GrStatement body = forStatement.getBody();
assert body != null;
final Set<String> usedLabels = ContainerUtil.newHashSet();
final Ref<Boolean> needLabel = Ref.create(false);
body.accept(new GroovyRecursiveElementVisitor() {
private int myLoops = 0;
@Override
public void visitReturnStatement(@NotNull GrReturnStatement returnStatement) {
if (returnStatement.getReturnValue() != null)
return;
if (myLoops > 0)
needLabel.set(true);
}
@Override
public void visitLabeledStatement(@NotNull GrLabeledStatement labeledStatement) {
super.visitLabeledStatement(labeledStatement);
usedLabels.add(labeledStatement.getName());
}
@Override
public void visitForStatement(@NotNull GrForStatement forStatement) {
myLoops++;
super.visitForStatement(forStatement);
myLoops--;
}
@Override
public void visitWhileStatement(@NotNull GrWhileStatement whileStatement) {
myLoops++;
super.visitWhileStatement(whileStatement);
myLoops--;
}
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
//don't go into closures
}
@Override
public void visitAnonymousClassDefinition(@NotNull GrAnonymousClassDefinition anonymousClassDefinition) {
//don't go into anonymous
}
});
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(forStatement.getProject());
final String continueText;
if (needLabel.get()) {
int i = 0;
String label = OUTER;
while (usedLabels.contains(label)) {
label = OUTER + i;
i++;
}
continueText = "continue " + label;
GrLabeledStatement labeled = (GrLabeledStatement) factory.createStatementFromText(label + ": while (true){}");
labeled.getStatement().replaceWithStatement(forStatement);
labeled = forStatement.replaceWithStatement(labeled);
forStatement = (GrForStatement) labeled.getStatement();
body = forStatement.getBody();
assert body != null;
} else {
continueText = "continue";
}
final GrStatement continueStatement = factory.createStatementFromText(continueText);
body.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReturnStatement(@NotNull GrReturnStatement returnStatement) {
if (returnStatement.getReturnValue() == null) {
returnStatement.replaceWithStatement(continueStatement);
}
}
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
//don't go into closures
}
@Override
public void visitAnonymousClassDefinition(@NotNull GrAnonymousClassDefinition anonymousClassDefinition) {
//don't go into anonymous
}
});
return forStatement;
}
Aggregations