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

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/JavadocMessageSend.java (-1 / +1 lines)
Lines 165-171 Link Here
165
				}
165
				}
166
			}
166
			}
167
		}
167
		}
168
		if (isMethodUseDeprecated(this.binding, scope)) {
168
		if (isMethodUseDeprecated(this.binding, scope, true)) {
169
			scope.problemReporter().javadocDeprecatedMethod(this.binding, this, scope.getDeclarationModifiers());
169
			scope.problemReporter().javadocDeprecatedMethod(this.binding, this, scope.getDeclarationModifiers());
170
		}
170
		}
171
171
(-)compiler/org/eclipse/jdt/internal/compiler/ast/ExplicitConstructorCall.java (-1 / +1 lines)
Lines 330-336 Link Here
330
				argumentTypes = new TypeBinding[] { scope.getJavaLangString(), BaseTypes.IntBinding };
330
				argumentTypes = new TypeBinding[] { scope.getJavaLangString(), BaseTypes.IntBinding };
331
			}
331
			}
332
			if ((binding = scope.getConstructor(receiverType, argumentTypes, this)).isValidBinding()) {
332
			if ((binding = scope.getConstructor(receiverType, argumentTypes, this)).isValidBinding()) {
333
				if (isMethodUseDeprecated(binding, scope))
333
				if (isMethodUseDeprecated(this.binding, scope, this.accessMode != ImplicitSuper))
334
					scope.problemReporter().deprecatedMethod(binding, this);
334
					scope.problemReporter().deprecatedMethod(binding, this);
335
				checkInvocationArguments(scope, null, receiverType, binding, this.arguments, argumentTypes, argsContainCast, this);
335
				checkInvocationArguments(scope, null, receiverType, binding, this.arguments, argumentTypes, argsContainCast, this);
336
				if (binding.isPrivate() || receiverType.isLocalType()) {
336
				if (binding.isPrivate() || receiverType.isLocalType()) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/QualifiedAllocationExpression.java (-1 / +1 lines)
Lines 303-309 Link Here
303
			}
303
			}
304
			ReferenceBinding allocationType = (ReferenceBinding) receiverType;
304
			ReferenceBinding allocationType = (ReferenceBinding) receiverType;
305
			if ((this.binding = scope.getConstructor(allocationType, argumentTypes, this)).isValidBinding()) {
305
			if ((this.binding = scope.getConstructor(allocationType, argumentTypes, this)).isValidBinding()) {
306
				if (isMethodUseDeprecated(binding, scope)) {
306
				if (isMethodUseDeprecated(binding, scope, true)) {
307
					scope.problemReporter().deprecatedMethod(this.binding, this);
307
					scope.problemReporter().deprecatedMethod(this.binding, this);
308
				}
308
				}
309
				checkInvocationArguments(scope, null, allocationType, binding, this.arguments, argumentTypes, argsContainCast, this);
309
				checkInvocationArguments(scope, null, allocationType, binding, this.arguments, argumentTypes, argsContainCast, this);
(-)compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java (-1 / +1 lines)
Lines 319-325 Link Here
319
			scope.problemReporter().invalidConstructor(this, binding);
319
			scope.problemReporter().invalidConstructor(this, binding);
320
			return this.resolvedType;
320
			return this.resolvedType;
321
		}
321
		}
322
		if (isMethodUseDeprecated(binding, scope))
322
		if (isMethodUseDeprecated(binding, scope, true))
323
			scope.problemReporter().deprecatedMethod(binding, this);
323
			scope.problemReporter().deprecatedMethod(binding, this);
324
		checkInvocationArguments(scope, null, allocationType, this.binding, this.arguments, argumentTypes, argsContainCast, this);
324
		checkInvocationArguments(scope, null, allocationType, this.binding, this.arguments, argumentTypes, argsContainCast, this);
325
325
(-)compiler/org/eclipse/jdt/internal/compiler/ast/JavadocAllocationExpression.java (-1 / +1 lines)
Lines 121-127 Link Here
121
				}
121
				}
122
			}
122
			}
123
		}
123
		}
124
		if (isMethodUseDeprecated(this.binding, scope)) {
124
		if (isMethodUseDeprecated(this.binding, scope, true)) {
125
			scope.problemReporter().javadocDeprecatedMethod(this.binding, this, scope.getDeclarationModifiers());
125
			scope.problemReporter().javadocDeprecatedMethod(this.binding, this, scope.getDeclarationModifiers());
126
		}
126
		}
127
		return allocationType;
127
		return allocationType;
(-)compiler/org/eclipse/jdt/internal/compiler/ast/ASTNode.java (-3 / +42 lines)
Lines 265-270 Link Here
265
			// ignore cases where field is used from within inside itself 
265
			// ignore cases where field is used from within inside itself 
266
			field.original().modifiers |= ExtraCompilerModifiers.AccLocallyUsed;
266
			field.original().modifiers |= ExtraCompilerModifiers.AccLocallyUsed;
267
		}
267
		}
268
		
269
		if ((field.modifiers & ExtraCompilerModifiers.AccRestrictedAccess) != 0) {
270
			AccessRestriction restriction = 
271
				scope.environment().getAccessRestriction(field.declaringClass);
272
			if (restriction != null) {
273
				scope.problemReporter().forbiddenReference(field, this,
274
						restriction.getFieldAccessMessageTemplate(), restriction.getProblemId());
275
			}
276
		}
268
	
277
	
269
		if (!field.isViewedAsDeprecated()) return false;
278
		if (!field.isViewedAsDeprecated()) return false;
270
	
279
	
Lines 284-301 Link Here
284
	/* Answer true if the method use is considered deprecated.
293
	/* Answer true if the method use is considered deprecated.
285
	* An access in the same compilation unit is allowed.
294
	* An access in the same compilation unit is allowed.
286
	*/
295
	*/
287
	public final boolean isMethodUseDeprecated(MethodBinding method, Scope scope) {
296
	public final boolean isMethodUseDeprecated(MethodBinding method, Scope scope,
288
297
			boolean isExplicitUse) {
289
		if ((method.isPrivate() || method.declaringClass.isLocalType()) && !scope.isDefinedInMethod(method)) {
298
		if ((method.isPrivate() || method.declaringClass.isLocalType()) && !scope.isDefinedInMethod(method)) {
290
			// ignore cases where method is used from within inside itself (e.g. direct recursions)
299
			// ignore cases where method is used from within inside itself (e.g. direct recursions)
291
			method.original().modifiers |= ExtraCompilerModifiers.AccLocallyUsed;
300
			method.original().modifiers |= ExtraCompilerModifiers.AccLocallyUsed;
292
		}
301
		}
293
		
302
		
303
		// TODO (maxime) consider separating concerns between deprecation and access restriction.
304
		// 				 Caveat: this was not the case when access restriction funtion was added.
305
		if (isExplicitUse && (method.modifiers & ExtraCompilerModifiers.AccRestrictedAccess) != 0) {
306
			// note: explicit constructors calls warnings are kept despite the 'new C1()' case (two
307
			//       warnings, one on type, the other on constructor), because of the 'super()' case.
308
			AccessRestriction restriction = 
309
				scope.environment().getAccessRestriction(method.declaringClass);
310
			if (restriction != null) {
311
				if (method.isConstructor()) {
312
					scope.problemReporter().forbiddenReference(method, this,
313
							restriction.getConstructorAccessMessageTemplate(),
314
							restriction.getProblemId());
315
				}
316
				else {
317
					scope.problemReporter().forbiddenReference(method, this,
318
							restriction.getMethodAccessMessageTemplate(),
319
							restriction.getProblemId());
320
				}
321
			}
322
		}
323
		
294
		if (!method.isViewedAsDeprecated()) return false;
324
		if (!method.isViewedAsDeprecated()) return false;
295
325
296
		// inside same unit - no report
326
		// inside same unit - no report
297
		if (scope.isDefinedInSameUnit(method.declaringClass)) return false;
327
		if (scope.isDefinedInSameUnit(method.declaringClass)) return false;
298
		
328
		
329
		// non explicit use and non explicitly deprecated - no report
330
		if (!isExplicitUse && 
331
				(method.modifiers & ClassFileConstants.AccDeprecated) == 0) {
332
			return false;		
333
		}
334
		
299
		// if context is deprecated, may avoid reporting
335
		// if context is deprecated, may avoid reporting
300
		if (!scope.compilerOptions().reportDeprecationInsideDeprecatedCode && scope.isInsideDeprecatedCode()) return false;
336
		if (!scope.compilerOptions().reportDeprecationInsideDeprecatedCode && scope.isInsideDeprecatedCode()) return false;
301
		return true;
337
		return true;
Lines 334-340 Link Here
334
				scope.problemReporter().forbiddenReference(type, this, restriction.getMessageTemplate(), restriction.getProblemId());
370
				scope.problemReporter().forbiddenReference(type, this, restriction.getMessageTemplate(), restriction.getProblemId());
335
			}
371
			}
