]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/Invocations.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / code / Invocations.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *     Dmitry Stalnov (dstalnov@fusionone.com) - contributed fixes for:
11  *       o Allow 'this' constructor to be inlined
12  *         (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=38093)
13  *******************************************************************************/
14 package org.eclipse.jdt.internal.corext.refactoring.code;
15
16 import java.util.List;
17
18 import org.eclipse.jdt.core.dom.ASTNode;
19 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
20 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
21 import org.eclipse.jdt.core.dom.ConstructorInvocation;
22 import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
23 import org.eclipse.jdt.core.dom.Expression;
24 import org.eclipse.jdt.core.dom.IMethodBinding;
25 import org.eclipse.jdt.core.dom.ITypeBinding;
26 import org.eclipse.jdt.core.dom.MethodInvocation;
27 import org.eclipse.jdt.core.dom.ParameterizedType;
28 import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
29 import org.eclipse.jdt.core.dom.SuperMethodInvocation;
30 import org.eclipse.jdt.core.dom.Type;
31 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
32 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
33
34 public class Invocations {
35
36         public static List<Expression> getArguments(ASTNode invocation) {
37                 switch (invocation.getNodeType()) {
38                         case ASTNode.METHOD_INVOCATION:
39                                 return ((MethodInvocation)invocation).arguments();
40                         case ASTNode.SUPER_METHOD_INVOCATION:
41                                 return ((SuperMethodInvocation)invocation).arguments();
42                                 
43                         case ASTNode.CONSTRUCTOR_INVOCATION:
44                                 return ((ConstructorInvocation)invocation).arguments();
45                         case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
46                                 return ((SuperConstructorInvocation)invocation).arguments();
47                                 
48                         case ASTNode.CLASS_INSTANCE_CREATION:
49                                 return ((ClassInstanceCreation)invocation).arguments();
50                         case ASTNode.ENUM_CONSTANT_DECLARATION:
51                                 return ((EnumConstantDeclaration)invocation).arguments();
52                                 
53                         default:
54                                 throw new IllegalArgumentException(invocation.toString());
55                 }
56         }
57
58         public static ChildListPropertyDescriptor getArgumentsProperty(ASTNode invocation) {
59                 switch (invocation.getNodeType()) {
60                         case ASTNode.METHOD_INVOCATION:
61                                 return MethodInvocation.ARGUMENTS_PROPERTY;
62                         case ASTNode.SUPER_METHOD_INVOCATION:
63                                 return SuperMethodInvocation.ARGUMENTS_PROPERTY;
64                                 
65                         case ASTNode.CONSTRUCTOR_INVOCATION:
66                                 return ConstructorInvocation.ARGUMENTS_PROPERTY;
67                         case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
68                                 return SuperConstructorInvocation.ARGUMENTS_PROPERTY;
69                                 
70                         case ASTNode.CLASS_INSTANCE_CREATION:
71                                 return ClassInstanceCreation.ARGUMENTS_PROPERTY;
72                         case ASTNode.ENUM_CONSTANT_DECLARATION:
73                                 return EnumConstantDeclaration.ARGUMENTS_PROPERTY;
74                                 
75                         default:
76                                 throw new IllegalArgumentException(invocation.toString());
77                 }
78         }
79         
80         public static Expression getExpression(ASTNode invocation) {
81                 switch (invocation.getNodeType()) {
82                         case ASTNode.METHOD_INVOCATION:
83                                 return ((MethodInvocation)invocation).getExpression();
84                         case ASTNode.SUPER_METHOD_INVOCATION:
85                                 return null;
86                                 
87                         case ASTNode.CONSTRUCTOR_INVOCATION:
88                                 return null;
89                         case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
90                                 return ((SuperConstructorInvocation)invocation).getExpression();
91                                 
92                         case ASTNode.CLASS_INSTANCE_CREATION:
93                                 return ((ClassInstanceCreation)invocation).getExpression();
94                         case ASTNode.ENUM_CONSTANT_DECLARATION:
95                                 return null;
96                                 
97                         default:
98                                 throw new IllegalArgumentException(invocation.toString());
99                 }
100         }
101
102         public static boolean isInvocation(ASTNode node) {
103                 int type= node.getNodeType();
104                 return type == ASTNode.METHOD_INVOCATION || type == ASTNode.SUPER_METHOD_INVOCATION ||
105                         type == ASTNode.CONSTRUCTOR_INVOCATION;
106         }
107         
108         public static boolean isInvocationWithArguments(ASTNode node) {
109                 switch (node.getNodeType()) {
110                         case ASTNode.METHOD_INVOCATION:
111                         case ASTNode.SUPER_METHOD_INVOCATION:
112                                 
113                         case ASTNode.CONSTRUCTOR_INVOCATION:
114                         case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
115                                 
116                         case ASTNode.CLASS_INSTANCE_CREATION:
117                         case ASTNode.ENUM_CONSTANT_DECLARATION:
118                                 return true;
119                                 
120                         default:
121                                 return false;
122                 }
123         }
124
125         public static IMethodBinding resolveBinding(ASTNode invocation) {
126                 switch (invocation.getNodeType()) {
127                         case ASTNode.METHOD_INVOCATION:
128                                 return ((MethodInvocation)invocation).resolveMethodBinding();
129                         case ASTNode.SUPER_METHOD_INVOCATION:
130                                 return ((SuperMethodInvocation)invocation).resolveMethodBinding();
131                                 
132                         case ASTNode.CONSTRUCTOR_INVOCATION:
133                                 return ((ConstructorInvocation)invocation).resolveConstructorBinding();
134                         case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
135                                 return ((SuperConstructorInvocation)invocation).resolveConstructorBinding();
136                                 
137                         case ASTNode.CLASS_INSTANCE_CREATION:
138                                 return ((ClassInstanceCreation)invocation).resolveConstructorBinding();
139                         case ASTNode.ENUM_CONSTANT_DECLARATION:
140                                 return ((EnumConstantDeclaration)invocation).resolveConstructorBinding();
141                                 
142                         default:
143                                 throw new IllegalArgumentException(invocation.toString());
144                 }
145         }
146
147         public static boolean isResolvedTypeInferredFromExpectedType(Expression invocation) {
148                 if (invocation == null)
149                         return false;
150                 
151                 switch (invocation.getNodeType()) {
152                         case ASTNode.METHOD_INVOCATION:
153                                 return ((MethodInvocation) invocation).isResolvedTypeInferredFromExpectedType();
154                         case ASTNode.SUPER_METHOD_INVOCATION:
155                                 return ((SuperMethodInvocation) invocation).isResolvedTypeInferredFromExpectedType();
156                         case ASTNode.CLASS_INSTANCE_CREATION:
157                                 return ((ClassInstanceCreation) invocation).isResolvedTypeInferredFromExpectedType();
158                                 
159                         default:
160                                 return false;
161                 }
162         }
163         
164         public static ListRewrite getInferredTypeArgumentsRewrite(ASTRewrite rewrite, Expression invocation) {
165                 switch (invocation.getNodeType()) {
166                         case ASTNode.METHOD_INVOCATION:
167                                 return rewrite.getListRewrite(invocation, MethodInvocation.TYPE_ARGUMENTS_PROPERTY);
168                         case ASTNode.SUPER_METHOD_INVOCATION:
169                                 return rewrite.getListRewrite(invocation, SuperMethodInvocation.TYPE_ARGUMENTS_PROPERTY);
170                         case ASTNode.CLASS_INSTANCE_CREATION:
171                                 Type type= ((ClassInstanceCreation) invocation).getType();
172                                 return rewrite.getListRewrite(type, ParameterizedType.TYPE_ARGUMENTS_PROPERTY);
173                                 
174                         default:
175                                 throw new IllegalArgumentException(invocation.toString());
176                 }
177         }
178
179         public static ITypeBinding[] getInferredTypeArguments(Expression invocation) {
180                 IMethodBinding methodBinding;
181                 switch (invocation.getNodeType()) {
182                         case ASTNode.METHOD_INVOCATION:
183                                 methodBinding= ((MethodInvocation) invocation).resolveMethodBinding();
184                                 return methodBinding == null ? null : methodBinding.getTypeArguments();
185                         case ASTNode.SUPER_METHOD_INVOCATION:
186                                 methodBinding= ((SuperMethodInvocation) invocation).resolveMethodBinding();
187                                 return methodBinding == null ? null : methodBinding.getTypeArguments();
188                         case ASTNode.CLASS_INSTANCE_CREATION:
189                                 Type type= ((ClassInstanceCreation) invocation).getType();
190                                 ITypeBinding typeBinding= type.resolveBinding();
191                                 return typeBinding == null ? null : typeBinding.getTypeArguments();
192                                 
193                         default:
194                                 throw new IllegalArgumentException(invocation.toString());
195                 }
196         }
197 }