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

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/FieldDeclaration.java (-1 / +4 lines)
Lines 150-156 Link Here
150
			SourceTypeBinding declaringType = classScope.enclosingSourceType();
150
			SourceTypeBinding declaringType = classScope.enclosingSourceType();
151
			checkHidingSuperField: {
151
			checkHidingSuperField: {
152
				if (declaringType.superclass == null) break checkHidingSuperField;
152
				if (declaringType.superclass == null) break checkHidingSuperField;
153
				FieldBinding existingVariable = classScope.findField(declaringType.superclass, this.name, this,  false /*do not resolve hidden field*/);
153
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318171, find field skipping visibility checks
154
				// we do the checks below ourselves, using the appropriate conditions for access check of
155
				// protected members from superclasses.
156
				FieldBinding existingVariable = classScope.findField(declaringType.superclass, this.name, this,  false /*do not resolve hidden field*/, true /* no visibility checks please */);
154
				if (existingVariable == null) break checkHidingSuperField; // keep checking outer scenario
157
				if (existingVariable == null) break checkHidingSuperField; // keep checking outer scenario
155
				if (!existingVariable.isValidBinding())  break checkHidingSuperField; // keep checking outer scenario
158
				if (!existingVariable.isValidBinding())  break checkHidingSuperField; // keep checking outer scenario
156
				if (existingVariable.original() == this.binding) break checkHidingSuperField; // keep checking outer scenario
159
				if (existingVariable.original() == this.binding) break checkHidingSuperField; // keep checking outer scenario
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java (-2 / +15 lines)
Lines 934-939 Link Here
934
		return null;
934
		return null;
935
	}
935
	}
936
936
937
	public FieldBinding findField(TypeBinding receiverType, char[] fieldName, InvocationSite invocationSite, boolean needResolve) {
938
		return findField(receiverType, fieldName, invocationSite, needResolve, false);
939
	}
937
	// Internal use only
940
	// Internal use only
938
	/*	Answer the field binding that corresponds to fieldName.
941
	/*	Answer the field binding that corresponds to fieldName.
939
		Start the lookup at the receiverType.
942
		Start the lookup at the receiverType.
Lines 941-950 Link Here
941
			isSuperAccess(); this is used to determine if the discovered field is visible.
944
			isSuperAccess(); this is used to determine if the discovered field is visible.
942
		Only fields defined by the receiverType or its supertypes are answered;
945
		Only fields defined by the receiverType or its supertypes are answered;
943
		a field of an enclosing type will not be found using this API.
946
		a field of an enclosing type will not be found using this API.
944
947
        If the parameter invisibleFieldsOk is true, visibility checks have not been run on
948
        any returned fields. The caller needs to apply these checks as needed. Otherwise,
945
		If no visible field is discovered, null is answered.
949
		If no visible field is discovered, null is answered.
946
	*/
950
	*/
