001: package com.puppycrawl.tools.checkstyle;
002:
003: ////////////////////////////////////////////////////////////////////////////////
004: // Test case file for checkstyle.
005: // Created: 2002
006: ////////////////////////////////////////////////////////////////////////////////
007:
008: /**
009: * Test case for hidden fields
010: * @author Rick Giles
011: **/
012: class InputHiddenField {
013: private int hidden = 0;
014:
015: public InputHiddenField() {
016: int hidden = 0; //shadows field
017: }
018:
019: public InputHiddenField(int hidden) //parameter shadows field
020: {
021: }
022:
023: public void shadow() {
024: int hidden = 0; //shadows field
025: }
026:
027: public void shadowFor() {
028: for (int hidden = 0; hidden < 1; hidden++) { //shadows field
029: }
030: }
031:
032: public void shadowParam(int hidden) //parameter shadows field
033: {
034: }
035:
036: public class Inner {
037: private int innerHidden = 0;
038:
039: public Inner() {
040: int innerHidden = 0; //shadows field
041: }
042:
043: public Inner(int innerHidden) //shadows field
044: {
045: }
046:
047: private void innerShadow() {
048: int innerHidden = 0; //shadows inner field
049: int hidden = 0; //shadows outer field
050: }
051:
052: private void innerShadowFor() {
053: for (int innerHidden = 0; innerHidden < 1; innerHidden++) {
054: }
055: //shadows outer field
056: for (int hidden = 0; hidden < 1; hidden++) {
057: }
058: }
059:
060: private void shadowParam(int innerHidden, //parameter shadows inner field
061: int hidden //parameter shadows outer field
062: ) {
063: }
064:
065: {
066: int innerHidden = 0;//shadows inner field
067: int hidden = 0; //shadows outer field
068: }
069: }
070:
071: {
072: int hidden = 0;//shadows field
073: }
074: }
075:
076: interface NothingHidden {
077: public static int notHidden = 0;
078:
079: // not an error
080: public void noShadow(int notHidden);
081: }
082:
083: /** tests ignoring the parameter of a property setter method */
084: class PropertySetter {
085: private int prop;
086:
087: /** setter */
088: public void setProp(int prop) {
089: this .prop = prop;
090: }
091:
092: /** error - incorrect method name */
093: public void setprop(int prop) {
094: this .prop = prop;
095: }
096:
097: /** error - more than one parameter */
098: public void setProp(int prop, int extra) {
099: this .prop = prop;
100: }
101: }
102:
103: /** tests a non-void method */
104: class PropertySetter2 {
105: private int prop;
106:
107: /** error - not a void method */
108: public int setProp(int prop) {
109: this .prop = prop;
110: return 0;
111: }
112: }
113:
114: /** tests for static fields */
115: class StaticFields {
116: private static int hidden;
117:
118: public static void staticMethod() {
119: int hidden;
120: }
121:
122: public void method() {
123: int hidden;
124: }
125:
126: static {
127: int hidden;
128: }
129:
130: {
131: int hidden;
132: }
133: }
134:
135: /** tests static methods & initializers */
136: class StaticMethods {
137: private int notHidden;
138:
139: public static void method() {
140: // local variables of static methods don't hide instance fields.
141: int notHidden;
142: }
143:
144: static {
145: // local variables of static initializers don't hide instance fields.
146: int notHidden;
147: }
148:
149: private int x;
150: private static int y;
151:
152: static class Inner {
153: void useX(int x) {
154: x++;
155: }
156:
157: void useY(int y) {
158: y++;
159: }
160: }
161: }
162:
163: enum HiddenEnum {
164: A(129), B(283), C(1212) {
165: /**
166: * Should not be flagged as error as we don't check
167: * hidden class level fields
168: */
169: int hidden;
170:
171: public void doSomething() {
172: //Should be flagged as hiding enum constant member
173: int hidden = 0;
174: }
175: };
176:
177: int hidden;
178: static int hiddenStatic;
179:
180: /**
181: * ctor parameter hides member
182: */
183: HiddenEnum(int hidden) {
184: }
185:
186: public void doSomething() {
187: //Should be flagged as hiding static member
188: int hidden = 0;
189: }
190:
191: public static void doSomethingStatic() {
192: //Should be flagged as hiding static member
193: int hiddenStatic = 0;
194: }
195: }
196:
197: // we should ignore this if user wants (ignoreAbstractMethods is true)
198: abstract class InputHiddenFieldBug1084512 {
199: String x;
200:
201: public abstract void methodA(String x);
202: }
|