1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.codegen.*;
15 import org.eclipse.jdt.internal.compiler.flow.*;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
18 public class AllocationExpression
20 implements InvocationSite {
22 public TypeReference type;
23 public Expression[] arguments;
24 public MethodBinding binding;
26 MethodBinding syntheticAccessor;
28 public FlowInfo analyseCode(
29 BlockScope currentScope,
30 FlowContext flowContext,
33 // check captured variables are initialized in current context (26134)
34 checkCapturedLocalInitializationIfNecessary(this.binding.declaringClass, currentScope, flowInfo);
37 if (arguments != null) {
38 for (int i = 0, count = arguments.length; i < count; i++) {
41 .analyseCode(currentScope, flowContext, flowInfo)
42 .unconditionalInits();
45 // record some dependency information for exception types
46 ReferenceBinding[] thrownExceptions;
47 if (((thrownExceptions = this.binding.thrownExceptions).length) != 0) {
48 // check exception handling
49 flowContext.checkExceptionHandlers(
55 manageEnclosingInstanceAccessIfNecessary(currentScope, flowInfo);
56 manageSyntheticAccessIfNecessary(currentScope, flowInfo);
61 public void checkCapturedLocalInitializationIfNecessary(ReferenceBinding checkedType, BlockScope currentScope, FlowInfo flowInfo) {
63 if (checkedType.isLocalType()
64 && !checkedType.isAnonymousType()
65 && !currentScope.isDefinedInType(checkedType)) { // only check external allocations
66 NestedTypeBinding nestedType = (NestedTypeBinding) checkedType;
67 SyntheticArgumentBinding[] syntheticArguments = nestedType.syntheticOuterLocalVariables();
68 if (syntheticArguments != null)
69 for (int i = 0, count = syntheticArguments.length; i < count; i++){
70 SyntheticArgumentBinding syntheticArgument = syntheticArguments[i];
71 LocalVariableBinding targetLocal;
72 if ((targetLocal = syntheticArgument.actualOuterLocalVariable) == null) continue;
73 if (targetLocal.declaration != null && !flowInfo.isDefinitelyAssigned(targetLocal)){
74 currentScope.problemReporter().uninitializedLocalVariable(targetLocal, this);
81 public Expression enclosingInstance() {
85 public void generateCode(
86 BlockScope currentScope,
87 CodeStream codeStream,
88 boolean valueRequired) {
90 int pc = codeStream.position;
91 ReferenceBinding allocatedType = binding.declaringClass;
93 codeStream.new_(allocatedType);
97 // better highlight for allocation: display the type individually
98 codeStream.recordPositionsFrom(pc, type.sourceStart);
100 // handling innerclass instance allocation - enclosing instance arguments
101 if (allocatedType.isNestedType()) {
102 codeStream.generateSyntheticEnclosingInstanceValues(
108 // generate the arguments for constructor
109 if (arguments != null) {
110 for (int i = 0, count = arguments.length; i < count; i++) {
111 arguments[i].generateCode(currentScope, codeStream, true);
114 // handling innerclass instance allocation - outer local arguments
115 if (allocatedType.isNestedType()) {
116 codeStream.generateSyntheticOuterArgumentValues(
121 // invoke constructor
122 if (syntheticAccessor == null) {
123 codeStream.invokespecial(binding);
125 // synthetic accessor got some extra arguments appended to its signature, which need values
127 max = syntheticAccessor.parameters.length - binding.parameters.length;
130 codeStream.aconst_null();
132 codeStream.invokespecial(syntheticAccessor);
134 codeStream.recordPositionsFrom(pc, this.sourceStart);
137 public boolean isSuperAccess() {
142 public boolean isTypeAccess() {
147 /* Inner emulation consists in either recording a dependency
148 * link only, or performing one level of propagation.
150 * Dependency mechanism is used whenever dealing with source target
151 * types, since by the time we reach them, we might not yet know their
154 public void manageEnclosingInstanceAccessIfNecessary(BlockScope currentScope, FlowInfo flowInfo) {
156 if (!flowInfo.isReachable()) return;
157 ReferenceBinding allocatedType;
159 // perform some emulation work in case there is some and we are inside a local type only
160 if ((allocatedType = binding.declaringClass).isNestedType()
161 && currentScope.enclosingSourceType().isLocalType()) {
163 if (allocatedType.isLocalType()) {
164 ((LocalTypeBinding) allocatedType).addInnerEmulationDependent(currentScope, false);
165 // request cascade of accesses
167 // locally propagate, since we already now the desired shape for sure
168 currentScope.propagateInnerEmulation(allocatedType, false);
169 // request cascade of accesses
174 public void manageSyntheticAccessIfNecessary(BlockScope currentScope, FlowInfo flowInfo) {
176 if (!flowInfo.isReachable()) return;
177 if (binding.isPrivate()
178 && (currentScope.enclosingSourceType() != binding.declaringClass)) {
183 .isPrivateConstructorAccessChangingVisibility) {
184 binding.tagForClearingPrivateModifier();
185 // constructor will not be dumped as private, no emulation required thus
188 ((SourceTypeBinding) binding.declaringClass).addSyntheticMethod(binding, isSuperAccess());
189 currentScope.problemReporter().needToEmulateMethodAccess(binding, this);
194 public StringBuffer printExpression(int indent, StringBuffer output) {
196 output.append("new "); //$NON-NLS-1$
197 type.printExpression(0, output);
199 if (arguments != null) {
200 for (int i = 0; i < arguments.length; i++) {
201 if (i > 0) output.append(", "); //$NON-NLS-1$
202 arguments[i].printExpression(0, output);
205 return output.append(')');
208 public TypeBinding resolveType(BlockScope scope) {
210 // Propagate the type checking to the arguments, and check if the constructor is defined.
211 constant = NotAConstant;
212 this.resolvedType = type.resolveType(scope);
213 // will check for null after args are resolved
215 // buffering the arguments' types
216 boolean argsContainCast = false;
217 TypeBinding[] argumentTypes = NoParameters;
218 if (arguments != null) {
219 boolean argHasError = false;
220 int length = arguments.length;
221 argumentTypes = new TypeBinding[length];
222 for (int i = 0; i < length; i++) {
223 Expression argument = this.arguments[i];
224 if (argument instanceof CastExpression) {
225 argument.bits |= IgnoreNeedForCastCheckMASK; // will check later on
226 argsContainCast = true;
228 if ((argumentTypes[i] = argument.resolveType(scope)) == null) {
233 return this.resolvedType;
236 if (this.resolvedType == null)
239 if (!this.resolvedType.canBeInstantiated()) {
240 scope.problemReporter().cannotInstantiate(type, this.resolvedType);
241 return this.resolvedType;
243 ReferenceBinding allocationType = (ReferenceBinding) this.resolvedType;
244 if (!(binding = scope.getConstructor(allocationType, argumentTypes, this))
246 if (binding.declaringClass == null)
247 binding.declaringClass = allocationType;
248 scope.problemReporter().invalidConstructor(this, binding);
249 return this.resolvedType;
251 if (isMethodUseDeprecated(binding, scope))
252 scope.problemReporter().deprecatedMethod(binding, this);
254 if (arguments != null) {
255 for (int i = 0; i < arguments.length; i++) {
256 arguments[i].implicitWidening(binding.parameters[i], argumentTypes[i]);
258 if (argsContainCast) {
259 CastExpression.checkNeedForArgumentCasts(scope, null, allocationType, binding, this.arguments, argumentTypes, this);
262 return allocationType;
265 public void setActualReceiverType(ReferenceBinding receiverType) {
269 public void setDepth(int i) {
273 public void setFieldIndex(int i) {
277 public void traverse(ASTVisitor visitor, BlockScope scope) {
279 if (visitor.visit(this, scope)) {
281 type.traverse(visitor, scope);
282 if (arguments != null) {
283 argumentsLength = arguments.length;
284 for (int i = 0; i < argumentsLength; i++)
285 arguments[i].traverse(visitor, scope);
288 visitor.endVisit(this, scope);