2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements;
18 import com.intellij.psi.PsiElement;
19 import com.intellij.psi.PsiManager;
20 import com.intellij.psi.PsiMethod;
21 import com.intellij.psi.PsiModifier;
22 import com.intellij.util.IncorrectOperationException;
23 import org.jetbrains.annotations.NotNull;
24 import org.jetbrains.plugins.groovy.annotator.intentions.QuickfixUtil;
25 import org.jetbrains.plugins.groovy.annotator.intentions.dynamic.DynamicManager;
26 import org.jetbrains.plugins.groovy.annotator.intentions.dynamic.MyPair;
27 import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
28 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
29 import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrDynamicImplicitMethod;
31 import java.util.ArrayList;
32 import java.util.List;
35 * User: Dmitry.Krasilschikov
38 public class DMethodElement extends DItemElement {
39 public List<MyPair> myPairs = new ArrayList<MyPair>();
40 private PsiMethod myImplicitMethod;
42 @SuppressWarnings("UnusedDeclaration") //for serialization
43 public DMethodElement() {
44 super(null, null, null);
47 public DMethodElement(Boolean isStatic, String name, String returnType, List<MyPair> pairs) {
48 super(isStatic, name, returnType);
53 public List<MyPair> getPairs() {
57 public void clearCache() {
58 myImplicitMethod = null;
62 public PsiMethod getPsi(PsiManager manager, final String containingClassName) {
63 if (myImplicitMethod != null) return myImplicitMethod;
65 final String type = getType();
67 String staticModifier = null;
68 Boolean isStatic = isStatic();
70 if (isStatic != null && isStatic.booleanValue()) {
71 staticModifier = PsiModifier.STATIC;
74 final String[] argumentsTypes = QuickfixUtil.getArgumentsTypes(myPairs);
75 final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(manager.getProject());
76 final GrMethod method = factory.createMethodFromText(staticModifier, getName(), type, argumentsTypes, null);
78 myImplicitMethod = new GrDynamicImplicitMethod(manager, method, containingClassName) {
80 public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
81 DynamicManager.getInstance(getProject()).replaceDynamicMethodName(containingClassName, getName(), name, argumentsTypes);
82 return super.setName(name);
85 return myImplicitMethod;