View | Details | Raw Unified | Return to bug 213509
Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java (-21 / +23 lines)
Lines 436-477 Link Here
436
	return originalMethod.declaringClass.retrieveAnnotations(originalMethod);
436
	return originalMethod.declaringClass.retrieveAnnotations(originalMethod);
437
}
437
}
438
/**
438
/**
439
 * @param index the index of the parameter of interest
439
 * @return the annotations for each of the method parameters or <code>null></code>
440
 * @return the annotations on the <code>index</code>th parameter
440
 * 	if there's no parameter.
441
 * @throws ArrayIndexOutOfBoundsException when <code>index</code> is not valid 
442
 */
441
 */
443
public AnnotationBinding[] getParameterAnnotations(int index) {
442
public AnnotationBinding[][] getParameterAnnotations() {
443
	int length = this.parameters.length;
444
	if (this.parameters == null || length == 0) {
445
		return null;
446
	}
444
	MethodBinding originalMethod = this.original();
447
	MethodBinding originalMethod = this.original();
445
	AnnotationHolder holder = originalMethod.declaringClass.retrieveAnnotationHolder(originalMethod, true);
448
	AnnotationHolder holder = originalMethod.declaringClass.retrieveAnnotationHolder(originalMethod, true);
446
	AnnotationBinding[][] allParameterAnnotations = holder == null ? null : holder.getParameterAnnotations();
449
	AnnotationBinding[][] allParameterAnnotations = holder == null ? null : holder.getParameterAnnotations();
447
	if (allParameterAnnotations == null && (this.tagBits & TagBits.HasParameterAnnotations) != 0) {
450
	if (allParameterAnnotations == null && (this.tagBits & TagBits.HasParameterAnnotations) != 0) {
451
		allParameterAnnotations = new AnnotationBinding[length][];
448
		// forward reference to method, where param annotations have not yet been associated to method
452
		// forward reference to method, where param annotations have not yet been associated to method
449
		if (this.declaringClass instanceof SourceTypeBinding) {
453
		if (this.declaringClass instanceof SourceTypeBinding) {
450
			SourceTypeBinding sourceType = (SourceTypeBinding) this.declaringClass;
454
			SourceTypeBinding sourceType = (SourceTypeBinding) this.declaringClass;
451
			if (sourceType.scope != null) {
455
			if (sourceType.scope != null) {
452
				AbstractMethodDeclaration methodDecl = sourceType.scope.referenceType().declarationOf(this);
456
				AbstractMethodDeclaration methodDecl = sourceType.scope.referenceType().declarationOf(this);
453
				if (methodDecl.arguments != null) {
457
				for (int i = 0; i < length; i++) {
454
					for (int i = 0, length = methodDecl.arguments.length; i < length; i++) {
458
					Argument argument = methodDecl.arguments[i];
455
						Argument argument = methodDecl.arguments[i];
459
					if (argument.annotations != null) {
456
						if (argument.annotations != null) {
460
						ASTNode.resolveAnnotations(methodDecl.scope, argument.annotations, argument.binding);
457
							ASTNode.resolveAnnotations(methodDecl.scope, argument.annotations, argument.binding);
461
						allParameterAnnotations[i] = argument.binding.getAnnotations();
458
							if (allParameterAnnotations == null) {
459
								allParameterAnnotations = new AnnotationBinding[length][];
460
							}
461
							allParameterAnnotations[i] = argument.binding.getAnnotations();
462
						}
463
					}
462
					}
464
					if (allParameterAnnotations != null)
465
						this.setParameterAnnotations(allParameterAnnotations);
466
				}
463
				}
464
				this.setParameterAnnotations(allParameterAnnotations);
465
			} else {
466
				for (int i = 0; i < length; i++) {
467
					allParameterAnnotations[i] = Binding.NO_ANNOTATIONS;
468
				}
469
			}
470
		} else {
471
			for (int i = 0; i < length; i++) {
472
				allParameterAnnotations[i] = Binding.NO_ANNOTATIONS;
467
			}
473
			}
468
		}
474
		}
469
	}
475
	}
470
	AnnotationBinding[] resultParameterAnnotations = allParameterAnnotations == null ? null : allParameterAnnotations[	index];
476
	return allParameterAnnotations;
471
	 if (resultParameterAnnotations != null) {
472
		 return resultParameterAnnotations;
473
	 }
474
	return Binding.NO_ANNOTATIONS;
475
}
477
}
476
public final int getAccessFlags() {
478
public final int getAccessFlags() {
477
	return modifiers & ExtraCompilerModifiers.AccJustFlag;
479
	return modifiers & ExtraCompilerModifiers.AccJustFlag;
(-)dom/org/eclipse/jdt/core/dom/MethodBinding.java (-13 / +20 lines)
Lines 52-58 Link Here
52
	private ITypeBinding[] typeParameters;
52
	private ITypeBinding[] typeParameters;
53
	private ITypeBinding[] typeArguments;
53
	private ITypeBinding[] typeArguments;
54
	private IAnnotationBinding[] annotations;
54
	private IAnnotationBinding[] annotations;
55
	private IAnnotationBinding[] parameterAnnotations;
55
	private IAnnotationBinding[][] parameterAnnotations;
56
56
57
	MethodBinding(BindingResolver resolver, org.eclipse.jdt.internal.compiler.lookup.MethodBinding binding) {
57
	MethodBinding(BindingResolver resolver, org.eclipse.jdt.internal.compiler.lookup.MethodBinding binding) {
58
		this.resolver = resolver;
58
		this.resolver = resolver;
Lines 134-156 Link Here
134
	}
134
	}
135
135
136
	public IAnnotationBinding[] getParameterAnnotations(int index) {
136
	public IAnnotationBinding[] getParameterAnnotations(int index) {
137
		if (this.parameterAnnotations != null) {
137
		if (getParameterTypes() == NO_TYPE_BINDINGS) {
138
			return this.parameterAnnotations;
138
			return AnnotationBinding.NoAnnotations;
139
		}
139
		}
140
		org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding[] annots = this.binding.getParameterAnnotations(index);
140
		if (this.parameterAnnotations != null) {
141
		int length = annots == null ? 0 : annots.length;
141
			return this.parameterAnnotations[index];
142
		if (length == 0) {
143
			return this.parameterAnnotations = AnnotationBinding.NoAnnotations;
144
		}
142
		}
145
		IAnnotationBinding[] domInstances =new AnnotationBinding[length];
143
		org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding[][] bindingAnnotations = this.binding.getParameterAnnotations();
144
		// bindingAnnoatations is never null as the method has one or several parameters
145
		int length = bindingAnnotations.length;
146
		this.parameterAnnotations = new AnnotationBinding[length][];
146
		for (int i = 0; i < length; i++) {
147
		for (int i = 0; i < length; i++) {
147
			final IAnnotationBinding annotationInstance = this.resolver.getAnnotationInstance(annots[i]);
148
			org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding[] paramBindingAnnotations = bindingAnnotations[i];
148
			if (annotationInstance == null) {
149
			int pLength = paramBindingAnnotations.length;
149
				return this.parameterAnnotations = AnnotationBinding.NoAnnotations;
150
			this.parameterAnnotations[i] = new AnnotationBinding[pLength];
151
			for (int j=0; j<pLength; j++) {
152
				IAnnotationBinding domAnnotation = this.resolver.getAnnotationInstance(paramBindingAnnotations[j]);
153
				if (domAnnotation == null) {
154
					this.parameterAnnotations[i] = AnnotationBinding.NoAnnotations;
155
					break;
156
				}
157
				this.parameterAnnotations[i][j] = domAnnotation;
150
			}
158
			}
151
			domInstances[i] = annotationInstance;
152
		}
159
		}
153
		return this.parameterAnnotations = domInstances;
160
		return this.parameterAnnotations[index];
154
	}
161
	}
155
162
156
	/**
163
	/**
(-)src/org/eclipse/jdt/core/tests/dom/ASTConverterBugsTest.java (+83 lines)
Lines 39-44 Link Here
39
	return buildModelTestSuite(ASTConverterBugsTest.class);
39
	return buildModelTestSuite(ASTConverterBugsTest.class);
40
}
40
}
41
41
42
protected void checkParameterAnnotations(String message, String expected, IMethodBinding methodBinding) {
43
	int size = methodBinding.getParameterTypes().length;
44
	StringBuffer buffer = new StringBuffer();
45
	for (int i=0; i<size; i++) {
46
		buffer.append("----- param ");
47
		buffer.append(i+1);
48
		buffer.append("-----\n");
49
		IAnnotationBinding[] bindings= methodBinding.getParameterAnnotations(i);
50
		int length = bindings.length;
51
		for (int j=0; j<length; j++) {
52
			buffer.append(bindings[j].getKey());
53
			buffer.append('\n');
54
		}
55
	}
56
	assertEquals(message, expected, buffer.toString());
57
}
58
42
public ASTNode runConversion(ICompilationUnit unit, boolean resolveBindings) {
59
public ASTNode runConversion(ICompilationUnit unit, boolean resolveBindings) {
43
	return runConversion(this.testLevel, unit, resolveBindings);
60
	return runConversion(this.testLevel, unit, resolveBindings);
44
}
61
}
Lines 363-366 Link Here
363
	);
380
	);
364
}
381
}
365
382
383
/**
384
 * @bug 213509: [dom] IMethodBinding.getParameterAnnotations returns annotations for wrong parameter
385
 * @test Ensures that all parameter annotations of a the method binding are correctly  returned
386
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=213509"
387
 */
388
public void testBug213509() throws CoreException, IOException {
389
	workingCopies = new ICompilationUnit[1];
390
	workingCopies[0] = getWorkingCopy("/Converter15/src/Test.java",
391
		"public class Test {\n" + 
392
		"	void m(@Foo @Bar @Annot String str, @Bar @Foo Object obj, @Annot int x) {}\n" + 
393
		"}\n" + 
394
		"@interface Foo {}\n" + 
395
		"@interface Bar {}\n" + 
396
		"@interface Annot {}\n"
397
	);
398
399
	CompilationUnit unit = (CompilationUnit) runConversion(workingCopies[0], true, false, true);
400
	MethodDeclaration methodDeclaration = (MethodDeclaration) getASTNode(unit, 0, 0);
401
	checkParameterAnnotations(methodDeclaration+" has invalid parameter annotations!",
402
		"----- param 1-----\n" + 
403
		"@LTest~Foo;\n" + 
404
		"@LTest~Bar;\n" + 
405
		"@LTest~Annot;\n" + 
406
		"----- param 2-----\n" + 
407
		"@LTest~Bar;\n" + 
408
		"@LTest~Foo;\n" + 
409
		"----- param 3-----\n" + 
410
		"@LTest~Annot;\n",
411
		methodDeclaration.resolveBinding()
412
	);
413
}
414
public void testBug213509_invocation() throws CoreException, IOException {
415
	workingCopies = new ICompilationUnit[2];
416
	workingCopies[0] = getWorkingCopy("/Converter15/src/Test.java",
417
		"public class Test {\n" + 
418
		"	void m(@Foo @Bar @Annot String str, @Bar @Foo Object obj, @Annot int x) {}\n" + 
419
		"}\n" + 
420
		"@interface Foo {}\n" + 
421
		"@interface Bar {}\n" + 
422
		"@interface Annot {}\n"
423
	);
424
	workingCopies[1] = getWorkingCopy("/Converter15/src/X.java",
425
		"public class X {\n" + 
426
		"public X(Test test) {\n" + 
427
		"	test.m(\"\", null, 7);\n" + 
428
		"}\n" + 
429
		"}"
430
	);
431
432
	CompilationUnit unit = (CompilationUnit) runConversion(workingCopies[1], true, false, true);
433
	MethodDeclaration methodDeclaration = (MethodDeclaration) getASTNode(unit, 0, 0);
434
	MethodInvocation methodInvocation = (MethodInvocation) ((ExpressionStatement) methodDeclaration.getBody().statements().get(0)).getExpression();
435
	checkParameterAnnotations(methodInvocation+" has invalid parameter annotations!",
436
		"----- param 1-----\n" + 
437
		"@LTest~Foo;\n" + 
438
		"@LTest~Bar;\n" + 
439
		"@LTest~Annot;\n" + 
440
		"----- param 2-----\n" + 
441
		"@LTest~Bar;\n" + 
442
		"@LTest~Foo;\n" + 
443
		"----- param 3-----\n" + 
444
		"@LTest~Annot;\n",
445
		methodInvocation.resolveMethodBinding()
446
	);
447
}
448
366
}
449
}

Return to bug 213509