336
		}
372
		}
337
		
373
374
		// force annotations resolution before deciding whether the type may be deprecated
375
		refType.getAnnotationTagBits();
376
	
338
		if (!refType.isViewedAsDeprecated()) return false;
377
		if (!refType.isViewedAsDeprecated()) return false;
339
		
378
		
340
		// inside same unit - no report
379
		// inside same unit - no report
(-)compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java (-1 / +1 lines)
Lines 431-437 Link Here
431
		}
431
		}
432
		// abstract private methods cannot occur nor abstract static............
432
		// abstract private methods cannot occur nor abstract static............
433
	}
433
	}
434
	if (isMethodUseDeprecated(binding, scope))
434
	if (isMethodUseDeprecated(binding, scope, true))
435
		scope.problemReporter().deprecatedMethod(binding, this);
435
		scope.problemReporter().deprecatedMethod(binding, this);
436
436
437
	// from 1.5 compliance on, array#clone() returns the array type (but binding still shows Object)
437
	// from 1.5 compliance on, array#clone() returns the array type (but binding still shows Object)
(-)model/org/eclipse/jdt/internal/core/builder/State.java (-3 / +7 lines)
Lines 330-338 Link Here
330
		int problemId = in.readInt();
330
		int problemId = in.readInt();
331
		accessRules[i] = new ClasspathAccessRule(pattern, problemId);
331
		accessRules[i] = new ClasspathAccessRule(pattern, problemId);
332
	}
332
	}
333
	String messageTemplate = in.readUTF();
333
	String[] messageTemplates = new String[AccessRuleSet.MESSAGE_TEMPLATES_LENGTH];
334
	for (int i = 0; i < 3; i++) {
335
		messageTemplates[i] = in.readUTF();
336
	}
334
	AccessRuleSet accessRuleSet = new AccessRuleSet(accessRules);
337
	AccessRuleSet accessRuleSet = new AccessRuleSet(accessRules);
335
	accessRuleSet.messageTemplate = messageTemplate;
338
	accessRuleSet.messageTemplates = messageTemplates;
336
	return accessRuleSet;
339
	return accessRuleSet;
337
}
340
}
338
341
Lines 601-607 Link Here
601
				writeName(accessRule.pattern, out);
604
				writeName(accessRule.pattern, out);
602
				out.writeInt(accessRule.problemId);
605
				out.writeInt(accessRule.problemId);
603
			}
606
			}
604
			out.writeUTF(accessRuleSet.messageTemplate);
607
			for (int i = 0; i < accessRuleSet.messageTemplates.length; i++)
608
				out.writeUTF(accessRuleSet.messageTemplates[i]);
605
		}
609
		}
606
	}
610
	}
607
}
611
}
(-)compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java (+36 lines)
Lines 1210-1215 Link Here
1210
		location.sourceStart,
1210
		location.sourceStart,
1211
		location.sourceEnd);
1211
		location.sourceEnd);
1212
}
1212
}
1213
public void forbiddenReference(MethodBinding method, ASTNode location, 
1214
		String messageTemplate, int problemId) {
1215
	if (method.isConstructor())
1216
		this.handle(
1217
			problemId,
1218
			new String[] { new String(method.readableName()) }, // distinct from msg arg for quickfix purpose
1219
			new String[] { 
1220
				MessageFormat.format(messageTemplate,
1221
						new String[]{new String(method.shortReadableName())})},
1222
			location.sourceStart,
1223
			location.sourceEnd);
1224
	else
1225
		this.handle(
1226
			problemId,
1227
			new String[] { new String(method.readableName()) }, // distinct from msg arg for quickfix purpose
1228
			new String[] { 
1229
				MessageFormat.format(messageTemplate, 
1230
					new String[]{
1231
						new String(method.shortReadableName()),
1232
				        new String(method.declaringClass.shortReadableName())})},
1233
			location.sourceStart,
1234
			location.sourceEnd);
1235
}
1236
public void forbiddenReference(FieldBinding field, ASTNode location, 
1237
		String messageTemplate, int problemId) {
1238
	this.handle(
1239
		problemId,
1240
		new String[] { new String(field.readableName()) }, // distinct from msg arg for quickfix purpose
1241
		new String[] { 
1242
			MessageFormat.format(messageTemplate, 
1243
				new String[]{
1244
					new String(field.shortReadableName()),
1245
			        new String(field.declaringClass.shortReadableName())})},
1246
		location.sourceStart,
1247
		location.sourceEnd);
1248
}
1213
public void forwardReference(Reference reference, int indexInQualification, TypeBinding type) {
1249
public void forwardReference(Reference reference, int indexInQualification, TypeBinding type) {
1214
	this.handle(
1250
	this.handle(
1215
		IProblem.ReferenceToForwardField,
1251
		IProblem.ReferenceToForwardField,
(-)batch/org/eclipse/jdt/internal/compiler/batch/Main.java (-2 / +1 lines)
Lines 2533-2540 Link Here
2533
			}
2533
			}
2534
		}
2534
		}
2535
		if (rulesOK) {
2535
		if (rulesOK) {
2536
			AccessRuleSet accessRuleSet = new AccessRuleSet(
2536
			AccessRuleSet accessRuleSet = new AccessRuleSet(accessRules);
2537
					accessRules, "{0}"); //$NON-NLS-1$
2538
			FileSystem.Classpath currentClasspath = FileSystem
2537
			FileSystem.Classpath currentClasspath = FileSystem
2539
					.getClasspath(currentClasspathName,
2538
					.getClasspath(currentClasspathName,
2540
							customEncoding, 0, accessRuleSet);
2539
							customEncoding, 0, accessRuleSet);
(-)eval/org/eclipse/jdt/internal/eval/CodeSnippetMessageSend.java (-1 / +1 lines)
Lines 335-341 Link Here
335
		}
335
		}
336
		// abstract private methods cannot occur nor abstract static............
336
		// abstract private methods cannot occur nor abstract static............
337
	}
337
	}
338
	if (isMethodUseDeprecated(binding, scope))
338
	if (isMethodUseDeprecated(binding, scope, true))
339
		scope.problemReporter().deprecatedMethod(binding, this);
339
		scope.problemReporter().deprecatedMethod(binding, this);
340
340
341
	// from 1.5 compliance on, array#clone() returns the array type (but binding still shows Object)
341
	// from 1.5 compliance on, array#clone() returns the array type (but binding still shows Object)
(-)eval/org/eclipse/jdt/internal/eval/CodeSnippetAllocationExpression.java (-1 / +1 lines)
Lines 191-197 Link Here
191
			return this.resolvedType;
191
			return this.resolvedType;
192
		}
192
		}
193
	}
193
	}
194
	if (isMethodUseDeprecated(this.binding, scope)) {
194
	if (isMethodUseDeprecated(this.binding, scope, true)) {
195
		scope.problemReporter().deprecatedMethod(this.binding, this);
195
		scope.problemReporter().deprecatedMethod(this.binding, this);
196
	}
196
	}
