Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 340482
Collapse All | Expand All

(-)src/org/eclipse/rwt/internal/util/ClassUtil.java (-5 / +23 lines)
Lines 11-16 Link Here
11
package org.eclipse.rwt.internal.util;
11
package org.eclipse.rwt.internal.util;
12
12
13
import java.lang.reflect.Constructor;
13
import java.lang.reflect.Constructor;
14
import java.lang.reflect.InvocationTargetException;
14
15
15
16
16
public final class ClassUtil {
17
public final class ClassUtil {
Lines 33-44 Link Here
33
34
34
  public static Object newInstance( Class type, Class[] paramTypes, Object[] paramValues ) {
35
  public static Object newInstance( Class type, Class[] paramTypes, Object[] paramValues ) {
35
    ParamCheck.notNull( type, "type" );
36
    ParamCheck.notNull( type, "type" );
37
    Object result = null;
36
    try {
38
    try {
37
      return createInstance( type, paramTypes, paramValues );
39
      result = createInstance( type, paramTypes, paramValues );
38
    } catch( Exception e ) {
40
    } catch( RuntimeException rte ) {
39
      String msg = "Failed to create instance of type: " + type.getName();
41
      throw rte;
40
      throw new ClassInstantiationException( msg, e );
42
    } catch( InvocationTargetException ite ) {
43
      rethrowRuntimeExceptions( ite );
44
      throwInstantiationException( type, ite.getCause() );
45
    } catch( Exception exception ) {
46
      throwInstantiationException( type, exception );
41
    }
47
    }
48
    return result;
42
  }
49
  }
43
50
44
  private static Object createInstance( Class type, Class[] paramTypes, Object[] paramValues ) 
51
  private static Object createInstance( Class type, Class[] paramTypes, Object[] paramValues ) 
Lines 51-57 Link Here
51
    return constructor.newInstance( paramValues );
58
    return constructor.newInstance( paramValues );
52
  }
59
  }
53
60
61
  private static void rethrowRuntimeExceptions( InvocationTargetException ite ) {
62
    if( ite.getCause() instanceof RuntimeException ) {
63
      throw ( RuntimeException )ite.getCause();
64
    }
65
  }
66
67
  private static void throwInstantiationException( Class type, Throwable cause ) {
68
    String msg = "Failed to create instance of type: " + type.getName();
69
    throw new ClassInstantiationException( msg, cause );
70
  }
71
54
  private ClassUtil() {
72
  private ClassUtil() {
55
    // prevent instantiation
73
    // prevent instantiation
56
  }
74
  }
57
}
75
}
(-)src/org/eclipse/rwt/internal/util/ClassUtil_Test.java (-7 / +22 lines)
Lines 10-16 Link Here
10
 ******************************************************************************/
10
 ******************************************************************************/
11
package org.eclipse.rwt.internal.util;
11
package org.eclipse.rwt.internal.util;
12
12
13
import java.lang.reflect.InvocationTargetException;
13
import java.io.IOException;
14
14
15
import junit.framework.TestCase;
15
import junit.framework.TestCase;
16
16
Lines 31-36 Link Here
31
    String stringParam;
31
    String stringParam;
32
    Long longParam;
32
    Long longParam;
33
    public PublicClass() {
33
    public PublicClass() {
34
      this( null ); // avoid unused private constructor marker 
34
    }
35
    }
35
    private PublicClass( Object objectParam ) {
36
    private PublicClass( Object objectParam ) {
36
      this.objectParam = objectParam;
37
      this.objectParam = objectParam;
Lines 46-56 Link Here
46
    }
47
    }
47
  }
48
  }
48
  
49
  
49
  static class ClassWithExceptionInConstructor {
50
  static class ClassWithRuntimeExceptionInConstructor {
50
    ClassWithExceptionInConstructor() {
51
    ClassWithRuntimeExceptionInConstructor() {
51
      throw new ConstructorException();
52
      throw new ConstructorException();
52
    }
53
    }
53
  }
54
  }
55
56
  static class ClassWithCheckedExceptionInConstructor {
57
    ClassWithCheckedExceptionInConstructor() throws IOException {
58
      throw new IOException();
59
    }
60
  }
54
  
61
  
55
  public void testNewInstanceWithNullClass() {
62
  public void testNewInstanceWithNullClass() {
56
    try {
63
    try {
Lines 86-97 Link Here
86
    assertEquals( instance.getClass(), PublicClass.class );
93
    assertEquals( instance.getClass(), PublicClass.class );
87
  }
94
  }
88
  
95
  
89
  public void testNewInstanceWithEceptionInConstructor() {
96
  public void testNewInstanceWithRuntimeExceptionInConstructor() {
90
    try {
97
    try {
91
      ClassUtil.newInstance( ClassWithExceptionInConstructor.class );
98
      ClassUtil.newInstance( ClassWithRuntimeExceptionInConstructor.class );
92
      fail();
99
      fail();
93
    } catch( ClassInstantiationException e ) {
100
    } catch( ConstructorException expected ) {
94
      assertEquals( e.getCause().getClass(), InvocationTargetException.class );
101
    }
102
  }
103
104
  public void testNewInstanceWithCheckedExceptionInConstructor() {
105
    try {
106
      ClassUtil.newInstance( ClassWithCheckedExceptionInConstructor.class );
107
      fail();
108
    } catch( ClassInstantiationException expected ) {
109
      assertEquals( expected.getCause().getClass(), IOException.class );
95
    }
110
    }
96
  }
111
  }
97
  
112
  

Return to bug 340482