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
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;
16 import java.util.List;
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;
34 public class Invocations {
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();
43 case ASTNode.CONSTRUCTOR_INVOCATION:
44 return ((ConstructorInvocation)invocation).arguments();
45 case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
46 return ((SuperConstructorInvocation)invocation).arguments();
48 case ASTNode.CLASS_INSTANCE_CREATION:
49 return ((ClassInstanceCreation)invocation).arguments();
50 case ASTNode.ENUM_CONSTANT_DECLARATION:
51 return ((EnumConstantDeclaration)invocation).arguments();
54 throw new IllegalArgumentException(invocation.toString());
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;
65 case ASTNode.CONSTRUCTOR_INVOCATION:
66 return ConstructorInvocation.ARGUMENTS_PROPERTY;
67 case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
68 return SuperConstructorInvocation.ARGUMENTS_PROPERTY;
70 case ASTNode.CLASS_INSTANCE_CREATION:
71 return ClassInstanceCreation.ARGUMENTS_PROPERTY;
72 case ASTNode.ENUM_CONSTANT_DECLARATION:
73 return EnumConstantDeclaration.ARGUMENTS_PROPERTY;
76 throw new IllegalArgumentException(invocation.toString());
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:
87 case ASTNode.CONSTRUCTOR_INVOCATION:
89 case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
90 return ((SuperConstructorInvocation)invocation).getExpression();
92 case ASTNode.CLASS_INSTANCE_CREATION:
93 return ((ClassInstanceCreation)invocation).getExpression();
94 case ASTNode.ENUM_CONSTANT_DECLARATION:
98 throw new IllegalArgumentException(invocation.toString());
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;
108 public static boolean isInvocationWithArguments(ASTNode node) {
109 switch (node.getNodeType()) {
110 case ASTNode.METHOD_INVOCATION:
111 case ASTNode.SUPER_METHOD_INVOCATION:
113 case ASTNode.CONSTRUCTOR_INVOCATION:
114 case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
116 case ASTNode.CLASS_INSTANCE_CREATION:
117 case ASTNode.ENUM_CONSTANT_DECLARATION:
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();
132 case ASTNode.CONSTRUCTOR_INVOCATION:
133 return ((ConstructorInvocation)invocation).resolveConstructorBinding();
134 case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
135 return ((SuperConstructorInvocation)invocation).resolveConstructorBinding();
137 case ASTNode.CLASS_INSTANCE_CREATION:
138 return ((ClassInstanceCreation)invocation).resolveConstructorBinding();
139 case ASTNode.ENUM_CONSTANT_DECLARATION:
140 return ((EnumConstantDeclaration)invocation).resolveConstructorBinding();
143 throw new IllegalArgumentException(invocation.toString());
147 public static boolean isResolvedTypeInferredFromExpectedType(Expression invocation) {
148 if (invocation == null)
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();
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);
175 throw new IllegalArgumentException(invocation.toString());
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();
194 throw new IllegalArgumentException(invocation.toString());