197
	if (arguments != null) {
197
	if (arguments != null) {
(-)model/org/eclipse/jdt/internal/core/util/messages.properties (+6 lines)
Lines 180-185 Link Here
180
### access restrictions
180
### access restrictions
181
restrictedAccess_project = The type {0} is not accessible due to restriction on required project {1}
181
restrictedAccess_project = The type {0} is not accessible due to restriction on required project {1}
182
restrictedAccess_library = The type {0} is not accessible due to restriction on required library {1}
182
restrictedAccess_library = The type {0} is not accessible due to restriction on required library {1}
183
restrictedAccess_constructor_project = The constructor {0} is not accessible due to restriction on required project {1}
184
restrictedAccess_constructor_library = The constructor {0} is not accessible due to restriction on required library {1}
185
restrictedAccess_field_project = The field {0} from the type {1} is not accessible due to restriction on required project {2}
186
restrictedAccess_field_library = The field {0} from the type {1} is not accessible due to restriction on required library {2}
187
restrictedAccess_method_project = The method {0} from the type {1} is not accessible due to restriction on required project {2}
188
restrictedAccess_method_library = The method {0} from the type {1} is not accessible due to restriction on required library {2}
183
189
184
### java conventions
190
### java conventions
185
convention_unit_nullName = Compilation unit name must not be null
191
convention_unit_nullName = Compilation unit name must not be null
(-)model/org/eclipse/jdt/internal/core/util/Messages.java (+6 lines)
Lines 170-175 Link Here
170
	public static String javamodel_initialization;
170
	public static String javamodel_initialization;
171
	public static String restrictedAccess_project;
171
	public static String restrictedAccess_project;
172
	public static String restrictedAccess_library;
172
	public static String restrictedAccess_library;
173
	public static String restrictedAccess_constructor_project;
174
	public static String restrictedAccess_constructor_library;
175
	public static String restrictedAccess_field_project;
176
	public static String restrictedAccess_field_library;
177
	public static String restrictedAccess_method_project;
178
	public static String restrictedAccess_method_library;
173
	public static String convention_unit_nullName;
179
	public static String convention_unit_nullName;
174
	public static String convention_unit_notJavaName;
180
	public static String convention_unit_notJavaName;
175
	public static String convention_classFile_nullName;
181
	public static String convention_classFile_nullName;
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java (+11 lines)
Lines 272-277 Link Here
272
			this.fields = new FieldBinding[size];
272
			this.fields = new FieldBinding[size];
273
			boolean use15specifics = sourceLevel >= ClassFileConstants.JDK1_5;
273
			boolean use15specifics = sourceLevel >= ClassFileConstants.JDK1_5;
274
			boolean isViewedAsDeprecated = isViewedAsDeprecated();
274
			boolean isViewedAsDeprecated = isViewedAsDeprecated();
275
			boolean hasRestrictedAccess = hasRestrictedAccess();
275
			for (int i = 0; i < size; i++) {
276
			for (int i = 0; i < size; i++) {
276
				IBinaryField binaryField = iFields[i];
277
				IBinaryField binaryField = iFields[i];
277
				char[] fieldSignature = use15specifics ? binaryField.getGenericSignature() : null;
278
				char[] fieldSignature = use15specifics ? binaryField.getGenericSignature() : null;
Lines 290-295 Link Here
290
					field.tagBits |= binaryField.getTagBits();
291
					field.tagBits |= binaryField.getTagBits();
291
				if (isViewedAsDeprecated && !field.isDeprecated())
292
				if (isViewedAsDeprecated && !field.isDeprecated())
292
					field.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly;
293
					field.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly;
294
				if (hasRestrictedAccess) {
295
					field.modifiers |= ExtraCompilerModifiers.AccRestrictedAccess;
296
				}
293
				if (fieldSignature != null)
297
				if (fieldSignature != null)
294
					field.modifiers |= ExtraCompilerModifiers.AccGenericSignature;
298
					field.modifiers |= ExtraCompilerModifiers.AccGenericSignature;
295
				this.fields[i] = field;
299
				this.fields[i] = field;
Lines 444-455 Link Here
444
	}
448
	}
445
449
446
	boolean isViewedAsDeprecated = isViewedAsDeprecated();
450
	boolean isViewedAsDeprecated = isViewedAsDeprecated();
451
	boolean hasRestrictedAccess = hasRestrictedAccess();
447
	this.methods = new MethodBinding[total];
452
	this.methods = new MethodBinding[total];
448
	if (total == initialTotal) {
453
	if (total == initialTotal) {
449
		for (int i = 0; i < initialTotal; i++) {
454
		for (int i = 0; i < initialTotal; i++) {
450
			MethodBinding method = createMethod(iMethods[i], sourceLevel);
455
			MethodBinding method = createMethod(iMethods[i], sourceLevel);
451
			if (isViewedAsDeprecated && !method.isDeprecated())
456
			if (isViewedAsDeprecated && !method.isDeprecated())
452
				method.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly;
457
				method.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly;
458
			if (hasRestrictedAccess) {
459
				method.modifiers |= ExtraCompilerModifiers.AccRestrictedAccess;
460
			}
453
			this.methods[i] = method;
461
			this.methods[i] = method;
454
		}
462
		}
455
	} else {
463
	} else {
Lines 458-463 Link Here
458
				MethodBinding method = createMethod(iMethods[i], sourceLevel);
466
				MethodBinding method = createMethod(iMethods[i], sourceLevel);
459
				if (isViewedAsDeprecated && !method.isDeprecated())
467
				if (isViewedAsDeprecated && !method.isDeprecated())
460
					method.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly;
468
					method.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly;
469
				if (hasRestrictedAccess) {
470
					method.modifiers |= ExtraCompilerModifiers.AccRestrictedAccess;
471
				}
461
				this.methods[index++] = method;
472
				this.methods[index++] = method;
462
			}
473
			}
463
		}
474
		}
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java (+9 lines)
Lines 644-649 Link Here
644
		} finally {
644
		} finally {
645
			typeDecl.staticInitializerScope.insideTypeAnnotation = old;
645
			typeDecl.staticInitializerScope.insideTypeAnnotation = old;
646
		}
646
		}
647
		if ((this.tagBits & AnnotationDeprecated) != 0) {
648
			this.modifiers |= ClassFileConstants.AccDeprecated;
649
		}
647
	}
650
	}
648
	return this.tagBits;
651
	return this.tagBits;
649
}
652
}
Lines 1085-1090 Link Here
1085
	}
1088
	}
1086
	if (isViewedAsDeprecated() && !field.isDeprecated())
1089
	if (isViewedAsDeprecated() && !field.isDeprecated())
1087
		field.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly;	
1090
		field.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly;	
1091
	if (hasRestrictedAccess()) {
1092
		field.modifiers |= ExtraCompilerModifiers.AccRestrictedAccess;
1093
	}
1088
	FieldDeclaration[] fieldDecls = scope.referenceContext.fields;
1094
	FieldDeclaration[] fieldDecls = scope.referenceContext.fields;
1089
	for (int f = 0, length = fieldDecls.length; f < length; f++) {
1095
	for (int f = 0, length = fieldDecls.length; f < length; f++) {
1090
		if (fieldDecls[f].binding != field)
1096
		if (fieldDecls[f].binding != field)
Lines 1138-1143 Link Here
1138
	}
1144
	}
1139
	if (isViewedAsDeprecated() && !method.isDeprecated())
1145
	if (isViewedAsDeprecated() && !method.isDeprecated())
1140
		method.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly;
1146
		method.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly;
1147
	if (hasRestrictedAccess()) {
1148
		method.modifiers |= ExtraCompilerModifiers.AccRestrictedAccess;
1149
	}
1141
1150
1142
	AbstractMethodDeclaration methodDecl = method.sourceMethod();
1151
	AbstractMethodDeclaration methodDecl = method.sourceMethod();
1143
	if (methodDecl == null) return null; // method could not be resolved in previous iteration
1152
	if (methodDecl == null) return null; // method could not be resolved in previous iteration
