item 39 JungHyunLyoo - JAVA-JIKIMI/EFFECTIVE-JAVA3 GitHub Wiki
λ©μλλ ν΄λμ€ λ±μ μ΄λ¦μ μ§μ λ, νΉμ ν κ·μΉμ λ£μ΄ νλ μμν¬μμ μ¬μ©λ μ μκ² νλ ν¨ν΄
- μ€ν testMethod(o), tsetMethod(x)
- μ¬λ°λ₯Έ νλ‘κ·Έλ¨ μμμμλ§ μ¬μ©λλ¦¬λΌ λ³΄μ¦ν λ°©λ²μ΄ μμ (λ©μλ μ΄λ¦μ΄ μλ ν΄λμ€ μ΄λ¦μ κ·μΉμ μ μ©ν¨)
- νλ‘κ·Έλ¨ μμλ₯Ό λ§€κ°λ³μλ‘ μ λ¬ν λ§λ ν λ°©λ²μ΄ μμ
μ λν μ΄μ μ μ΄ λͺ¨λ λ¬Έμ λ₯Ό ν΄κ²°ν΄μ€λ€!
package effectivejava.chapter6.item39.markerannotation;
import java.lang.annotation.*;
import java.lang.annotation.*;
/**
* ν
μ€νΈ λ©μλμμ μ μΈνλ μ λν
μ΄μ
μ΄λ€.
* λ§€κ°λ³μ μλ μ μ λ©μλ μ μ©μ΄λ€.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) -> λ©ν μ λν
μ΄μ
public @interface Test { -> λ§μ»€ μ λν
μ΄μ
}ackage effectivejava.chapter6.item39.markerannotation;
public class Sample {
@Test
public static void m1() { } // μ±κ³΅ν΄μΌ νλ€.
public static void m2() { }
@Test public static void m3() { // μ€ν¨ν΄μΌ νλ€.
throw new RuntimeException("μ€ν¨");
}
public static void m4() { } // ν
μ€νΈκ° μλλ€.
@Test public void m5() { } // μλͺ» μ¬μ©ν μ: μ μ λ©μλκ° μλλ€.
public static void m6() { }
@Test public static void m7() { // μ€ν¨ν΄μΌ νλ€.
throw new RuntimeException("μ€ν¨");
}
public static void m8() { }
}package effectivejava.chapter6.item39.markerannotation;
import java.lang.reflect.*;
public class RunTests {
public static void main(String[] args) throws Exception {
int tests = 0;
int passed = 0;
Class<?> testClass = Class.forName(args[0]);
for (Method m : testClass.getDeclaredMethods()) {
if (m.isAnnotationPresent(Test.class)) {
tests++;
try {
m.invoke(null);
passed++;
} catch (InvocationTargetException wrappedExc) {
Throwable exc = wrappedExc.getCause();
System.out.println(m + " μ€ν¨: " + exc);
} catch (Exception exc) {
System.out.println("μλͺ» μ¬μ©ν @Test: " + m);
}
}
}
System.out.printf("μ±κ³΅: %d, μ€ν¨: %d%n",
passed, tests - passed);
}
}package effectivejava.chapter6.item39.annotationwithparameter;
import java.lang.annotation.*;
/**
* λͺ
μν μμΈλ₯Ό λμ ΈμΌλ§ μ±κ³΅νλ ν
μ€νΈ λ©μλμ© μ λν
μ΄μ
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExceptionTest {
Class<? extends Throwable> value();
}package effectivejava.chapter6.item39.annotationwithparameter;
import effectivejava.chapter6.item39.annotationwithparameter.ExceptionTest;
import java.util.*;
public class Sample2 {
@ExceptionTest(ArithmeticException.class)
public static void m1() { // μ±κ³΅ν΄μΌ νλ€.
int i = 0;
i = i / i;
}
@ExceptionTest(ArithmeticException.class)
public static void m2() { // μ€ν¨ν΄μΌ νλ€. (λ€λ₯Έ μμΈ λ°μ)
int[] a = new int[0];
int i = a[1];
}
@ExceptionTest(ArithmeticException.class)
public static void m3() { } // μ€ν¨ν΄μΌ νλ€. (μμΈκ° λ°μνμ§ μμ)
}package effectivejava.chapter6.item39.annotationwithparameter;
import effectivejava.chapter6.item39.markerannotation.Test;
import java.lang.reflect.*;
public class RunTests {
public static void main(String[] args) throws Exception {
int tests = 0;
int passed = 0;
Class<?> testClass = Class.forName(args[0]);
for (Method m : testClass.getDeclaredMethods()) {
if (m.isAnnotationPresent(Test.class)) {
tests++;
try {
m.invoke(null);
passed++;
} catch (InvocationTargetException wrappedExc) {
Throwable exc = wrappedExc.getCause();
System.out.println(m + " μ€ν¨: " + exc);
} catch (Exception exc) {
System.out.println("μλͺ» μ¬μ©ν @Test: " + m);
}
}
if (m.isAnnotationPresent(ExceptionTest.class)) {
tests++;
try {
m.invoke(null);
System.out.printf("ν
μ€νΈ %s μ€ν¨: μμΈλ₯Ό λμ§μ§ μμ%n", m);
} catch (InvocationTargetException wrappedEx) {
Throwable exc = wrappedEx.getCause();
Class<? extends Throwable> excType =
m.getAnnotation(ExceptionTest.class).value();
if (excType.isInstance(exc)) {
passed++;
} else {
System.out.printf(
"ν
μ€νΈ %s μ€ν¨: κΈ°λν μμΈ %s, λ°μν μμΈ %s%n",
m, excType.getName(), exc);
}
} catch (Exception exc) {
System.out.println("μλͺ» μ¬μ©ν @ExceptionTest: " + m);
}
}
}
System.out.printf("μ±κ³΅: %d, μ€ν¨: %d%n",
passed, tests - passed);
}
}package effectivejava.chapter6.item39.annotationwitharrayparameter;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExceptionTest {
Class<? extends Exception>[] value();
}package effectivejava.chapter6.item39.annotationwitharrayparameter;
import java.util.*;
public class Sample3 {
// μ΄ λ³νμ μμ νλμ§λ¦¬ λ§€κ°λ³μλ₯Ό λ°λ μ λν
μ΄μ
λ μ²λ¦¬ν μ μλ€. (241μͺ½ Sample2μ κ°μ)
@ExceptionTest(ArithmeticException.class)
public static void m1() { // μ±κ³΅ν΄μΌ νλ€.
int i = 0;
i = i / i;
}
@ExceptionTest(ArithmeticException.class)
public static void m2() { // μ€ν¨ν΄μΌ νλ€. (λ€λ₯Έ μμΈ λ°μ)
int[] a = new int[0];
int i = a[1];
}
@ExceptionTest(ArithmeticException.class)
public static void m3() { } // μ€ν¨ν΄μΌ νλ€. (μμΈκ° λ°μνμ§ μμ)
// μ½λ 39-7 λ°°μ΄ λ§€κ°λ³μλ₯Ό λ°λ μ λν
μ΄μ
μ μ¬μ©νλ μ½λ (242-243μͺ½)
@ExceptionTest({ IndexOutOfBoundsException.class,
NullPointerException.class })
public static void doublyBad() { // μ±κ³΅ν΄μΌ νλ€.
List<String> list = new ArrayList<>();
// μλ° API λͺ
μΈμ λ°λ₯΄λ©΄ λ€μ λ©μλλ IndexOutOfBoundsExceptionμ΄λ
// NullPointerExceptionμ λμ§ μ μλ€.
list.addAll(5, null);
}
}package effectivejava.chapter6.item39.annotationwitharrayparameter;
import effectivejava.chapter6.item39.markerannotation.Test;
import java.lang.reflect.*;
public class RunTests {
public static void main(String[] args) throws Exception {
int tests = 0;
int passed = 0;
Class<?> testClass = Class.forName(args[0]);
for (Method m : testClass.getDeclaredMethods()) {
if (m.isAnnotationPresent(Test.class)) {
tests++;
try {
m.invoke(null);
passed++;
} catch (InvocationTargetException wrappedExc) {
Throwable exc = wrappedExc.getCause();
System.out.println(m + " μ€ν¨: " + exc);
} catch (Exception exc) {
System.out.println("μλͺ» μ¬μ©ν @Test: " + m);
}
}
if (m.isAnnotationPresent(ExceptionTest.class)) {
tests++;
try {
m.invoke(null);
System.out.printf("ν
μ€νΈ %s μ€ν¨: μμΈλ₯Ό λμ§μ§ μμ%n", m);
} catch (Throwable wrappedExc) {
Throwable exc = wrappedExc.getCause();
int oldPassed = passed;
Class<? extends Throwable>[] excTypes =
m.getAnnotation(ExceptionTest.class).value();
for (Class<? extends Throwable> excType : excTypes) {
if (excType.isInstance(exc)) {
passed++;
break;
}
}
if (passed == oldPassed)
System.out.printf("ν
μ€νΈ %s μ€ν¨: %s %n", m, exc);
}
}
}
System.out.printf("μ±κ³΅: %d, μ€ν¨: %d%n",
passed, tests - passed);
}
}@Repeatable λ©ν μ λν μ΄μ ?