use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class TraitMethodImplementor method createImplementationPrototypes.
@NotNull
@Override
public PsiMethod[] createImplementationPrototypes(PsiClass inClass, PsiMethod method) throws IncorrectOperationException {
if (!(inClass instanceof GrTypeDefinition && method instanceof GrTraitMethod))
return PsiMethod.EMPTY_ARRAY;
final PsiClass containingClass = method.getContainingClass();
PsiSubstitutor substitutor = inClass.isInheritor(containingClass, true) ? TypeConversionUtil.getSuperClassSubstitutor(containingClass, inClass, PsiSubstitutor.EMPTY) : PsiSubstitutor.EMPTY;
return new GrMethod[] { GroovyOverrideImplementUtil.generateTraitMethodPrototype((GrTypeDefinition) inClass, (GrTraitMethod) method, substitutor) };
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrAnnotatorImpl method annotate.
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (FileIndexFacade.getInstance(element.getProject()).isInLibrarySource(element.getContainingFile().getVirtualFile()))
return;
if (element instanceof GroovyPsiElement) {
final GroovyAnnotator annotator = new GroovyAnnotator(holder);
((GroovyPsiElement) element).accept(annotator);
if (PsiUtil.isCompileStatic(element)) {
final GroovyStaticTypeCheckVisitor typeCheckVisitor = myTypeCheckVisitorThreadLocal.get();
assert typeCheckVisitor != null;
typeCheckVisitor.accept((GroovyPsiElement) element, holder);
}
} else if (element instanceof PsiComment) {
String text = element.getText();
if (text.startsWith("/*") && !(text.endsWith("*/"))) {
TextRange range = element.getTextRange();
holder.createErrorAnnotation(TextRange.create(range.getEndOffset() - 1, range.getEndOffset()), GroovyBundle.message("doc.end.expected"));
}
} else {
final PsiElement parent = element.getParent();
if (parent instanceof GrMethod) {
if (element.equals(((GrMethod) parent).getNameIdentifierGroovy()) && ((GrMethod) parent).getReturnTypeElementGroovy() == null) {
GroovyAnnotator.checkMethodReturnType((GrMethod) parent, element, holder);
}
} else if (parent instanceof GrField) {
final GrField field = (GrField) parent;
if (element.equals(field.getNameIdentifierGroovy())) {
final GrAccessorMethod[] getters = field.getGetters();
for (GrAccessorMethod getter : getters) {
GroovyAnnotator.checkMethodReturnType(getter, field.getNameIdentifierGroovy(), holder);
}
final GrAccessorMethod setter = field.getSetter();
if (setter != null) {
GroovyAnnotator.checkMethodReturnType(setter, field.getNameIdentifierGroovy(), holder);
}
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrMethodBaseImpl method setName.
@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
PsiElement nameElement = getNameIdentifierGroovy();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(nameElement.getProject());
PsiElement newNameElement;
if (PsiNameHelper.getInstance(getProject()).isIdentifier(name)) {
try {
GrMethod method = factory.createMethod(name, null);
newNameElement = method.getNameIdentifierGroovy();
} catch (IncorrectOperationException e) {
newNameElement = factory.createLiteralFromValue(name).getFirstChild();
}
} else {
newNameElement = factory.createLiteralFromValue(name).getFirstChild();
}
nameElement.replace(newNameElement);
return this;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class DuplicatesUtil method collectMethodDuplicates.
public static void collectMethodDuplicates(Map<GrMethod, List<GrMethod>> map, HashSet<GrMethod> duplicateMethodsWarning, HashSet<GrMethod> duplicateMethodsErrors) {
for (GrMethod method : map.keySet()) {
List<GrMethod> duplicateMethods = map.get(method);
if (duplicateMethods != null && duplicateMethods.size() > 1) {
HashMap<PsiType, GrMethod> duplicateMethodsToReturnTypeMap = new HashMap<>();
for (GrMethod duplicateMethod : duplicateMethods) {
GrTypeElement typeElement = duplicateMethod.getReturnTypeElementGroovy();
PsiType methodReturnType;
if (typeElement != null) {
methodReturnType = typeElement.getType();
} else {
methodReturnType = PsiType.NULL;
}
duplicateMethodsWarning.add(duplicateMethod);
GrMethod grMethodWithType = duplicateMethodsToReturnTypeMap.get(methodReturnType);
if (grMethodWithType != null) {
duplicateMethodsErrors.add(duplicateMethod);
duplicateMethodsErrors.add(grMethodWithType);
duplicateMethodsWarning.remove(duplicateMethod);
duplicateMethodsWarning.remove(grMethodWithType);
}
duplicateMethodsToReturnTypeMap.put(methodReturnType, duplicateMethod);
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class IntentionUtils method createTemplateForMethod.
public static void createTemplateForMethod(PsiType[] argTypes, ChooseTypeExpression[] paramTypesExpressions, PsiMethod method, PsiClass owner, TypeConstraint[] constraints, boolean isConstructor, @NotNull final PsiElement context) {
final Project project = owner.getProject();
PsiTypeElement typeElement = method.getReturnTypeElement();
ChooseTypeExpression expr = new ChooseTypeExpression(constraints, PsiManager.getInstance(project), context.getResolveScope(), method.getLanguage() == GroovyLanguage.INSTANCE);
TemplateBuilderImpl builder = new TemplateBuilderImpl(method);
if (!isConstructor) {
assert typeElement != null;
builder.replaceElement(typeElement, expr);
}
PsiParameter[] parameters = method.getParameterList().getParameters();
assert parameters.length == argTypes.length;
for (int i = 0; i < parameters.length; i++) {
PsiParameter parameter = parameters[i];
PsiTypeElement parameterTypeElement = parameter.getTypeElement();
builder.replaceElement(parameterTypeElement, paramTypesExpressions[i]);
builder.replaceElement(parameter.getNameIdentifier(), new ParameterNameExpression(null));
}
PsiCodeBlock body = method.getBody();
if (body != null) {
PsiElement lbrace = body.getLBrace();
assert lbrace != null;
builder.setEndVariableAfter(lbrace);
} else {
builder.setEndVariableAfter(method.getParameterList());
}
method = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(method);
Template template = builder.buildTemplate();
final PsiFile targetFile = owner.getContainingFile();
final Editor newEditor = positionCursor(project, targetFile, method);
TextRange range = method.getTextRange();
newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
TemplateManager manager = TemplateManager.getInstance(project);
TemplateEditingListener templateListener = new TemplateEditingAdapter() {
@Override
public void templateFinished(Template template, boolean brokenOff) {
ApplicationManager.getApplication().runWriteAction(() -> {
PsiDocumentManager.getInstance(project).commitDocument(newEditor.getDocument());
final int offset = newEditor.getCaretModel().getOffset();
PsiMethod method1 = PsiTreeUtil.findElementOfClassAtOffset(targetFile, offset - 1, PsiMethod.class, false);
if (context instanceof PsiMethod) {
final PsiTypeParameter[] typeParameters = ((PsiMethod) context).getTypeParameters();
if (typeParameters.length > 0) {
for (PsiTypeParameter typeParameter : typeParameters) {
if (CreateMethodFromUsageFix.checkTypeParam(method1, typeParameter)) {
final JVMElementFactory factory = JVMElementFactories.getFactory(method1.getLanguage(), method1.getProject());
PsiTypeParameterList list = method1.getTypeParameterList();
if (list == null) {
PsiTypeParameterList newList = factory.createTypeParameterList();
list = (PsiTypeParameterList) method1.addAfter(newList, method1.getModifierList());
}
list.add(factory.createTypeParameter(typeParameter.getName(), typeParameter.getExtendsList().getReferencedTypes()));
}
}
}
}
if (method1 != null) {
try {
final boolean hasNoReturnType = method1.getReturnTypeElement() == null && method1 instanceof GrMethod;
if (hasNoReturnType) {
((GrMethod) method1).setReturnType(PsiType.VOID);
}
if (method1.getBody() != null) {
FileTemplateManager templateManager = FileTemplateManager.getInstance(project);
FileTemplate fileTemplate = templateManager.getCodeTemplate(GroovyTemplates.GROOVY_FROM_USAGE_METHOD_BODY);
PsiClass containingClass = method1.getContainingClass();
LOG.assertTrue(!containingClass.isInterface() || GrTraitUtil.isTrait(containingClass), "Interface bodies should be already set up");
CreateFromUsageUtils.setupMethodBody(method1, containingClass, fileTemplate);
}
if (hasNoReturnType) {
((GrMethod) method1).setReturnType(null);
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
CreateFromUsageUtils.setupEditor(method1, newEditor);
}
});
}
};
manager.startTemplate(newEditor, template, templateListener);
}
Aggregations