947
	public FieldBinding findField(TypeBinding receiverType, char[] fieldName, InvocationSite invocationSite, boolean needResolve) {
951
	public FieldBinding findField(TypeBinding receiverType, char[] fieldName, InvocationSite invocationSite, boolean needResolve, boolean invisibleFieldsOk) {
948
952
949
		CompilationUnitScope unitScope = compilationUnitScope();
953
		CompilationUnitScope unitScope = compilationUnitScope();
950
		unitScope.recordTypeReference(receiverType);
954
		unitScope.recordTypeReference(receiverType);
Lines 989-994 Link Here
989
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=316456
993
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=316456
990
		boolean insideTypeAnnotations = this instanceof MethodScope && ((MethodScope) this).insideTypeAnnotation;
994
		boolean insideTypeAnnotations = this instanceof MethodScope && ((MethodScope) this).insideTypeAnnotation;
991
		if (field != null) {
995
		if (field != null) {
996
			if (invisibleFieldsOk) {
997
				return field;
998
			}
992
			if (invocationSite == null || insideTypeAnnotations
999
			if (invocationSite == null || insideTypeAnnotations
993
				? field.canBeSeenBy(getCurrentPackage())
1000
				? field.canBeSeenBy(getCurrentPackage())
994
				: field.canBeSeenBy(currentType, invocationSite, this))
1001
				: field.canBeSeenBy(currentType, invocationSite, this))
Lines 1027-1032 Link Here
1027
			currentType.initializeForStaticImports();
1034
			currentType.initializeForStaticImports();
1028
			currentType = (ReferenceBinding) currentType.capture(this, invocationSite == null ? 0 : invocationSite.sourceEnd());
1035
			currentType = (ReferenceBinding) currentType.capture(this, invocationSite == null ? 0 : invocationSite.sourceEnd());
1029
			if ((field = currentType.getField(fieldName, needResolve)) != null) {
1036
			if ((field = currentType.getField(fieldName, needResolve)) != null) {
1037
				if (invisibleFieldsOk) {
1038
					return field;
1039
				}
1030
				keepLooking = false;
1040
				keepLooking = false;
1031
				if (field.canBeSeenBy(receiverType, invocationSite, this)) {
1041
				if (field.canBeSeenBy(receiverType, invocationSite, this)) {
1032
					if (visibleField == null)
1042
					if (visibleField == null)
Lines 1048-1053 Link Here
1048
				unitScope.recordTypeReference(anInterface);
1058
				unitScope.recordTypeReference(anInterface);
1049
				// no need to capture rcv interface, since member field is going to be static anyway
1059
				// no need to capture rcv interface, since member field is going to be static anyway
1050
				if ((field = anInterface.getField(fieldName, true /*resolve*/)) != null) {
1060
				if ((field = anInterface.getField(fieldName, true /*resolve*/)) != null) {
1061
					if (invisibleFieldsOk) {
1062
						return field;
1063
					}
1051
					if (visibleField == null) {
1064
					if (visibleField == null) {
1052
						visibleField = field;
1065
						visibleField = field;
1053
					} else {
1066
					} else {
(-)src/org/eclipse/jdt/core/tests/compiler/regression/Compliance_1_3.java (-10 / +16 lines)
Lines 1028-1043 Link Here
1028
			"	String bar = \"X2.bar\";	\n" +
1028
			"	String bar = \"X2.bar\";	\n" +
1029
			"}	\n",
1029
			"}	\n",
1030
		},
1030
		},
1031
		"----------\n" +
1031
		"----------\n" + 
1032
		"1. ERROR in p\\X.java (at line 4)\n" +
1032
		"1. ERROR in p\\X.java (at line 4)\n" + 
1033
		"	System.out.println(new q.X2().foo);	\n" +
1033
		"	System.out.println(new q.X2().foo);	\n" + 
1034
		"	                              ^^^\n" +
1034
		"	                              ^^^\n" + 
1035
		"The field X2.foo is not visible\n" +
1035
		"The field X2.foo is not visible\n" + 
1036
		"----------\n" +
1036
		"----------\n" + 
1037
		"2. ERROR in p\\X.java (at line 5)\n" +
1037
		"2. ERROR in p\\X.java (at line 5)\n" + 
1038
		"	System.out.println(new q.X2().bar);	\n" +
1038
		"	System.out.println(new q.X2().bar);	\n" + 
1039
		"	                              ^^^\n" +
1039
		"	                              ^^^\n" + 
1040
		"The field X2.bar is not visible\n" +
1040
		"The field X2.bar is not visible\n" + 
1041
		"----------\n" + 
1042
		"----------\n" + 
1043
		"1. WARNING in q\\X2.java (at line 3)\n" + 
1044
		"	protected String foo = \"X2.foo\";	\n" + 
1045
		"	                 ^^^\n" + 
1046
		"The field X2.foo is hiding a field from type X1\n" + 
1041
		"----------\n");
1047
		"----------\n");
1042
}
1048
}
1043
/*
1049
/*
(-)src/org/eclipse/jdt/core/tests/compiler/regression/Compliance_1_4.java (-10 / +16 lines)
Lines 1015-1030 Link Here
1015
			"	String bar = \"X2.bar\";	\n" +
1015
			"	String bar = \"X2.bar\";	\n" +
1016
			"}	\n",
1016
			"}	\n",
1017
		},
1017
		},
1018
		"----------\n" +
1018
		"----------\n" + 
1019
		"1. ERROR in p\\X.java (at line 4)\n" +
1019
		"1. ERROR in p\\X.java (at line 4)\n" + 
1020
		"	System.out.println(new q.X2().foo);	\n" +
1020
		"	System.out.println(new q.X2().foo);	\n" + 
1021
		"	                              ^^^\n" +
1021
		"	                              ^^^\n" + 
1022
		"The field X2.foo is not visible\n" +
1022
		"The field X2.foo is not visible\n" + 
1023
		"----------\n" +
1023
		"----------\n" + 
1024
		"2. ERROR in p\\X.java (at line 5)\n" +
1024
		"2. ERROR in p\\X.java (at line 5)\n" + 
1025
		"	System.out.println(new q.X2().bar);	\n" +
1025
		"	System.out.println(new q.X2().bar);	\n" + 
1026
		"	                              ^^^\n" +
1026
		"	                              ^^^\n" + 
1027
		"The field X2.bar is not visible\n" +
1027
		"The field X2.bar is not visible\n" + 
1028
		"----------\n" + 
1029
		"----------\n" + 
1030
		"1. WARNING in q\\X2.java (at line 3)\n" + 
1031
		"	protected String foo = \"X2.foo\";	\n" + 
1032
		"	                 ^^^\n" + 
1033
		"The field X2.foo is hiding a field from type X1\n" + 
1028
		"----------\n");
1034
		"----------\n");
1029
}
1035
}
1030
1036
(-)src/org/eclipse/jdt/core/tests/compiler/regression/Compliance_1_5.java (-10 / +16 lines)
Lines 1032-1047 Link Here
1032
			"	String bar = \"X2.bar\";	\n" +
1032
			"	String bar = \"X2.bar\";	\n" +
1033
			"}	\n",
1033
			"}	\n",
1034
		},
1034
		},
1035
		"----------\n" +
1035
		"----------\n" + 
1036
		"1. ERROR in p\\X.java (at line 4)\n" +
1036
		"1. ERROR in p\\X.java (at line 4)\n" + 
1037
		"	System.out.println(new q.X2().foo);	\n" +
1037
		"	System.out.println(new q.X2().foo);	\n" + 
1038
		"	                              ^^^\n" +
1038
		"	                              ^^^\n" + 
1039
		"The field X2.foo is not visible\n" +
1039
		"The field X2.foo is not visible\n" + 
1040
		"----------\n" +
1040
		"----------\n" + 
1041
		"2. ERROR in p\\X.java (at line 5)\n" +
1041
		"2. ERROR in p\\X.java (at line 5)\n" + 
1042
		"	System.out.println(new q.X2().bar);	\n" +
1042
		"	System.out.println(new q.X2().bar);	\n" + 
1043
		"	                              ^^^\n" +
1043
		"	                              ^^^\n" + 
1044
		"The field X2.bar is not visible\n" +
1044
		"The field X2.bar is not visible\n" + 
1045
		"----------\n" + 
1046
		"----------\n" + 
1047
		"1. WARNING in q\\X2.java (at line 3)\n" + 
1048
		"	protected String foo = \"X2.foo\";	\n" + 
1049
		"	                 ^^^\n" + 
1050
		"The field X2.foo is hiding a field from type X1\n" + 
1045
		"----------\n");
1051
		"----------\n");
1046
}
1052
}
1047
1053
(-)src/org/eclipse/jdt/core/tests/compiler/regression/FieldAccessTest.java (+89 lines)
Lines 679-684 Link Here
679
		"OLD_FIELD cannot be resolved or is not a field\n" + 
679
		"OLD_FIELD cannot be resolved or is not a field\n" + 
680
		"----------\n");
680
		"----------\n");
681
}
681
}
682
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318171
683
public void test023() {
684
	this.runNegativeTest(
685
		new String[] {
686
			"p1/A.java",
687
			"package p1;\n" +
688
			"public abstract class A {\n" +
689
			"    protected int field;\n" +
690
			"}\n",
691
			"p2/B.java",
692
			"package p2;\n" +
693
			"import p1.A;\n" +
694
			"public abstract class B extends A {\n" +
695
			"    protected int field;\n" +
696
			"}\n"
697
		},
698
		"----------\n" + 
699
		"1. WARNING in p2\\B.java (at line 4)\n" + 
700
		"	protected int field;\n" + 
701
		"	              ^^^^^\n" + 
702
		"The field B.field is hiding a field from type A\n" + 
703
		"----------\n");
704
}
705
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318171
706
public void test024() {
707
	this.runNegativeTest(
708
		new String[] {
709
			"p1/A.java",
710
			"package p1;\n" +
711
			"public abstract class A extends Super {\n" +
712
			"}\n",
713
			"p1/Super.java",
714
			"package p1;\n" +
715
			"public abstract class Super extends SuperSuper {\n" +
716
			"}\n",
717
			"p1/SuperSuper.java",
718
			"package p1;\n" +
719
			"public abstract class SuperSuper {\n" +
720
			"    protected int field;\n" +
721
			"}\n",
722
			"p2/B.java",
723
			"package p2;\n" +
724
			"import p1.A;\n" +
725
			"public abstract class B extends A {\n" +
726
			"    protected int field;\n" +
727
			"}\n"
728
		},
729
		"----------\n" + 
730
		"1. WARNING in p2\\B.java (at line 4)\n" + 
731
		"	protected int field;\n" + 
732
		"	              ^^^^^\n" + 
733
		"The field B.field is hiding a field from type SuperSuper\n" + 
734
		"----------\n");
735
}
736
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318171
737
public void test025() {
738
	this.runNegativeTest(
739
		new String[] {
740
			"p1/A.java",
741
			"package p1;\n" +
742
			"public abstract class A extends Super {\n" +
743
			"}\n",
744
			"p1/Super.java",
745
			"package p1;\n" +
746
			"public abstract class Super extends SuperSuper {\n" +
747
			"}\n",
748
			"p1/SuperSuper.java",
749
			"package p1;\n" +
750
			"public abstract class SuperSuper implements Interface{\n" +
751
			"}\n",
752
			"p1/Interface.java",
753
			"package p1;\n" +
754
			"public interface Interface{\n" +
755
			"    int field = 123;\n" +
756
			"}\n",
757
			"p2/B.java",
758
			"package p2;\n" +
759
			"import p1.A;\n" +
760
			"public abstract class B extends A {\n" +
761
			"    protected int field;\n" +
762
			"}\n"
763
		},
764
		"----------\n" + 
765
		"1. WARNING in p2\\B.java (at line 4)\n" + 
766
		"	protected int field;\n" + 
767
		"	              ^^^^^\n" + 
768
		"The field B.field is hiding a field from type Interface\n" + 
769
		"----------\n");
770
}
682
public static Class testClass() {
771
public static Class testClass() {
683
	return FieldAccessTest.class;
772
	return FieldAccessTest.class;
684
}
773
}

Return to bug 318171