001: ////////////////////////////////////////////////////////////////////////////////
002: // Test case file for checkstyle.
003: // Created: 2001
004: ////////////////////////////////////////////////////////////////////////////////
005: package com.puppycrawl.tools.checkstyle;
006:
007: import java.io.*; // star import for instantiation tests
008: import java.awt.Dimension; // explicit import for instantiation tests
009: import java.awt.Color;
010:
011: /**
012: * Test case for detecting simple semantic errors.
013: * @author Lars Kühne
014: **/
015: class InputSemantic {
016: /* Boolean instantiation in a static initializer */
017: static {
018: Boolean x = new Boolean(true);
019: }
020:
021: /* Boolean instantiation in a non-static initializer */
022: {
023: Boolean x = new Boolean(true);
024: Boolean[] y = new Boolean[] { Boolean.TRUE, Boolean.FALSE };
025: }
026:
027: /** fully qualified Boolean instantiation in a method. **/
028: Boolean getBoolean() {
029: return new java.lang.Boolean(true);
030: }
031:
032: void otherInstantiations() {
033: // instantiation of classes in the same package
034: Object o1 = new InputBraces();
035: Object o2 = new InputModifier();
036: // classes in another package with .* import
037: ByteArrayOutputStream s = new ByteArrayOutputStream();
038: File f = new File("/tmp");
039: // classes in another package with explicit import
040: Dimension dim = new Dimension();
041: Color col = new Color(0, 0, 0);
042: }
043:
044: void exHandlerTest() {
045: try {
046: ; // do stuff and don't handle exceptions in some cases
047: } catch (IllegalStateException emptyCatchIsAlwaysAnError) {
048: } catch (NullPointerException ex) {
049: // can never happen, but only commentig this is currently an error
050: // Possible future enhancement: allowEmptyCatch="commented"
051: } catch (ArrayIndexOutOfBoundsException ex) {
052: ;
053: // can never happen, semicolon makes checkstyle happy
054: // this is a workaround for above problem
055: } catch (NegativeArraySizeException ex) {
056: {
057: }
058: // can never happen, empty compound statement is another workaround
059: } catch (UnsupportedOperationException handledException) {
060: System.out.println(handledException.getMessage());
061: } catch (SecurityException ex) { /* hello */
062: } catch (StringIndexOutOfBoundsException ex) {
063: } catch (IllegalArgumentException ex) {
064: }
065:
066: try {
067: } finally {
068: }
069: try {
070: // something
071: } finally {
072: // something
073: }
074: try {
075: ; // something
076: } finally {
077: ; // statement
078: }
079: }
080:
081: /** test **/
082: private static final long IGNORE = 666l + 666L;
083:
084: public class EqualsVsHashCode1 {
085: public boolean equals(int a) // wrong arg type, don't flag
086: {
087: return a == 1;
088: }
089: }
090:
091: public class EqualsVsHashCode2 {
092: public boolean equals(String a) // flag
093: {
094: return true;
095: }
096: }
097:
098: public class EqualsVsHashCode3 {
099: public boolean equals(Object a) // don't flag
100: {
101: return true;
102: }
103:
104: public int hashCode() {
105: return 0;
106: }
107: }
108:
109: public class EqualsVsHashCode4 {
110: // in anon inner class
111: ByteArrayOutputStream bos1 = new ByteArrayOutputStream() {
112: public boolean equals(Object a) // don't flag
113: {
114: return true;
115: }
116:
117: public int hashCode() {
118: return 0;
119: }
120: };
121:
122: ByteArrayOutputStream bos2 = new ByteArrayOutputStream() {
123: public boolean equals(Object a) // flag
124: {
125: return true;
126: }
127: };
128: }
129:
130: public void triggerEmptyBlockWithoutBlock() {
131: // an if statement without a block to increase test coverage
132: if (true)
133: return;
134: }
135:
136: // empty instance initializer
137: {
138: }
139:
140: public class EqualsVsHashCode5 {
141: public <A> boolean equals(int a) // wrong arg type, don't flag even with generics
142: {
143: return a == 1;
144: }
145: }
146:
147: public class EqualsVsHashCode6 {
148: public <A> boolean equals(Comparable<A> a) // flag, weven with generics
149: {
150: return true;
151: }
152: }
153:
154: }
|