(-)compiler/org/eclipse/jdt/internal/compiler/env/AccessRestriction.java (-4 / +16 lines)
Lines 13-22 Link Here
13
public class AccessRestriction {
13
public class AccessRestriction {
14
14
15
	private AccessRule accessRule;
15
	private AccessRule accessRule;
16
	private String messageTemplate;
16
	private String[] messageTemplates;
17
	public AccessRestriction(AccessRule accessRule, String messageTemplate) {
17
	public AccessRestriction(AccessRule accessRule, String [] messageTemplates) {
18
		this.accessRule = accessRule;
18
		this.accessRule = accessRule;
19
		this.messageTemplate = messageTemplate;
19
		this.messageTemplates = messageTemplates;
20
	}
20
	}
21
	
21
	
22
	/**
22
	/**
Lines 25-33 Link Here
25
	 * e.g. "{0} has restricted access"
25
	 * e.g. "{0} has restricted access"
26
	 */
26
	 */
27
	public String getMessageTemplate() {
27
	public String getMessageTemplate() {
28
		return this.messageTemplate;
28
		return this.messageTemplates[0];
29
	}
29
	}
30
	
30
	
31
	public String getConstructorAccessMessageTemplate() {
32
		return this.messageTemplates[1];
33
	}
34
35
	public String getMethodAccessMessageTemplate() {
36
		return this.messageTemplates[2];
37
	}
38
39
	public String getFieldAccessMessageTemplate() {
40
		return this.messageTemplates[3];
41
	}
42
31
	public int getProblemId() {
43
	public int getProblemId() {
32
		return this.accessRule.problemId;
44
		return this.accessRule.problemId;
33
	}
45
	}
(-)compiler/org/eclipse/jdt/internal/compiler/env/AccessRuleSet.java (-11 / +30 lines)
Lines 19-34 Link Here
19
public class AccessRuleSet {
19
public class AccessRuleSet {
20
20
21
	private AccessRule[] accessRules;
21
	private AccessRule[] accessRules;
22
	public String messageTemplate;
22
	public String[] messageTemplates;
23
	public static final int MESSAGE_TEMPLATES_LENGTH = 4;
23
	
24
	
24
	
25
	
25
	public AccessRuleSet(AccessRule[] accessRules) {
26
	public AccessRuleSet(AccessRule[] accessRules) {
26
		this.accessRules = accessRules;
27
		this(accessRules, null);
27
	}
28
	}
28
	public AccessRuleSet(AccessRule[] accessRules, String messageTemplate) {
29
	
30
	/**
31
	 * Make a new set of access rules.
32
	 * @param accessRules the access rules to be contained by the new set
33
	 * @param messageTemplates a Sting[4] array specifying the messages for type, 
34
	 * constructor, method and field access violation; each should contain as many
35
	 * placeholders as expected by the respective access violation message (that is,
36
	 * one for type and constructor, two for method and field).
37
	 */
38
	// TODO (maxime) move to better support
39
	public AccessRuleSet(AccessRule[] accessRules, String[] messageTemplates) {
29
		this.accessRules = accessRules;
40
		this.accessRules = accessRules;
30
		this.messageTemplate = messageTemplate;
41
		if (messageTemplates != null && messageTemplates.length == MESSAGE_TEMPLATES_LENGTH)
42
			this.messageTemplates = messageTemplates;
43
		else
44
			this.messageTemplates = new String[] {"{0}", "{0}", "{0} {1}", "{0} {1}"};  //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$
31
	}
45
	}
46
	
32
	/**
47
	/**
33
	 * @see java.lang.Object#equals(java.lang.Object)
48
	 * @see java.lang.Object#equals(java.lang.Object)
34
	 */
49
	 */
Lines 38-45 Link Here
38
		if (!(object instanceof AccessRuleSet))
53
		if (!(object instanceof AccessRuleSet))
39
			return false;
54
			return false;
40
		AccessRuleSet otherRuleSet = (AccessRuleSet) object;
55
		AccessRuleSet otherRuleSet = (AccessRuleSet) object;
41
		if (!this.messageTemplate.equals(otherRuleSet.messageTemplate)) 
56
		if (this.messageTemplates.length != MESSAGE_TEMPLATES_LENGTH ||
42
			return false;
57
				otherRuleSet.messageTemplates.length != MESSAGE_TEMPLATES_LENGTH)
58
			return false; // guard
59
		for (int i = 0; i < MESSAGE_TEMPLATES_LENGTH; i++) 
60
			if (!this.messageTemplates[i].equals(otherRuleSet.messageTemplates[i])) 
61
				return false;
43
		int rulesLength = this.accessRules.length;
62
		int rulesLength = this.accessRules.length;
44
		if (rulesLength != otherRuleSet.accessRules.length) return false;
63
		if (rulesLength != otherRuleSet.accessRules.length) return false;
45
		for (int i = 0; i < rulesLength; i++)
64
		for (int i = 0; i < rulesLength; i++)
Lines 67-73 Link Here
67
			switch (accessRule.problemId) {
86
			switch (accessRule.problemId) {
68
				case IProblem.ForbiddenReference:
87
				case IProblem.ForbiddenReference:
69
				case IProblem.DiscouragedReference:
88
				case IProblem.DiscouragedReference:
70
					return new AccessRestriction(accessRule, this.messageTemplate);
89
					return new AccessRestriction(accessRule, this.messageTemplates);
71
				default:
90
				default:
72
					return null;
91
					return null;
73
			}
92
			}
Lines 95-104 Link Here
95
			else if (i < length-1)
114
			else if (i < length-1)
96
				buffer.append(", "); //$NON-NLS-1$
115
				buffer.append(", "); //$NON-NLS-1$
97
		}
116
		}
98
		buffer
117
		buffer.append("} [templates:\""); //$NON-NLS-1$
99
			.append("} [template:\"") //$NON-NLS-1$
118
		for (int i = 0; i < messageTemplates.length; i++)
100
			.append(this.messageTemplate)
119
			buffer.append(this.messageTemplates[i]);
101
			.append("\"]"); //$NON-NLS-1$
120
		buffer.append("\"]"); //$NON-NLS-1$
102
		return buffer.toString();
121
		return buffer.toString();
103
	}
122
	}
104
}
123
}
(-)model/org/eclipse/jdt/internal/core/ClasspathEntry.java (-4 / +24 lines)
Lines 220-226 Link Here
220
		AccessRuleSet ruleSet = createAccessRuleSet(accessRules);
220
		AccessRuleSet ruleSet = createAccessRuleSet(accessRules);
221
		if (ruleSet != null) {
221
		if (ruleSet != null) {
222
			// compute message template
222
			// compute message template
223
			ruleSet.messageTemplate = getMessageTemplate();
223
			ruleSet.messageTemplates = getMessageTemplates();
224
		}
224
		}
225
		this.accessRuleSet = ruleSet;
225
		this.accessRuleSet = ruleSet;
226
		
226
		
Lines 944-954 Link Here
944
		return this.extraAttributes;
944
		return this.extraAttributes;
945
	}
945
	}
946
	
946
	
947
	private String getMessageTemplate() {
947
	private String[] getMessageTemplates() {
948
		String [] result = new String[AccessRuleSet.MESSAGE_TEMPLATES_LENGTH];
948
		if (this.entryKind == CPE_PROJECT || this.entryKind == CPE_SOURCE) { // can be remote source entry when reconciling
949
		if (this.entryKind == CPE_PROJECT || this.entryKind == CPE_SOURCE) { // can be remote source entry when reconciling
949
			return Messages.bind(
950
			result[0] = Messages.bind(
950
				org.eclipse.jdt.internal.core.util.Messages.restrictedAccess_project,
951
				org.eclipse.jdt.internal.core.util.Messages.restrictedAccess_project,
951
				new String[] {"{0}", getPath().segment(0)});  //$NON-NLS-1$
952
				new String[] {"{0}", getPath().segment(0)});  //$NON-NLS-1$
953
			result[1] = Messages.bind(
954
					org.eclipse.jdt.internal.core.util.Messages.restrictedAccess_constructor_project,
955
					new String[] {"{0}", getPath().segment(0)});  //$NON-NLS-1$
956
			result[2] = Messages.bind(
957
					org.eclipse.jdt.internal.core.util.Messages.restrictedAccess_method_project,
958
					new String[] {"{0}", "{1}", getPath().segment(0)});  //$NON-NLS-1$ //$NON-NLS-2$
959
			result[3] = Messages.bind(
960
					org.eclipse.jdt.internal.core.util.Messages.restrictedAccess_field_project,
961
					new String[] {"{0}", "{1}", getPath().segment(0)});  //$NON-NLS-1$ //$NON-NLS-2$
952
		} else {
962
		} else {
953
			IPath libPath = getPath();
963
			IPath libPath = getPath();
954
			Object target = JavaModel.getTarget(ResourcesPlugin.getWorkspace().getRoot(), libPath, false);
964
			Object target = JavaModel.getTarget(ResourcesPlugin.getWorkspace().getRoot(), libPath, false);
Lines 957-966 Link Here
957
				pathString = libPath.toOSString();
967
				pathString = libPath.toOSString();
958
			else
968
			else
959
				pathString = libPath.makeRelative().toString();
969
				pathString = libPath.makeRelative().toString();
960
			return Messages.bind(
970
			result[0] = Messages.bind(
961
				org.eclipse.jdt.internal.core.util.Messages.restrictedAccess_library,
971
				org.eclipse.jdt.internal.core.util.Messages.restrictedAccess_library,
962
				new String[] {"{0}", pathString}); //$NON-NLS-1$ 
972
				new String[] {"{0}", pathString}); //$NON-NLS-1$ 
973
			result[1] = Messages.bind(
974
					org.eclipse.jdt.internal.core.util.Messages.restrictedAccess_constructor_library,
975
					new String[] {"{0}", pathString}); //$NON-NLS-1$ 
976
			result[2] = Messages.bind(
977
					org.eclipse.jdt.internal.core.util.Messages.restrictedAccess_method_library,
978
					new String[] {"{0}", "{1}", pathString}); //$NON-NLS-1$ //$NON-NLS-2$ 
979
			result[3] = Messages.bind(
980
					org.eclipse.jdt.internal.core.util.Messages.restrictedAccess_field_library,
981
					new String[] {"{0}", "{1}", pathString}); //$NON-NLS-1$ //$NON-NLS-2$ 
963
		}
982
		}
983
		return result;
964
	}
984
	}
