View | Details | Raw Unified | Return to bug 163443 | Differences between
and this patch

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java (-3 lines)
Lines 6344-6352 Link Here
6344
}
6344
}
6345
public void unusedPrivateConstructor(ConstructorDeclaration constructorDecl) {
6345
public void unusedPrivateConstructor(ConstructorDeclaration constructorDecl) {
6346
	
6346
	
6347
	// no complaint for no-arg constructors (or default ones) - known pattern to block instantiation
6348
	if (constructorDecl.arguments == null || constructorDecl.arguments.length == 0) return;
6349
6350
	int severity = computeSeverity(IProblem.UnusedPrivateConstructor);
6347
	int severity = computeSeverity(IProblem.UnusedPrivateConstructor);
6351
	if (severity == ProblemSeverities.Ignore) return;
6348
	if (severity == ProblemSeverities.Ignore) return;
6352
					
6349
					
(-)compiler/org/eclipse/jdt/internal/compiler/ast/ConstructorDeclaration.java (-18 / +30 lines)
Lines 51-60 Link Here
51
	int nonStaticFieldInfoReachMode = flowInfo.reachMode();
51
	int nonStaticFieldInfoReachMode = flowInfo.reachMode();
52
	flowInfo.setReachMode(initialReachMode);
52
	flowInfo.setReachMode(initialReachMode);
53
	
53
	
54
	if (this.binding != null && !this.binding.isUsed() && (this.binding.isPrivate() || (this.binding.declaringClass.tagBits & (TagBits.IsAnonymousType|TagBits.IsLocalType)) == TagBits.IsLocalType)) {
54
	checkUnused: {
55
		if (!classScope.referenceCompilationUnit().compilationResult.hasSyntaxError) {
55
		MethodBinding constructorBinding;
56
			this.scope.problemReporter().unusedPrivateConstructor(this);
56
		if ((constructorBinding = this.binding) == null) break checkUnused;
57
		if (this.isDefaultConstructor) break checkUnused;
58
		if (constructorBinding.isUsed()) break checkUnused;
59
		if (constructorBinding.isPrivate()) {
60
			if ((this.binding.declaringClass.tagBits & TagBits.HasNonPrivateConstructor) == 0)
61
				break checkUnused; // tolerate as known pattern to block instantiation
62
		} else if ((this.binding.declaringClass.tagBits & (TagBits.IsAnonymousType|TagBits.IsLocalType)) != TagBits.IsLocalType) {
63
			break checkUnused;
57
		}
64
		}
65
		// complain unused
66
		this.scope.problemReporter().unusedPrivateConstructor(this);
58
	}
67
	}
59
		
68
		
60
	// check constructor recursion, once all constructor got resolved
69
	// check constructor recursion, once all constructor got resolved
Lines 412-439 Link Here
412
 * for recursive constructor invocations.
421
 * for recursive constructor invocations.
413
 */
422
 */
414
public void resolveStatements() {
423
public void resolveStatements() {
415
	if (!CharOperation.equals(this.scope.enclosingSourceType().sourceName, this.selector)){
424
	SourceTypeBinding sourceType = this.scope.enclosingSourceType();
425
	if (!CharOperation.equals(sourceType.sourceName, this.selector)){
416
		this.scope.problemReporter().missingReturnType(this);
426
		this.scope.problemReporter().missingReturnType(this);
417
	}
427
	}
418
419
	if (this.typeParameters != null) {
428
	if (this.typeParameters != null) {
420
		for (int i = 0, length = this.typeParameters.length; i < length; i++) {
429
		for (int i = 0, length = this.typeParameters.length; i < length; i++) {
421
			this.typeParameters[i].resolve(this.scope);
430
			this.typeParameters[i].resolve(this.scope);
422
		}
431
		}
423
	}
432
	}
424
	
433
	if (this.binding != null) {
425
	// if null ==> an error has occurs at parsing time ....
434
		if (!this.binding.isPrivate()) {
426
	if (this.constructorCall != null) {
435
			sourceType.tagBits |= TagBits.HasNonPrivateConstructor;
427
		// e.g. using super() in java.lang.Object
436
		}
428
		if (this.binding != null
437
		// if null ==> an error has occurs at parsing time ....
429
			&& this.binding.declaringClass.id == TypeIds.T_JavaLangObject
438
		if (this.constructorCall != null) {
430
			&& this.constructorCall.accessMode != ExplicitConstructorCall.This) {
439
			// e.g. using super() in java.lang.Object
431
				if (this.constructorCall.accessMode == ExplicitConstructorCall.Super) {
440
			if (sourceType.id == TypeIds.T_JavaLangObject
432
					this.scope.problemReporter().cannotUseSuperInJavaLangObject(this.constructorCall);
441
				&& this.constructorCall.accessMode != ExplicitConstructorCall.This) {
433
				}
442
					if (this.constructorCall.accessMode == ExplicitConstructorCall.Super) {
434
				this.constructorCall = null;
443
						this.scope.problemReporter().cannotUseSuperInJavaLangObject(this.constructorCall);
435
		} else {
444
					}
436
			this.constructorCall.resolve(this.scope);
445
					this.constructorCall = null;
446
			} else {
447
				this.constructorCall.resolve(this.scope);
448
			}
437
		}
449
		}
438
	}
450
	}
439
	if ((this.modifiers & ExtraCompilerModifiers.AccSemicolonBody) != 0) {
451
	if ((this.modifiers & ExtraCompilerModifiers.AccSemicolonBody) != 0) {
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/TagBits.java (+3 lines)
Lines 106-109 Link Here
106
	long AllStandardAnnotationsMask = AnnotationTargetMASK | AnnotationRetentionMASK | AnnotationDeprecated | AnnotationDocumented | AnnotationInherited |  AnnotationOverride | AnnotationSuppressWarnings;
106
	long AllStandardAnnotationsMask = AnnotationTargetMASK | AnnotationRetentionMASK | AnnotationDeprecated | AnnotationDocumented | AnnotationInherited |  AnnotationOverride | AnnotationSuppressWarnings;
107
	
107
	
108
	long DefaultValueResolved = ASTNode.Bit52L;
108
	long DefaultValueResolved = ASTNode.Bit52L;
109
	
110
	// set when type contains non-private constructor(s)
111
	long HasNonPrivateConstructor = ASTNode.Bit53L;
109
}
112
}
(-)src/org/eclipse/jdt/core/tests/compiler/regression/ProblemConstructorTest.java (+35 lines)
Lines 98-101 Link Here
98
		"This method requires a body instead of a semicolon\n" + 
98
		"This method requires a body instead of a semicolon\n" + 
99
		"----------\n");
99
		"----------\n");
100
}
100
}
101
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=163443
102
public void test003() {
103
	this.runNegativeTest(
104
		new String[] {
105
			"Example.java",
106
			"class Example {\n" + 
107
			"  private Example() {\n" + 
108
			"  }\n" + 
109
			"  public Example(int i) {\n" + 
110
			"  }\n" + 
111
			"}\n" + 
112
			"class E1 {\n" + 
113
			"    private E1(int i) {}\n" + 
114
			"    private E1(long l) {}\n" + 
115
			"}\n" + 
116
			"class E2 {\n" + 
117
			"    private E2(int i) {}\n" + 
118
			"}\n" + 
119
			"class E3 {\n" + 
120
			"    public E3(int i) {}\n" + 
121
			"    Zork z;\n" + 
122
			"}\n"
123
		},
124
		"----------\n" + 
125
		"1. WARNING in Example.java (at line 2)\n" + 
126
		"	private Example() {\n" + 
127
		"	        ^^^^^^^^^\n" + 
128
		"The constructor Example() is never used locally\n" + 
129
		"----------\n" + 
130
		"2. ERROR in Example.java (at line 16)\n" + 
131
		"	Zork z;\n" + 
132
		"	^^^^\n" + 
133
		"Zork cannot be resolved to a type\n" + 
134
		"----------\n");
135
}
101
}
136
}

Return to bug 163443