use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyDocumentationProvider method generateDocumentationContentStub.
@Override
public String generateDocumentationContentStub(PsiComment contextComment) {
if (!(contextComment instanceof GrDocComment)) {
return null;
}
final GrDocCommentOwner owner = GrDocCommentUtil.findDocOwner((GrDocComment) contextComment);
if (owner == null)
return null;
Project project = contextComment.getProject();
final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter) LanguageCommenters.INSTANCE.forLanguage(owner.getLanguage());
StringBuilder builder = StringBuilderSpinAllocator.alloc();
try {
if (owner instanceof GrMethod) {
final GrMethod method = (GrMethod) owner;
JavaDocumentationProvider.generateParametersTakingDocFromSuperMethods(project, builder, commenter, method);
final PsiType returnType = method.getInferredReturnType();
if ((returnType != null || method.getModifierList().hasModifierProperty(GrModifier.DEF)) && !PsiType.VOID.equals(returnType)) {
builder.append(CodeDocumentationUtil.createDocCommentLine(RETURN_TAG, project, commenter));
builder.append(LINE_SEPARATOR);
}
final PsiClassType[] references = method.getThrowsList().getReferencedTypes();
for (PsiClassType reference : references) {
builder.append(CodeDocumentationUtil.createDocCommentLine(THROWS_TAG, project, commenter));
builder.append(reference.getClassName());
builder.append(LINE_SEPARATOR);
}
} else if (owner instanceof GrTypeDefinition) {
final PsiTypeParameterList typeParameterList = ((PsiClass) owner).getTypeParameterList();
if (typeParameterList != null) {
JavaDocumentationProvider.createTypeParamsListComment(builder, project, commenter, typeParameterList);
}
}
return builder.length() > 0 ? builder.toString() : null;
} finally {
StringBuilderSpinAllocator.dispose(builder);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrChangeSignatureHandler method invoke.
private static void invoke(PsiMethod method, final Project project) {
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method))
return;
if (method instanceof GrReflectedMethod)
method = ((GrReflectedMethod) method).getBaseMethod();
PsiMethod newMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
if (newMethod == null)
return;
if (!newMethod.equals(method)) {
ChangeSignatureUtil.invokeChangeSignatureOn(newMethod, project);
return;
}
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method))
return;
//todo
if (!(method instanceof GrMethod))
return;
final GrChangeSignatureDialog dialog = new GrChangeSignatureDialog(project, new GrMethodDescriptor((GrMethod) method), true, null);
dialog.show();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class AnonymousFromMapGenerator method writeAnonymousMap.
static void writeAnonymousMap(GrListOrMap operand, GrTypeElement typeElement, final StringBuilder builder, ExpressionContext context) {
final PsiType type = typeElement.getType();
final PsiClass psiClass;
final PsiSubstitutor substitutor;
if (type instanceof PsiClassType) {
final PsiClassType.ClassResolveResult resolveResult = ((PsiClassType) type).resolveGenerics();
psiClass = resolveResult.getElement();
substitutor = resolveResult.getSubstitutor();
} else {
psiClass = null;
substitutor = PsiSubstitutor.EMPTY;
}
builder.append("new ");
TypeWriter.writeTypeForNew(builder, type, operand);
builder.append("() {\n");
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(operand.getProject());
final GrExpression caller = factory.createExpressionFromText("this");
for (GrNamedArgument arg : operand.getNamedArguments()) {
final String name = arg.getLabelName();
final GrExpression expression = arg.getExpression();
if (name == null || expression == null || !(expression instanceof GrClosableBlock))
continue;
final GrClosableBlock closure = (GrClosableBlock) expression;
final GrParameter[] allParameters = closure.getAllParameters();
List<GrParameter> actual = new ArrayList<>(Arrays.asList(allParameters));
final PsiType clReturnType = context.typeProvider.getReturnType(closure);
GrExpression[] args = new GrExpression[allParameters.length];
for (int i = 0; i < allParameters.length; i++) {
args[i] = factory.createExpressionFromText(allParameters[i].getName());
}
for (int param = allParameters.length; param >= 0; param--) {
if (param < allParameters.length && !actual.get(param).isOptional())
continue;
if (param < allParameters.length) {
final GrParameter opt = actual.remove(param);
args[param] = opt.getInitializerGroovy();
}
final GrParameter[] parameters = actual.toArray(new GrParameter[actual.size()]);
final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(parameters, clReturnType);
final GrMethod pattern = factory.createMethodFromSignature(name, signature);
PsiMethod found = null;
if (psiClass != null) {
found = psiClass.findMethodBySignature(pattern, true);
}
if (found != null) {
ModifierListGenerator.writeModifiers(builder, found.getModifierList(), ModifierListGenerator.JAVA_MODIFIERS_WITHOUT_ABSTRACT);
} else {
builder.append("public ");
}
PsiType returnType;
if (found != null) {
returnType = substitutor.substitute(context.typeProvider.getReturnType(found));
} else {
returnType = signature.getReturnType();
}
TypeWriter.writeType(builder, returnType, operand);
builder.append(' ').append(name);
GenerationUtil.writeParameterList(builder, parameters, new GeneratorClassNameProvider(), context);
final ExpressionContext extended = context.extend();
extended.setInAnonymousContext(true);
if (param == allParameters.length) {
new CodeBlockGenerator(builder, extended).generateCodeBlock(allParameters, closure, false);
} else {
builder.append("{\n");
final ExpressionGenerator expressionGenerator = new ExpressionGenerator(builder, extended);
GenerationUtil.invokeMethodByName(caller, name, args, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, expressionGenerator, arg);
builder.append(";\n}\n");
}
}
}
builder.append("}");
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class ExpressionGenerator method isImplicitlyCastedToArray.
private static boolean isImplicitlyCastedToArray(GrListOrMap list) {
PsiElement parent = list.getParent();
GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(list);
if (!(owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod))
return false;
if (!(parent instanceof GrReturnStatement || ControlFlowUtils.isReturnValue(list, owner)))
return false;
PsiType type = ((GrMethod) owner.getParent()).getReturnType();
return type instanceof PsiArrayType;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrChageSignatureUsageSearcher method findSimpleUsagesWithoutParameters.
private PsiMethod[] findSimpleUsagesWithoutParameters(final PsiMethod method, final ArrayList<UsageInfo> result, boolean isToModifyArgs, boolean isToThrowExceptions, boolean isOriginal) {
GlobalSearchScope projectScope = GlobalSearchScope.projectScope(method.getProject());
PsiMethod[] overridingMethods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
for (PsiMethod overridingMethod : overridingMethods) {
if (GroovyLanguage.INSTANCE.equals(overridingMethod.getLanguage())) {
result.add(new OverriderUsageInfo(overridingMethod, method, isOriginal, isToModifyArgs, isToThrowExceptions));
}
}
boolean needToChangeCalls = !myChangeInfo.isGenerateDelegate() && (myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged() || myChangeInfo.isVisibilityChanged());
if (needToChangeCalls) {
PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
for (PsiReference ref : refs) {
PsiElement element = ref.getElement();
if (!GroovyLanguage.INSTANCE.equals(element.getLanguage()))
continue;
boolean isToCatchExceptions = isToThrowExceptions && needToCatchExceptions(RefactoringUtil.getEnclosingMethod(element));
if (PsiUtil.isMethodUsage(element)) {
result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
} else if (element instanceof GrDocTagValueToken) {
result.add(new UsageInfo(ref.getElement()));
} else if (element instanceof GrMethod && ((GrMethod) element).isConstructor()) {
DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((GrMethod) element, ((GrMethod) element).getContainingClass(), method);
result.add(implicitUsageInfo);
} else if (element instanceof PsiClass) {
LOG.assertTrue(method.isConstructor());
final PsiClass psiClass = (PsiClass) element;
if (psiClass instanceof GrAnonymousClassDefinition) {
result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
continue;
}
/*if (!(myChangeInfo instanceof JavaChangeInfoImpl)) continue; todo propagate methods
if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
((JavaChangeInfoImpl)myChangeInfo).propagateParametersMethods)) {
continue;
}
if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
((JavaChangeInfoImpl)myChangeInfo).propagateExceptionsMethods)) {
continue;
}*/
result.add(new NoConstructorClassUsageInfo(psiClass));
} else if (ref instanceof PsiCallReference) {
result.add(new CallReferenceUsageInfo((PsiCallReference) ref));
} else {
result.add(new MoveRenameUsageInfo(element, ref, method));
}
}
} else if (myChangeInfo.isParameterTypesChanged()) {
PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
for (PsiReference reference : refs) {
final PsiElement element = reference.getElement();
if (element instanceof GrDocTagValueToken) {
result.add(new UsageInfo(reference));
}
}
}
// Conflicts
if (method instanceof GrMethod) {
detectLocalsCollisionsInMethod((GrMethod) method, result, isOriginal);
}
for (final PsiMethod overridingMethod : overridingMethods) {
if (overridingMethod instanceof GrMethod) {
detectLocalsCollisionsInMethod((GrMethod) overridingMethod, result, isOriginal);
}
}
return overridingMethods;
}
Aggregations