965
985
966
	/**
986
	/**
(-)src/org/eclipse/jdt/core/tests/model/AllJavaModelTests.java (+3 lines)
Lines 125-130 Link Here
125
		// Inclusion patterns tests
125
		// Inclusion patterns tests
126
		InclusionPatternsTests.class,
126
		InclusionPatternsTests.class,
127
		
127
		
128
		// Access restrictions tests
129
		AccessRestrictionsTests.class,
130
		
128
		// Signature tests
131
		// Signature tests
129
		SignatureTests.class,
132
		SignatureTests.class,
130
		
133
		
(-)src/org/eclipse/jdt/core/tests/model/ReconcilerTests.java (-12 / +2 lines)
Lines 2083-2097 Link Here
2083
			"----------\n" + 
2083
			"----------\n" + 
2084
			"1. WARNING in /Reconciler15/src/Y.java (at line 1)\n" + 
2084
			"1. WARNING in /Reconciler15/src/Y.java (at line 1)\n" + 
2085
			"	public class Y extends X {\n" + 
2085
			"	public class Y extends X {\n" + 
2086
			"	             ^\n" + 
2087
			"The constructor X() is deprecated\n" + 
2088
			"----------\n" + 
2089
			"2. WARNING in /Reconciler15/src/Y.java (at line 1)\n" + 
2090
			"	public class Y extends X {\n" + 
2091
			"	                       ^\n" + 
2086
			"	                       ^\n" + 
2092
			"The type X is deprecated\n" + 
2087
			"The type X is deprecated\n" + 
2093
			"----------\n" + 
2088
			"----------\n" + 
2094
			"3. ERROR in /Reconciler15/src/Y.java (at line 4)\n" + 
2089
			"2. ERROR in /Reconciler15/src/Y.java (at line 4)\n" + 
2095
			"	Zork z;\n" + 
2090
			"	Zork z;\n" + 
2096
			"	^^^^\n" + 
2091
			"	^^^^\n" + 
2097
			"Zork cannot be resolved to a type\n" + 
2092
			"Zork cannot be resolved to a type\n" + 
Lines 2219-2233 Link Here
2219
			"----------\n" + 
2214
			"----------\n" + 
2220
			"1. WARNING in /Reconciler15/src/Y.java (at line 1)\n" + 
2215
			"1. WARNING in /Reconciler15/src/Y.java (at line 1)\n" + 
2221
			"	public class Y extends X {\n" + 
2216
			"	public class Y extends X {\n" + 
2222
			"	             ^\n" + 
2223
			"The constructor X() is deprecated\n" + 
2224
			"----------\n" + 
2225
			"2. WARNING in /Reconciler15/src/Y.java (at line 1)\n" + 
2226
			"	public class Y extends X {\n" + 
2227
			"	                       ^\n" + 
2217
			"	                       ^\n" + 
2228
			"The type X is deprecated\n" + 
2218
			"The type X is deprecated\n" + 
2229
			"----------\n" + 
2219
			"----------\n" + 
2230
			"3. ERROR in /Reconciler15/src/Y.java (at line 4)\n" + 
2220
			"2. ERROR in /Reconciler15/src/Y.java (at line 4)\n" + 
2231
			"	Zork z;\n" + 
2221
			"	Zork z;\n" + 
2232
			"	^^^^\n" + 
2222
			"	^^^^\n" + 
2233
			"Zork cannot be resolved to a type\n" + 
2223
			"Zork cannot be resolved to a type\n" + 
(-)src/org/eclipse/jdt/core/tests/model/ModifyingResourceTests.java (-5 / +13 lines)
Lines 295-306 Link Here
295
	for (int j = 0; j < ruleCount; j++) {
295
	for (int j = 0; j < ruleCount; j++) {
296
		String rule = tokenizer.nextToken();
296
		String rule = tokenizer.nextToken();
297
		int kind;
297
		int kind;
298
		if (rule.charAt(0) == '+') {
298
		switch (rule.charAt(0)) {
299
			kind = IAccessRule.K_ACCESSIBLE;
299
			case '+':
300
		} else {
300
				kind = IAccessRule.K_ACCESSIBLE;
301
			kind = IAccessRule.K_NON_ACCESSIBLE;
301
				break;
302
			nonAccessibleRules++;
302
			case '~':
303
				kind = IAccessRule.K_DISCOURAGED;
304
				break;
305
			case '-':
306
			default:		// TODO (maxime) consider forbidding unspecified rule start; this one tolerates
307
							// 		shortcuts that only specify a path matching pattern
308
				kind = IAccessRule.K_NON_ACCESSIBLE;
309
				break;
303
		}
310
		}
311
		nonAccessibleRules++;
304
		accessRules[j] = JavaCore.newAccessRule(new Path(rule.substring(1)), kind);
312
		accessRules[j] = JavaCore.newAccessRule(new Path(rule.substring(1)), kind);
305
	}
313
	}
306
314
(-)src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java (-1 / +1 lines)
Lines 56-62 Link Here
56
	public static class ProblemRequestor implements IProblemRequestor {
56
	public static class ProblemRequestor implements IProblemRequestor {
57
		public StringBuffer problems;
57
		public StringBuffer problems;
58
		public int problemCount;
58
		public int problemCount;
59
		private char[] unitSource;
59
		protected char[] unitSource;
60
		public ProblemRequestor() {
60
		public ProblemRequestor() {
61
			initialize(null);
61
			initialize(null);
62
		}
62
		}
(-)src/org/eclipse/jdt/core/tests/model/AccessRestrictionsTests.java (+483 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2005 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
 *******************************************************************************/
11
package org.eclipse.jdt.core.tests.model;
12
13
14
import junit.framework.Test;
15
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.jdt.core.*;
18
import org.eclipse.jdt.core.compiler.IProblem;
19
20
public class AccessRestrictionsTests extends ModifyingResourceTests {
21
	static class ProblemRequestor extends AbstractJavaModelTests.ProblemRequestor {
22
		ProblemRequestor (String source) {
23
			if (source != null) 
24
				unitSource = source.toCharArray();
25
		}
26
		ProblemRequestor() {
27
		}
28
		public void acceptProblem(IProblem problem) {
29
			super.acceptProblem(problem);
30
		}
31
	}
32
	
33
	protected ProblemRequestor problemRequestor;
34
35
	public AccessRestrictionsTests(String name) {
36
		super(name);
37
	}
38
	
39
	// Use this static initializer to specify subset for tests
40
	// All specified tests which do not belong to the class are skipped...
41
	static {
42
		// Names of tests to run, like "testXXX"
43
  		//TESTS_NAMES = new String[] { "test004" };
44
		// Numbers of tests to run: "test<number>" will be run for each number of this array
45
		//TESTS_NUMBERS = new int[] { 1 };
46
		// Range numbers of tests to run: all tests between "test<first>" and "test<last>" will be run for { first, last }
47
		//TESTS_RANGE = new int[] { 16, -1 };
48
	}
49
50
	public static Test suite() {
51
		return buildTestSuite(AccessRestrictionsTests.class);
52
	}
53
54
	protected void assertProblems(String message, String expected) {
55
		assertProblems(message, expected, this.problemRequestor);
56
	}
57
58
/*
59
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=76266
60
 * Ensures that a problem is created for a reference to a member of a type that is not
61
 * accessible in a prereq project, even though it is accessed through an intermediate 
62
 * accessible mother class.
63
 * Checking methods.
64
 */
65
public void test001() throws CoreException {
66
	ICompilationUnit x1 = null, x2 = null, y =  null, z = null;
67
	try {
68
		WorkingCopyOwner owner = new WorkingCopyOwner(){};
69
		createJavaProject(
70
			"P1", 
71
			new String[] {"src"}, 
72
			new String[] {"JCL_LIB"}, 
73
			"bin");
74
		this.problemRequestor = new ProblemRequestor();
75
		x1 = getWorkingCopy(
76
			"/P1/src/p/X1.java",
77
			"package p;\n" +
78
			"public class X1 {\n" +
79
			"	void foo() {\n" +
80
			"	}\n" +
81
			"}",
82
			owner,
83
			this.problemRequestor);	
84
		assertProblems(
85
			"Unexpected problems", 
86
			"----------\n----------\n----------\n----------\n"
87
		);
88
		this.problemRequestor = new ProblemRequestor();
89
		x2 = getWorkingCopy(			
90
			"/P1/src/p/X2.java",
91
			"package p;\n" +
92
			"public class X2 extends X1 {\n" +
93
			"	void bar() {\n" +
94
			"	}\n" +
95
			"}",			owner,
96
			this.problemRequestor);
97
		assertProblems(
98
			"Unexpected problems", 
99
			"----------\n----------\n----------\n----------\n"
100
		);
101
		IJavaProject p2 = createJavaProject("P2", new String[] {"src"}, 
102
				new String[] {"JCL_LIB"}, "bin");
103
		IClasspathEntry[] classpath = p2.getRawClasspath();
104
		int length = classpath.length;
105
		System.arraycopy(classpath, 0, classpath = new IClasspathEntry[length+1], 0, length);
106
		classpath[length] = createSourceEntry("P2", "/P1", "-p/X1");
107
		p2.setRawClasspath(classpath, null);
108
		// check the most basic case
109
		String src =
110
			"package p;\n" +
111
			"public class Z extends X1 {\n" +
112
			"}";
113
		this.problemRequestor = new ProblemRequestor(src);
114
		z = getWorkingCopy(			
115
			"/P2/src/p/Z.java", 
116
			src,
117
			owner,
118
			this.problemRequestor);
119
		assertProblems(
120
			"Unexpected problems value", 
121
			"----------\n----------\n----------\n" + 
122
			"1. ERROR in /P2/src/p/Z.java (at line 2)\n" + 
123
			"	public class Z extends X1 {\n" + 
124
			"	                       ^^\n" + 
125
			"Access restriction: The type X1 is not accessible due to restriction on required project P1\n" + 
126
			"----------\n"
127
		);
128
		// check the specifics of this test case
129
		src = 
130
			"package p;\n" +
131
			"public class Y extends X2 {\n" +
132
			"	void foobar() {\n" +
133
			"		foo(); // accesses X1.foo, should trigger an error\n" +
134
			"		bar(); // accesses X2.bar, OK\n" +
135
			"	}\n" +
136
			"}";
137
		this.problemRequestor = new ProblemRequestor(src);
138
		y = getWorkingCopy(			
139
			"/P2/src/p/Y.java", 
140
			src,
141
			owner,
142
			this.problemRequestor);
143
		assertProblems(
144
			"Unexpected problems value", 
145
			"----------\n" + 
146
			"----------\n" + 
147
			"----------\n" + 
148
			"1. ERROR in /P2/src/p/Y.java (at line 4)\n" + 
149
			"	foo(); // accesses X1.foo, should trigger an error\n" + 
150
			"	^^^^^\n" + 
151
			"Access restriction: The method foo() from the type X1 is not accessible due to restriction on required project P1\n" + 
152
			"----------\n"
153
		);
154
	} finally {
155
		if (x1 != null)
156
			x1.discardWorkingCopy();
157
		if (x2 != null)
158
			x2.discardWorkingCopy();
159
		if (y != null)
160
			y.discardWorkingCopy();
161
		if (z != null)
162
			z.discardWorkingCopy();
163
		deleteProjects(new String[] {"P1", "P2"});
164
	}
165
}
166
167
/*
168
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=76266
169
 * Ensures that a problem is created for a reference to a member of a type that is not
170
 * accessible in a prereq project, even though it is accessed through an intermediate 
171
 * accessible mother class.
172
 * Checking members.
173
 */
174
public void test002() throws CoreException {
175
	ICompilationUnit x1 = null, x2 = null, y =  null, z = null;
176
	try {
177
		WorkingCopyOwner owner = new WorkingCopyOwner(){};
178
		createJavaProject(
179
			"P1", 
180
			new String[] {"src"}, 
181
			new String[] {"JCL_LIB"}, 
182
			"bin");
183
		this.problemRequestor = new ProblemRequestor();
184
		x1 = getWorkingCopy(
185
			"/P1/src/p/X1.java",
186
			"package p;\n" +
187
			"public class X1 {\n" +
188
			"	int m1;\n" +
189
			"}",
190
			owner,
191
			this.problemRequestor);	
192
		assertProblems(
193
			"Unexpected problems", 
194
			"----------\n----------\n----------\n----------\n"
195
		);
196
		this.problemRequestor = new ProblemRequestor();
197
		x2 = getWorkingCopy(			
198
			"/P1/src/p/X2.java",
199
			"package p;\n" +
200
			"public class X2 extends X1 {\n" +
201
			"	char m2;\n" +
202
			"}",
203
			owner,
204
			this.problemRequestor);
205
		assertProblems(
206
			"Unexpected problems", 
207
			"----------\n----------\n----------\n----------\n"
208
		);
209
		IJavaProject p2 = createJavaProject("P2", new String[] {"src"}, 
210
				new String[] {"JCL_LIB"}, "bin");
211
		IClasspathEntry[] classpath = p2.getRawClasspath();
212
		int length = classpath.length;
213
		System.arraycopy(classpath, 0, classpath = new IClasspathEntry[length+1], 0, length);
214
		classpath[length] = createSourceEntry("P2", "/P1", "-p/X1");
215
		p2.setRawClasspath(classpath, null);
216
		// check the most basic case
217
		String src =
218
			"package p;\n" +
219
			"public class Z extends X1 {\n" +
220
			"}";
221
		this.problemRequestor = new ProblemRequestor(src);
222
		z = getWorkingCopy(			
223
			"/P2/src/p/Z.java", 
224
			src,
225
			owner,
226
			this.problemRequestor);
227
		assertProblems(
228
			"Unexpected problems value", 
229
			"----------\n----------\n----------\n" + 
230
			"1. ERROR in /P2/src/p/Z.java (at line 2)\n" + 
231
			"	public class Z extends X1 {\n" + 
232
			"	                       ^^\n" + 
233
			"Access restriction: The type X1 is not accessible due to restriction on required project P1\n" + 
234
			"----------\n"
235
		);
236
		// check the specifics of this test case
237
		src =
238
			"package p;\n" +
239
			"public class Y extends X2 {\n" +
240
			"	void foobar() {\n" +
241
			"		int l1 = m1; // accesses X1.m1, should trigger an error\n" +
242
			"		char l2 = m2; // accesses X2.m2, OK\n" +
243
			"	}\n" +
244
			"}";
245
		this.problemRequestor = new ProblemRequestor(src);
246
		y = getWorkingCopy(			
247
			"/P2/src/p/Y.java", 
248
			src,
249
			owner,
250
			this.problemRequestor);
251
		assertProblems(
252
			"Unexpected problems value", 
253
			"----------\n" + 
254
			"----------\n" + 
255
			"----------\n" + 
256
			"1. ERROR in /P2/src/p/Y.java (at line 4)\n" + 
257
			"	int l1 = m1; // accesses X1.m1, should trigger an error\n" + 
258
			"	         ^^\n" + 
259
			"Access restriction: The field m1 from the type X1 is not accessible due to restriction on required project P1\n" + 
260
			"----------\n"
261
		);
262
	} finally {
263
		if (x1 != null)
264
			x1.discardWorkingCopy();
265
		if (x2 != null)
266
			x2.discardWorkingCopy();
267
		if (y != null)
268
			y.discardWorkingCopy();
269
		if (z != null)
270
			z.discardWorkingCopy();
271
		deleteProjects(new String[] {"P1", "P2"});
272
	}
273
}
274
275
/*
276
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=76266
277
 * Ensures that a problem is created for a reference to a member of a type that is not
278
 * accessible in a prereq project, even though it is accessed through an intermediate 
279
 * accessible mother class.
280
 * Checking member types.
281
 */
282
public void test003() throws CoreException {
283
	ICompilationUnit x1 = null, x2 = null, y =  null, z = null;
284
	try {
285
		WorkingCopyOwner owner = new WorkingCopyOwner(){};
286
		createJavaProject(
287
			"P1", 
288
			new String[] {"src"}, 
289
			new String[] {"JCL_LIB"}, 
290
			"bin");
291
		this.problemRequestor = new ProblemRequestor();
292
		x1 = getWorkingCopy(
293
			"/P1/src/p/X1.java",
294
			"package p;\n" +
295
			"public class X1 {\n" +
296
			"	class C1 {\n" +
297
			"	   protected C1 (int dummy) {}\n" +
298
			"	   protected void foo() {}\n" +
299
			"	}\n" +
300
			"	interface I1 {}\n" +
301
			"}",
302
			owner,
303
			this.problemRequestor);	
304
		assertProblems(
305
			"Unexpected problems", 
306
			"----------\n----------\n----------\n----------\n"
307
		);
308
		this.problemRequestor = new ProblemRequestor();
309
		x2 = getWorkingCopy(			
310
			"/P1/src/p/X2.java",
311
			"package p;\n" +
312
			"public class X2 extends X1 {\n" +
313
			"	class C2 {}\n" +
314
			"	interface I2 {}\n" +
315
			"}",
316
			owner,
317
			this.problemRequestor);
318
		assertProblems(
319
			"Unexpected problems", 
320
			"----------\n----------\n----------\n----------\n"
321
		);
322
		IJavaProject p2 = createJavaProject("P2", new String[] {"src"}, 
323
				new String[] {"JCL_LIB"}, "bin");
324
		IClasspathEntry[] classpath = p2.getRawClasspath();
325
		int length = classpath.length;
326
		System.arraycopy(classpath, 0, classpath = new IClasspathEntry[length+1], 0, length);
327
		classpath[length] = createSourceEntry("P2", "/P1", "-p/X1");
328
		p2.setRawClasspath(classpath, null);
329
		// check the most basic case
330
		String src =
331
			"package p;\n" +
332
			"public class Z extends X1 {\n" +
333
			"}";
334
		this.problemRequestor = new ProblemRequestor(src);
335
		z = getWorkingCopy(			
336
			"/P2/src/p/Z.java", 
337
			src,
338
			owner,
339
			this.problemRequestor);
340
		assertProblems(
341
			"Unexpected problems value", 
342
			"----------\n----------\n----------\n" + 
343
			"1. ERROR in /P2/src/p/Z.java (at line 2)\n" + 
344
			"	public class Z extends X1 {\n" + 
345
			"	                       ^^\n" + 
346
			"Access restriction: The type X1 is not accessible due to restriction on required project P1\n" + 
347
			"----------\n"
348
		);
349
		// check the specifics of this test case
350
		src =
351
			"package p;\n" +
352
			"public class Y extends X2 {\n" +
353
			"	class C3a extends C1 {      // error\n" +
354
			"	   C3a() {\n" +
355
			"	      super(0);\n" +
356
			"	      foo();                // error\n" +
357
			"	   }\n" +
358
			"	}\n" +
359
			"	class C3c extends C2 implements I2 {}\n" +
360
			"	void foobar() {\n" +
361
			"		C1 m1 =                 // error\n" +
362
			"		        new C1(0);      // error\n" +
363
			"		C2 m2 = new C2();\n" +
364
			"	}\n" +
365
			"}";
366
		this.problemRequestor = new ProblemRequestor(src);
367
		y = getWorkingCopy(			
368
			"/P2/src/p/Y.java", 
369
			src,
370
			owner,
371
			this.problemRequestor);
372
		assertProblems(
373
			"Unexpected problems value", 
374
			"----------\n" + 
375
			"----------\n" + 
376
			"----------\n" + 
377
			"1. ERROR in /P2/src/p/Y.java (at line 3)\n" + 
378
			"	class C3a extends C1 {      // error\n" + 
379
			"	                  ^^\n" + 
380
			"Access restriction: The type X1.C1 is not accessible due to restriction on required project P1\n" + 
381
			"----------\n" + 
382
			"2. ERROR in /P2/src/p/Y.java (at line 5)\n" + 
383
			"	super(0);\n" + 
384
			"	^^^^^^^^\n" + 
385
			"Access restriction: The constructor X1.C1(int) is not accessible due to restriction on required project P1\n" + 
386
			"----------\n" + 
387
			"3. ERROR in /P2/src/p/Y.java (at line 6)\n" + 
388
			"	foo();                // error\n" + 
389
			"	^^^^^\n" + 
390
			"Access restriction: The method foo() from the type X1.C1 is not accessible due to restriction on required project P1\n" + 
391
			"----------\n" + 
392
			"4. ERROR in /P2/src/p/Y.java (at line 11)\n" + 
393
			"	C1 m1 =                 // error\n" + 
394
			"	^^\n" + 
395
			"Access restriction: The type X1.C1 is not accessible due to restriction on required project P1\n" + 
396
			"----------\n" + 
397
			"5. ERROR in /P2/src/p/Y.java (at line 12)\n" + 
398
			"	new C1(0);      // error\n" + 
399
			"	^^^^^^^^^\n" + 
400
			"Access restriction: The constructor X1.C1(int) is not accessible due to restriction on required project P1\n" + 
401
			"----------\n" + 
402
			"6. ERROR in /P2/src/p/Y.java (at line 12)\n" + 
403
			"	new C1(0);      // error\n" + 
404
			"	    ^^\n" + 
405
			"Access restriction: The type X1.C1 is not accessible due to restriction on required project P1\n" + 
406
			"----------\n"
407
		);
408
	} finally {
409
		if (x1 != null)
410
			x1.discardWorkingCopy();
411
		if (x2 != null)
412
			x2.discardWorkingCopy();
413
		if (y != null)
414
			y.discardWorkingCopy();
415
		if (z != null)
416
			z.discardWorkingCopy();
417
		deleteProjects(new String[] {"P1", "P2"});
418
	}
419
}
420
421
/*
422
 * Discouraged access message - type via discouraged rule.
423
 */
424
public void test004() throws CoreException {
425
	ICompilationUnit x1 = null, z = null;
426
	try {
427
		WorkingCopyOwner owner = new WorkingCopyOwner(){};
428
		createJavaProject(
429
			"P1", 
430
			new String[] {"src"}, 
431
			new String[] {"JCL_LIB"}, 
432
			"bin");
433
		this.problemRequestor = new ProblemRequestor();
434
		x1 = getWorkingCopy(
435
			"/P1/src/p/X1.java",
436
			"package p;\n" +
437
			"public class X1 {\n" +
438
			"	class C1 {}\n" +
439
			"	interface I1 {}\n" +
440
			"}",
441
			owner,
442
			this.problemRequestor);	
443
		assertProblems(
444
			"Unexpected problems", 
445
			"----------\n----------\n----------\n----------\n"
446
		);
447
		IJavaProject p2 = createJavaProject("P2", new String[] {"src"}, 
448
				new String[] {"JCL_LIB"}, "bin");
449
		IClasspathEntry[] classpath = p2.getRawClasspath();
450
		int length = classpath.length;
451
		System.arraycopy(classpath, 0, classpath = new IClasspathEntry[length+1], 0, length);
452
		classpath[length] = createSourceEntry("P2", "/P1", "~p/X1");
453
		p2.setRawClasspath(classpath, null);
454
		String src =
455
			"package p;\n" +
456
			"public class Z extends X1 {\n" +
457
			"}";
458
		this.problemRequestor = new ProblemRequestor(src);
459
		z = getWorkingCopy(			
460
			"/P2/src/p/Z.java", 
461
			src,
462
			owner,
463
			this.problemRequestor);
464
		assertProblems(
465
			"Unexpected problems value", 
466
			"----------\n" + 
467
			"----------\n" + 
468
			"----------\n" + 
469
			"1. WARNING in /P2/src/p/Z.java (at line 2)\n" + 
470
			"	public class Z extends X1 {\n" + 
471
			"	                       ^^\n" + 
472
			"Discouraged access: The type X1 is not accessible due to restriction on required project P1\n" + 
473
			"----------\n"
474
		);
475
	} finally {
476
		if (x1 != null)
477
			x1.discardWorkingCopy();
478
		if (z != null)
479
			z.discardWorkingCopy();
480
		deleteProjects(new String[] {"P1", "P2"});
481
	}
482
}
483
}
(-)src/org/eclipse/jdt/core/tests/compiler/regression/AnnotationTest.java (-7 / +94 lines)
Lines 12-17 Link Here
12
12
13
import java.io.File;
13
import java.io.File;
14
import java.io.IOException;
14
import java.io.IOException;
15
import java.util.HashMap;
15
import java.util.Hashtable;
16
import java.util.Hashtable;
16
import java.util.Map;
17
import java.util.Map;
17
18
Lines 3704-3711 Link Here
3704
			"----------\n" + 
3705
			"----------\n" + 
3705
			"1. WARNING in Y.java (at line 1)\n" + 
3706
			"1. WARNING in Y.java (at line 1)\n" + 
3706
			"	public class Y extends X {\n" + 
3707
			"	public class Y extends X {\n" + 
3707
			"	             ^\n" + 
3708
			"	                       ^\n" + 
3708
			"The constructor X() is deprecated\n" + 
3709
			"The type X is deprecated\n" + 
3709
			"----------\n" + 
3710
			"----------\n" + 
3710
			"2. WARNING in Y.java (at line 2)\n" + 
3711
			"2. WARNING in Y.java (at line 2)\n" + 
3711
			"	void foo(){ super.foo(); }\n" + 
3712
			"	void foo(){ super.foo(); }\n" + 
Lines 3741-3748 Link Here
3741
			"----------\n" + 
3742
			"----------\n" + 
3742
			"1. WARNING in Y.java (at line 1)\n" + 
3743
			"1. WARNING in Y.java (at line 1)\n" + 
3743
			"	public class Y extends X {\n" + 
3744
			"	public class Y extends X {\n" + 
3744
			"	             ^\n" + 
3745
			"	                       ^\n" + 
3745
			"The constructor X() is deprecated\n" + 
3746
			"The type X is deprecated\n" + 
3746
			"----------\n" + 
3747
			"----------\n" + 
3747
			"2. WARNING in Y.java (at line 2)\n" + 
3748
			"2. WARNING in Y.java (at line 2)\n" + 
3748
			"	void foo(){ super.foo(); }\n" + 
3749
			"	void foo(){ super.foo(); }\n" + 
Lines 3955-3962 Link Here
3955
			"----------\n" + 
3956
			"----------\n" + 
3956
			"1. WARNING in Y.java (at line 1)\n" + 
3957
			"1. WARNING in Y.java (at line 1)\n" + 
3957
			"	public class Y extends X {\n" + 
3958
			"	public class Y extends X {\n" + 
3958
			"	             ^\n" + 
3959
			"	                       ^\n" + 
3959
			"The constructor X() is deprecated\n" + 
3960
			"The type X is deprecated\n" + 
3960
			"----------\n" + 
3961
			"----------\n" + 
3961
			"2. ERROR in Y.java (at line 4)\n" + 
3962
			"2. ERROR in Y.java (at line 4)\n" + 
3962
			"	Zork z;\n" + 
3963
			"	Zork z;\n" + 
Lines 4510-4517 Link Here
4510
			"	Zork z;\n" + 
4511
			"	Zork z;\n" + 
4511
			"	^^^^\n" + 
4512
			"	^^^^\n" + 
4512
			"Zork cannot be resolved to a type\n" + 
4513
			"Zork cannot be resolved to a type\n" + 
4513
			"----------\n");
4514
			"----------\n",
4515
			null,
4516
			true,
4517
			null);
4514
    }        
4518
    }        
4519
public void test142b() {
4520
	Map raiseInvalidJavadocSeverity = 
4521
		new HashMap(2);
4522
	raiseInvalidJavadocSeverity.put(
4523
			CompilerOptions.OPTION_ReportInvalidJavadoc, CompilerOptions.ERROR);
4524
	// admittingly, when these are errors, SuppressWarnings is not enough to 
4525
	// filter them out *but* the deprecation level being WARNING, we get them
4526
	// out anyway
4527
    this.runNegativeTest(
4528
        new String[] {
4529
            "X.java",
4530
			"@SuppressWarnings(\"deprecation\")\n" + 
4531
			"public class X extends p.OldStuff {\n" + 
4532
			"	/**\n" + 
4533
			"	 * @see p.OldStuff#foo()\n" + 
4534
			"	 */\n" + 
4535
			"	@Override\n" + 
4536
			"	public void foo() {\n" + 
4537
			"		super.foo();\n" + 
4538
			"	}\n" + 
4539
			"}\n",
4540
            "p/OldStuff.java",
4541
            "package p;\n" +
4542
            "@Deprecated\n" +
4543
			"public class OldStuff {\n" + 
4544
			"	public void foo() {\n" + 
4545
			"	}	\n" + 
4546
			"  Zork z;\n" +
4547
			"}\n",
4548
        },
4549
		"----------\n" + 
4550
		"1. ERROR in p\\OldStuff.java (at line 6)\n" + 
4551
		"	Zork z;\n" + 
4552
		"	^^^^\n" + 
4553
		"Zork cannot be resolved to a type\n" + 
4554
		"----------\n",
4555
		null,
4556
		true,
4557
		raiseInvalidJavadocSeverity);
4558
}        
4559
public void test142c() {
4560
	Map raiseDeprecationReduceInvalidJavadocSeverity = 
4561
		new HashMap(2);
4562
	raiseDeprecationReduceInvalidJavadocSeverity.put(
4563
			CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.ERROR);
4564
	raiseDeprecationReduceInvalidJavadocSeverity.put(
4565
			CompilerOptions.OPTION_ReportInvalidJavadoc, CompilerOptions.WARNING);
4566
    this.runNegativeTest(
4567
        new String[] {
4568
            "X.java",
4569
			"@SuppressWarnings(\"deprecation\")\n" + 
4570
			"public class X extends p.OldStuff {\n" + 
4571
			"	/**\n" + 
4572
			"	 * @see p.OldStuff#foo()\n" + 
4573
			"	 */\n" + 
4574
			"	@Override\n" + 
4575
			"	public void foo() {\n" + 
4576
			"		super.foo();\n" + 
4577
			"	}\n" + 
4578
			"}\n",
4579
            "p/OldStuff.java",
4580
            "package p;\n" +
4581
            "@Deprecated\n" +
4582
			"public class OldStuff {\n" + 
4583
			"	public void foo() {\n" + 
4584
			"	}	\n" + 
4585
			"}\n",
4586
        },
4587
		"----------\n" + 
4588
		"1. ERROR in X.java (at line 2)\n" + 
4589
		"	public class X extends p.OldStuff {\n" + 
4590
		"	                       ^^^^^^^^^^\n" + 
4591
		"The type OldStuff is deprecated\n" + 
4592
		"----------\n" + 
4593
		"2. ERROR in X.java (at line 8)\n" + 
4594
		"	super.foo();\n" + 
4595
		"	^^^^^^^^^^^\n" + 
4596
		"The method foo() from the type OldStuff is deprecated\n" + 
4597
		"----------\n",
4598
		null,
4599
		true,
4600
		raiseDeprecationReduceInvalidJavadocSeverity);
4601
}        
4515
    public void _test143() {
4602
    public void _test143() {
4516
        this.runNegativeTest(
4603
        this.runNegativeTest(
4517
            new String[] {
4604
            new String[] {
(-)src/org/eclipse/jdt/core/tests/compiler/regression/DeprecatedTest.java (-5 lines)
Lines 197-207 Link Here
197
		"----------\n" + 
197
		"----------\n" + 
198
		"1. WARNING in A.java (at line 1)\n" + 
198
		"1. WARNING in A.java (at line 1)\n" + 
199
		"	public class A extends X.Y {}\n" + 
199
		"	public class A extends X.Y {}\n" + 
200
		"	             ^\n" + 
201
		"The constructor X.Y() is deprecated\n" + 
202
		"----------\n" + 
203
		"2. WARNING in A.java (at line 1)\n" + 
204
		"	public class A extends X.Y {}\n" + 
205
		"	                       ^^^\n" + 
200
		"	                       ^^^\n" + 
206
		"The type X.Y is deprecated\n" + 
201
		"The type X.Y is deprecated\n" + 
207
		"----------\n",// expected output
202
		"----------\n",// expected output

Return to bug 76266