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 339827
Collapse All | Expand All

(-)src/org/eclipse/rwt/RWT.java (-13 / +4 lines)
Lines 24-29 Link Here
24
import org.eclipse.rwt.internal.lifecycle.RWTLifeCycle;
24
import org.eclipse.rwt.internal.lifecycle.RWTLifeCycle;
25
import org.eclipse.rwt.internal.resources.ResourceManager;
25
import org.eclipse.rwt.internal.resources.ResourceManager;
26
import org.eclipse.rwt.internal.service.*;
26
import org.eclipse.rwt.internal.service.*;
27
import org.eclipse.rwt.internal.util.ClassUtil;
27
import org.eclipse.rwt.internal.widgets.BrowserHistory;
28
import org.eclipse.rwt.internal.widgets.BrowserHistory;
28
import org.eclipse.rwt.lifecycle.ILifeCycle;
29
import org.eclipse.rwt.lifecycle.ILifeCycle;
29
import org.eclipse.rwt.resources.IResourceManager;
30
import org.eclipse.rwt.resources.IResourceManager;
Lines 133-145 Link Here
133
      synchronized( map ) {
134
      synchronized( map ) {
134
        result = map.get( bundle );
135
        result = map.get( bundle );
135
        if( result == null ) {
136
        if( result == null ) {
136
          try {
137
          result = ClassUtil.newInstance( clazz );
137
            Constructor constructor = clazz.getDeclaredConstructor( null );
138
            constructor.setAccessible( true );
139
            result = constructor.newInstance( null );
140
          } catch( final Exception ex ) {
141
            throw new IllegalStateException( ex.getMessage() );
142
          }
143
          Field[] fields = clazz.getDeclaredFields();
138
          Field[] fields = clazz.getDeclaredFields();
144
          for( int i = 0; i < fields.length; i++ ) {
139
          for( int i = 0; i < fields.length; i++ ) {
145
            String fieldName = fields[ i ].getName();
140
            String fieldName = fields[ i ].getName();
Lines 161-172 Link Here
161
                }
156
                }
162
              }
157
              }
163
            } catch( final Exception ex ) {
158
            } catch( final Exception ex ) {
164
              String msg
159
              String qualifiedName = clazz.getName() + "#" + fieldName;
165
                = "Failed to load localized message for: " 
160
              ServletLog.log( "Failed to load localized message for: " + qualifiedName, ex );
166
                + clazz.getName() 
167
                + "#" 
168
                + fieldName;
169
              ServletLog.log( msg, ex );
170
            }
161
            }
171
          }
162
          }
172
          map.put( bundle, result );
163
          map.put( bundle, result );
(-)src/org/eclipse/rwt/SessionSingletonBase.java (-47 / +2 lines)
Lines 10-22 Link Here
10
 ******************************************************************************/
10
 ******************************************************************************/
11
package org.eclipse.rwt;
11
package org.eclipse.rwt;
12
12
13
import java.lang.reflect.Constructor;
14
import java.lang.reflect.InvocationTargetException;
15
import java.util.Hashtable;
13
import java.util.Hashtable;
16
import java.util.Map;
14
import java.util.Map;
17
15
18
import org.eclipse.rwt.internal.service.ContextProvider;
16
import org.eclipse.rwt.internal.service.ContextProvider;
19
import org.eclipse.rwt.internal.service.IServiceStateInfo;
17
import org.eclipse.rwt.internal.service.IServiceStateInfo;
18
import org.eclipse.rwt.internal.util.ClassUtil;
20
import org.eclipse.rwt.service.ISessionStore;
19
import org.eclipse.rwt.service.ISessionStore;
21
20
22
21
Lines 62-69 Link Here
62
  private final static String PREFIX = "com_w4t_session_singleton_";
61
  private final static String PREFIX = "com_w4t_session_singleton_";
63
  private static final String LOCK_POSTFIX = "#typeLock";
62
  private static final String LOCK_POSTFIX = "#typeLock";
64
  
63
  
65
  private final static Class[] EMPTY_PARAMS = new Class[ 0 ];
66
  
67
  private final static Map instanceKeyMap = new Hashtable();
64
  private final static Map instanceKeyMap = new Hashtable();
68
  private final static Map lockKeyMap = new Hashtable();
65
  private final static Map lockKeyMap = new Hashtable();
69
66
Lines 145-193 Link Here
145
  private static Object getInstanceInternal( final Class type ) {
142
  private static Object getInstanceInternal( final Class type ) {
146
    Object result = getAttribute( getInstanceKey( type ) ); 
143
    Object result = getAttribute( getInstanceKey( type ) ); 
147
    if( result == null ) {
144
    if( result == null ) {
148
      try {
145
      result = ClassUtil.newInstance( type );
149
        Constructor constructor = type.getDeclaredConstructor( EMPTY_PARAMS );
150
        if( constructor.isAccessible() ) {
151
          result = type.newInstance();
152
        } else {
153
          constructor.setAccessible( true );
154
          result = constructor.newInstance( null );
155
        }
156
      } catch( final SecurityException ex ) {
157
        String msg =   "Could not created the session singleton instance of '"
158
                     + type.getName() 
159
                     + "' due to security restrictions that probably do "
160
                     + "not allow reflection.";
161
        throw new RuntimeException( msg, ex );
162
      } catch( final IllegalArgumentException iae ) {
163
        String msg =   "Could not create the session singleton instance of '"
164
                     + type.getName() 
165
                     + "'. Probably there is no parameterless constructor.";
166
        throw new RuntimeException( msg, iae );
167
      } catch( final NoSuchMethodException nsme ) {
168
        String msg =   "Could not create the session singleton instance of '"
169
                     + type.getName() 
170
                     + "'. Probably there is no parameterless constructor.";
171
        throw new RuntimeException( msg, nsme );
172
      } catch( final InstantiationException ise ) {
173
        String msg =   "Could not create the session singleton instance of '"
174
                     + type.getName() 
175
                     + "'. Unable to create an instance.";
176
        throw new RuntimeException( msg, ise );
177
      } catch( final IllegalAccessException iae ) {
178
        String msg =   "Could not create the session singleton instance of '"
179
                      + type.getName() 
180
                      + "'. Not allowed to access the constructor "
181
                      + "for unknown reasons.";
182
        throw new RuntimeException( msg, iae );
183
      } catch( final InvocationTargetException ite ) {
184
        String msg =   "Could not create the session singleton instance of '"
185
                     + type.getName() 
186
                     + "' because an Exception was thrown by the constructor:\n"
187
                     + ite.getCause().getMessage()
188
                     + ".";
189
        throw new RuntimeException( msg, ite );
190
      }
191
      setAttribute( getInstanceKey( type ), result );
146
      setAttribute( getInstanceKey( type ), result );
192
    }
147
    }
193
    return result;
148
    return result;
(-)src/org/eclipse/rwt/internal/AdapterFactoryRegistryInstance.java (-9 / +6 lines)
Lines 19-25 Link Here
19
import org.eclipse.rwt.Adaptable;
19
import org.eclipse.rwt.Adaptable;
20
import org.eclipse.rwt.AdapterFactory;
20
import org.eclipse.rwt.AdapterFactory;
21
import org.eclipse.rwt.internal.service.ServletLog;
21
import org.eclipse.rwt.internal.service.ServletLog;
22
import org.eclipse.rwt.internal.util.ParamCheck;
22
import org.eclipse.rwt.internal.util.*;
23
23
24
24
25
public class AdapterFactoryRegistryInstance {
25
public class AdapterFactoryRegistryInstance {
Lines 64-76 Link Here
64
          factoryClass.getName(),
64
          factoryClass.getName(),
65
          adaptableClass.getName()
65
          adaptableClass.getName()
66
        };
66
        };
67
        String text
67
        String text = "The factory ''{0}'' was already added for the adaptable ''{1}''.";
68
          = "The factory ''{0}'' was already added for the adaptable ''{1}''.";
69
        String msg = MessageFormat.format( text, params );
68
        String msg = MessageFormat.format( text, params );
70
        throw new IllegalArgumentException( msg );
69
        throw new IllegalArgumentException( msg );
71
      }
70
      }
72
    }
71
    }
73
    
74
    FactoryEntry factoryEntry = new FactoryEntry();
72
    FactoryEntry factoryEntry = new FactoryEntry();
75
    factoryEntry.factoryClass = factoryClass;
73
    factoryEntry.factoryClass = factoryClass;
76
    factoryEntry.adaptableClass = adaptableClass;
74
    factoryEntry.adaptableClass = adaptableClass;
Lines 82-94 Link Here
82
    for( int i = 0; i < entries.length; i++ ) {
80
    for( int i = 0; i < entries.length; i++ ) {
83
      Class clazz = entries[ i ].factoryClass;
81
      Class clazz = entries[ i ].factoryClass;
84
      try {
82
      try {
85
        AdapterFactory factory = ( AdapterFactory )clazz.newInstance();
83
        AdapterFactory factory = ( AdapterFactory )ClassUtil.newInstance( clazz );
86
        AdapterManager manager = AdapterManagerImpl.getInstance();
84
        AdapterManagerImpl.getInstance().registerAdapters( factory, entries[ i ].adaptableClass );
87
        manager.registerAdapters( factory, entries[ i ].adaptableClass );
85
      } catch( ClassInstantiationException cie ) {
88
      } catch( Throwable thr ) {
89
        String text = "Could not create an instance of ''{0}''.";
86
        String text = "Could not create an instance of ''{0}''.";
90
        String msg = MessageFormat.format( text, new Object[] { clazz } );
87
        String msg = MessageFormat.format( text, new Object[] { clazz } );
91
        ServletLog.log( msg, thr );
88
        ServletLog.log( msg, cie );
92
      }
89
      }
93
    }
90
    }
94
  }
91
  }
(-)src/org/eclipse/rwt/internal/FacadesInitializer.java (-11 / +2 lines)
Lines 10-30 Link Here
10
 ******************************************************************************/
10
 ******************************************************************************/
11
package org.eclipse.rwt.internal;
11
package org.eclipse.rwt.internal;
12
12
13
import java.text.MessageFormat;
13
import org.eclipse.rwt.internal.util.ClassUtil;
14
14
15
public final class FacadesInitializer {
15
public final class FacadesInitializer {
16
16
17
  public static Object load( final Class facade ) {
17
  public static Object load( final Class facade ) {
18
    String name = facade.getName();
18
    String name = facade.getName();
19
    Object result = null;
19
    return ClassUtil.newInstance( facade.getClassLoader(), name + "Impl" );
20
    try {
21
      ClassLoader loader = facade.getClassLoader();
22
      result = loader.loadClass( name + "Impl" ).newInstance();
23
    } catch( Throwable throwable ) {
24
      String txt = "Could not load facade for {0}";
25
      String msg = MessageFormat.format( txt, new Object[] { name } );
26
      throw new RuntimeException( msg, throwable );
27
    }
28
    return result;
29
  }
20
  }
30
}
21
}
(-)src/org/eclipse/rwt/internal/engine/RWTContext.java (-26 / +2 lines)
Lines 11-21 Link Here
11
 ******************************************************************************/
11
 ******************************************************************************/
12
package org.eclipse.rwt.internal.engine;
12
package org.eclipse.rwt.internal.engine;
13
13
14
import java.lang.reflect.Constructor;
15
import java.text.MessageFormat;
14
import java.text.MessageFormat;
16
import java.util.HashMap;
15
import java.util.HashMap;
17
import java.util.Map;
16
import java.util.Map;
18
17
18
import org.eclipse.rwt.internal.util.ClassUtil;
19
import org.eclipse.rwt.internal.util.ParamCheck;
19
import org.eclipse.rwt.internal.util.ParamCheck;
20
20
21
21
Lines 51-57 Link Here
51
51
52
  private void createInstances( Class[] instanceTypes ) {
52
  private void createInstances( Class[] instanceTypes ) {
53
    for( int i = 0; i < instanceTypes.length; i++ ) {
53
    for( int i = 0; i < instanceTypes.length; i++ ) {
54
      Object instance = createInstance( instanceTypes[ i ] );
54
      Object instance = ClassUtil.newInstance( instanceTypes[ i ] );
55
      bufferInstance( instanceTypes[ i ], instance );
55
      bufferInstance( instanceTypes[ i ], instance );
56
    }
56
    }
57
  }
57
  }
Lines 77-94 Link Here
77
    }
77
    }
78
  }
78
  }
79
79
80
  private static Object createInstance( Class instanceType ) {
81
    Object result = null;
82
    try {
83
      Constructor constructor = instanceType.getDeclaredConstructor( null );
84
      constructor.setAccessible( true );
85
      result = constructor.newInstance( null );
86
    } catch( Exception shouldNotHappen ) {
87
      handleCreationProblem( instanceType, shouldNotHappen );
88
    }
89
    return result;
90
  }
91
92
  private static Object createInstanceFromFactory( Object instance ) {
80
  private static Object createInstanceFromFactory( Object instance ) {
93
    Object result = instance;
81
    Object result = instance;
94
    if( instance instanceof InstanceTypeFactory ) {
82
    if( instance instanceof InstanceTypeFactory ) {
Lines 107-124 Link Here
107
    return result;
95
    return result;
108
  }
96
  }
109
97
110
  private static void handleCreationProblem( Class instanceType, final Exception cause ) {
111
    String pattern = "Could not create instance of type ''{0}''.";
112
    Object[] arguments = new Object[] { instanceType.getName() };
113
    String msg = MessageFormat.format( pattern, arguments );
114
    throw new IllegalArgumentException( msg ) {
115
      private static final long serialVersionUID = 1L;
116
      public Throwable getCause() {
117
        return cause;
118
      }
119
    };
120
  }
121
122
  private static void checkRegistered( Class instanceType, Object instance ) {
98
  private static void checkRegistered( Class instanceType, Object instance ) {
123
    if( instance == null ) {
99
    if( instance == null ) {
124
      String pattern = "Unregistered instance type ''{0}''";
100
      String pattern = "Unregistered instance type ''{0}''";
(-)src/org/eclipse/rwt/internal/engine/RWTServletContextListener.java (-43 / +31 lines)
Lines 28-33 Link Here
28
import org.eclipse.rwt.internal.theme.*;
28
import org.eclipse.rwt.internal.theme.*;
29
import org.eclipse.rwt.internal.theme.css.CssFileReader;
29
import org.eclipse.rwt.internal.theme.css.CssFileReader;
30
import org.eclipse.rwt.internal.theme.css.StyleSheet;
30
import org.eclipse.rwt.internal.theme.css.StyleSheet;
31
import org.eclipse.rwt.internal.util.ClassInstantiationException;
32
import org.eclipse.rwt.internal.util.ClassUtil;
31
import org.eclipse.rwt.lifecycle.PhaseListener;
33
import org.eclipse.rwt.lifecycle.PhaseListener;
32
import org.eclipse.rwt.resources.IResource;
34
import org.eclipse.rwt.resources.IResource;
33
import org.eclipse.rwt.resources.IResourceManagerFactory;
35
import org.eclipse.rwt.resources.IResourceManagerFactory;
Lines 40-61 Link Here
40
public final class RWTServletContextListener implements ServletContextListener {
42
public final class RWTServletContextListener implements ServletContextListener {
41
43
42
  private static final String PREFIX = "org.eclipse.rwt.";
44
  private static final String PREFIX = "org.eclipse.rwt.";
43
  public static final String ENTRY_POINTS_PARAM
45
  public static final String ENTRY_POINTS_PARAM = PREFIX + "entryPoints";
44
    = PREFIX + "entryPoints";
46
  public static final String THEMES_PARAM = PREFIX + "themes";
45
  public static final String THEMES_PARAM
47
  public static final String RESOURCE_MANAGER_FACTORY_PARAM = PREFIX + "resourceManagerFactory";
46
    = PREFIX + "themes";
48
  public static final String SETTING_STORE_FACTORY_PARAM = PREFIX + "settingStoreFactory";
47
  public static final String RESOURCE_MANAGER_FACTORY_PARAM
49
  public static final String ADAPTER_FACTORIES_PARAM = PREFIX + "adapterFactories";
48
    = PREFIX + "resourceManagerFactory";
50
  public static final String PHASE_LISTENERS_PARAM = PREFIX + "phaseListeners";
49
  public static final String SETTING_STORE_FACTORY_PARAM
51
  public static final String RESOURCES_PARAM = PREFIX + "resources";
50
    = PREFIX + "settingStoreFactory";
52
  public static final String BRANDINGS_PARAM = PREFIX + "brandings";
51
  public static final String ADAPTER_FACTORIES_PARAM
52
    = PREFIX + "adapterFactories";
53
  public static final String PHASE_LISTENERS_PARAM
54
    = PREFIX + "phaseListeners";
55
  public static final String RESOURCES_PARAM
56
    = PREFIX + "resources";
57
  public static final String BRANDINGS_PARAM
58
    = PREFIX + "brandings";
59
53
60
  private static final String SEPARATOR = ",";
54
  private static final String SEPARATOR = ",";
61
55
Lines 68-73 Link Here
68
  private static final String REGISTERED_BRANDINGS
62
  private static final String REGISTERED_BRANDINGS
69
    = RWTServletContextListener.class.getName() + "registeredBrandings";
63
    = RWTServletContextListener.class.getName() + "registeredBrandings";
70
  
64
  
65
  private static final ClassLoader CLASS_LOADER = RWTServletContextListener.class.getClassLoader();
71
  
66
  
72
  public static class ContextDestroyer implements Runnable {
67
  public static class ContextDestroyer implements Runnable {
73
    protected final ServletContext servletContext;
68
    protected final ServletContext servletContext;
Lines 206-220 Link Here
206
  public static void registerResourceManagerFactory(
201
  public static void registerResourceManagerFactory(
207
    final ServletContext context )
202
    final ServletContext context )
208
  {
203
  {
209
    String factoryName
204
    String factoryName = context.getInitParameter( RESOURCE_MANAGER_FACTORY_PARAM );
210
      = context.getInitParameter( RESOURCE_MANAGER_FACTORY_PARAM );
211
    if( factoryName != null ) {
205
    if( factoryName != null ) {
212
      try {
206
      try {
213
        Class clazz = Class.forName( factoryName );
207
        IResourceManagerFactory factory
214
        IResourceManagerFactory factory;
208
          = ( IResourceManagerFactory )ClassUtil.newInstance( CLASS_LOADER, factoryName );
215
        factory = ( IResourceManagerFactory )clazz.newInstance();
216
        ResourceManager.register( factory );
209
        ResourceManager.register( factory );
217
      } catch( final Exception ex ) {
210
      } catch( ClassInstantiationException ex ) {
218
        String text = "Failed to register resource manager factory ''{0}''.";
211
        String text = "Failed to register resource manager factory ''{0}''.";
219
        String msg = MessageFormat.format( text, new Object[] { factoryName } );
212
        String msg = MessageFormat.format( text, new Object[] { factoryName } );
220
        context.log( msg, ex );
213
        context.log( msg, ex );
Lines 235-248 Link Here
235
        = context.getInitParameter( SETTING_STORE_FACTORY_PARAM );
228
        = context.getInitParameter( SETTING_STORE_FACTORY_PARAM );
236
      if( factoryName != null ) {
229
      if( factoryName != null ) {
237
        try {
230
        try {
238
          Class clazz = Class.forName( factoryName );
231
          ISettingStoreFactory factory
239
          ISettingStoreFactory factory; 
232
            = ( ISettingStoreFactory )ClassUtil.newInstance( CLASS_LOADER, factoryName ); 
240
          factory = ( ISettingStoreFactory )clazz.newInstance();
241
          SettingStoreManager.register( factory );
233
          SettingStoreManager.register( factory );
242
        } catch( final Exception ex ) {
234
        } catch( ClassInstantiationException cie ) {
243
          String text = "Failed to register setting store factory ''{0}''.";
235
          String text = "Failed to register setting store factory ''{0}''.";
244
          String msg = MessageFormat.format( text, new Object[] { factoryName } );
236
          String msg = MessageFormat.format( text, new Object[] { factoryName } );
245
          context.log( msg, ex );
237
          context.log( msg, cie );
246
        }
238
        }
247
      } else {
239
      } else {
248
        SettingStoreManager.register( new RWTFileSettingStoreFactory() );
240
        SettingStoreManager.register( new RWTFileSettingStoreFactory() );
Lines 297-309 Link Here
297
      for( int i = 0; i < listenerNames.length; i++ ) {
289
      for( int i = 0; i < listenerNames.length; i++ ) {
298
        String className = listenerNames[ i ].trim();
290
        String className = listenerNames[ i ].trim();
299
        try {
291
        try {
300
          Class clazz = Class.forName( className );
292
          PhaseListener listener = ( PhaseListener )ClassUtil.newInstance( CLASS_LOADER, className );
301
          PhaseListener listener = ( PhaseListener )clazz.newInstance();
302
          phaseListeners.add( listener );
293
          phaseListeners.add( listener );
303
        } catch( final Throwable thr ) {
294
        } catch( ClassInstantiationException cie ) {
304
          String text = "Failed to register phase listener ''{0}''.";
295
          String text = "Failed to register phase listener ''{0}''.";
305
          String msg = MessageFormat.format( text, new Object[] { className } );
296
          String msg = MessageFormat.format( text, new Object[] { className } );
306
          context.log( msg, thr );
297
          context.log( msg, cie );
307
        }
298
        }
308
      }
299
      }
309
    } else {
300
    } else {
Lines 345-357 Link Here
345
      for( int i = 0; i < resourceClassNames.length; i++ ) {
336
      for( int i = 0; i < resourceClassNames.length; i++ ) {
346
        String className = resourceClassNames[ i ].trim();
337
        String className = resourceClassNames[ i ].trim();
347
        try {
338
        try {
348
          Class clazz = Class.forName( className );
339
          IResource resource = ( IResource )ClassUtil.newInstance( CLASS_LOADER, className );
349
          IResource resource = ( IResource )clazz.newInstance();
350
          resources.add( resource );
340
          resources.add( resource );
351
        } catch( final Throwable thr ) {
341
        } catch( ClassInstantiationException cie ) {
352
          String text = "Failed to register resource ''{0}''.";
342
          String text = "Failed to register resource ''{0}''.";
353
          String msg = MessageFormat.format( text, new Object[] { className } );
343
          String msg = MessageFormat.format( text, new Object[] { className } );
354
          context.log( msg, thr );
344
          context.log( msg, cie );
355
        }
345
        }
356
      }
346
      }
357
    }
347
    }
Lines 393-405 Link Here
393
          String fileName = parts[ 1 ];
383
          String fileName = parts[ 1 ];
394
          try {
384
          try {
395
            String themeName = "Unnamed Theme: " + themeId;
385
            String themeName = "Unnamed Theme: " + themeId;
396
            StyleSheet styleSheet
386
            StyleSheet styleSheet = CssFileReader.readStyleSheet( fileName, loader );
397
              = CssFileReader.readStyleSheet( fileName, loader );
398
            Theme theme = new Theme( themeId, themeName, styleSheet );
387
            Theme theme = new Theme( themeId, themeName, styleSheet );
399
            manager.registerTheme( theme );
388
            manager.registerTheme( theme );
400
          } catch( Exception e ) {
389
          } catch( Exception e ) {
401
            String text = "Failed to register custom theme ''{0}'' "
390
            String text = "Failed to register custom theme ''{0}'' from resource ''{1}''";
402
                          + "from resource ''{1}''";
403
            Object[] args = new Object[] { themeId, fileName };
391
            Object[] args = new Object[] { themeId, fileName };
404
            String msg = MessageFormat.format( text, args );
392
            String msg = MessageFormat.format( text, args );
405
            context.log( msg, e );
393
            context.log( msg, e );
Lines 438-451 Link Here
438
      for( int i = 0; i < brandings.length; i++ ) {
426
      for( int i = 0; i < brandings.length; i++ ) {
439
        String className = brandings[ i ].trim();
427
        String className = brandings[ i ].trim();
440
        try {
428
        try {
441
          Object newInstance = Class.forName( className ).newInstance();
429
          AbstractBranding branding
442
          AbstractBranding branding = ( AbstractBranding )newInstance;
430
            = ( AbstractBranding )ClassUtil.newInstance( CLASS_LOADER, className );
443
          BrandingManager.register( branding );
431
          BrandingManager.register( branding );
444
          registeredBrandings.add( branding );
432
          registeredBrandings.add( branding );
445
        } catch( Exception e ) {
433
        } catch( ClassInstantiationException cie ) {
446
          String text = "Failed to register branding ''{0}''.";
434
          String text = "Failed to register branding ''{0}''.";
447
          String msg = MessageFormat.format( text, new Object[] { className } );
435
          String msg = MessageFormat.format( text, new Object[] { className } );
448
          servletContext.log( msg, e );
436
          servletContext.log( msg, cie );
449
        }
437
        }
450
      }
438
      }
451
      setRegisteredBrandings( servletContext, registeredBrandings );
439
      setRegisteredBrandings( servletContext, registeredBrandings );
(-)src/org/eclipse/rwt/internal/lifecycle/EntryPointInstantiationException.java (-25 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2002, 2007 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.rwt.internal.lifecycle;
13
14
15
/**
16
 * TODO [rh] JavaDoc
17
 */
18
public final class EntryPointInstantiationException extends RuntimeException {
19
20
  private static final long serialVersionUID = 1L;
21
  
22
  EntryPointInstantiationException( final String msg, final Throwable cause ) {
23
    super( msg, cause );
24
  }
25
}
(-)src/org/eclipse/rwt/internal/lifecycle/EntryPointManagerInstance.java (-10 / +3 lines)
Lines 16-22 Link Here
16
import java.util.Map;
16
import java.util.Map;
17
17
18
import org.eclipse.rwt.internal.service.ContextProvider;
18
import org.eclipse.rwt.internal.service.ContextProvider;
19
import org.eclipse.rwt.internal.util.ParamCheck;
19
import org.eclipse.rwt.internal.util.*;
20
import org.eclipse.rwt.lifecycle.IEntryPoint;
20
import org.eclipse.rwt.lifecycle.IEntryPoint;
21
import org.eclipse.rwt.service.ISessionStore;
21
import org.eclipse.rwt.service.ISessionStore;
22
22
Lines 60-68 Link Here
60
  }
60
  }
61
  
61
  
62
  int createUI( final String name ) {
62
  int createUI( final String name ) {
63
    ParamCheck.notNull( name, "name" );
63
    IEntryPoint entryPoint;
64
    IEntryPoint entryPoint;
64
    Class clazz;
65
    Class clazz;
65
    ParamCheck.notNull( name, "name" );
66
    synchronized( registry ) {
66
    synchronized( registry ) {
67
      if( !registry.containsKey( name ) ) {
67
      if( !registry.containsKey( name ) ) {
68
        String text = "An entry point named ''{0}'' does not exist.";
68
        String text = "An entry point named ''{0}'' does not exist.";
Lines 73-86 Link Here
73
    }
73
    }
74
    // no synchronization during instance creation to avoid lock in case
74
    // no synchronization during instance creation to avoid lock in case
75
    // of expensive constructor operations
75
    // of expensive constructor operations
76
    try {
76
    entryPoint = ( IEntryPoint )ClassUtil.newInstance( clazz );
77
      entryPoint = ( IEntryPoint )clazz.newInstance();
78
    } catch( Exception e ) {
79
      String text = "Failed to instantiate ''{0}''.";
80
      Object[] args = new Object[] { clazz.getName() };
81
      String msg = MessageFormat.format( text, args );
82
      throw new EntryPointInstantiationException( msg, e ) ;
83
    }      
84
    ISessionStore session = ContextProvider.getSession();
77
    ISessionStore session = ContextProvider.getSession();
85
    session.setAttribute( EntryPointManager.CURRENT_ENTRY_POINT, name );
78
    session.setAttribute( EntryPointManager.CURRENT_ENTRY_POINT, name );
86
    return entryPoint.createUI();
79
    return entryPoint.createUI();
(-)src/org/eclipse/rwt/internal/lifecycle/LifeCycleAdapterFactory.java (-3 / +4 lines)
Lines 15-20 Link Here
15
import java.util.HashMap;
15
import java.util.HashMap;
16
import java.util.Map;
16
import java.util.Map;
17
import org.eclipse.rwt.AdapterFactory;
17
import org.eclipse.rwt.AdapterFactory;
18
import org.eclipse.rwt.internal.util.ClassInstantiationException;
19
import org.eclipse.rwt.internal.util.ClassUtil;
18
import org.eclipse.rwt.lifecycle.ILifeCycleAdapter;
20
import org.eclipse.rwt.lifecycle.ILifeCycleAdapter;
19
import org.eclipse.rwt.lifecycle.IWidgetLifeCycleAdapter;
21
import org.eclipse.rwt.lifecycle.IWidgetLifeCycleAdapter;
20
import org.eclipse.swt.internal.widgets.displaykit.DisplayLCAFacade;
22
import org.eclipse.swt.internal.widgets.displaykit.DisplayLCAFacade;
Lines 110-118 Link Here
110
      String classToLoad = buffer.toString();
112
      String classToLoad = buffer.toString();
111
      ClassLoader loader = clazz.getClassLoader();
113
      ClassLoader loader = clazz.getClassLoader();
112
      try {
114
      try {
113
        Class adapterClass = loader.loadClass( classToLoad );
115
        result = ( IWidgetLifeCycleAdapter )ClassUtil.newInstance( loader, classToLoad );
114
        result = ( IWidgetLifeCycleAdapter )adapterClass.newInstance();
116
      } catch( ClassInstantiationException thr ) {
115
      } catch( final Throwable thr ) {
116
        // ignore and try to load next package name variant
117
        // ignore and try to load next package name variant
117
      }
118
      }
118
    }
119
    }
(-)src/org/eclipse/rwt/internal/lifecycle/LifeCycleFactoryInstance.java (-17 / +9 lines)
Lines 12-21 Link Here
12
 ******************************************************************************/
12
 ******************************************************************************/
13
package org.eclipse.rwt.internal.lifecycle;
13
package org.eclipse.rwt.internal.lifecycle;
14
14
15
import java.text.MessageFormat;
15
import org.eclipse.rwt.internal.ConfigurationReader;
16
16
import org.eclipse.rwt.internal.IConfiguration;
17
import org.eclipse.rwt.internal.*;
18
import org.eclipse.rwt.internal.service.ContextProvider;
17
import org.eclipse.rwt.internal.service.ContextProvider;
18
import org.eclipse.rwt.internal.util.ClassUtil;
19
import org.eclipse.rwt.lifecycle.ILifeCycle;
19
import org.eclipse.rwt.lifecycle.ILifeCycle;
20
import org.eclipse.rwt.service.ISessionStore;
20
import org.eclipse.rwt.service.ISessionStore;
21
21
Lines 41-60 Link Here
41
  public ILifeCycle loadLifeCycle() {
41
  public ILifeCycle loadLifeCycle() {
42
    LifeCycle result = globalLifeCycle;
42
    LifeCycle result = globalLifeCycle;
43
    if( result == null ) {
43
    if( result == null ) {
44
      String lifeCycleClassName = null;
44
      IConfiguration configuration = ConfigurationReader.getConfiguration();
45
      try {
45
      String lifeCycleClassName = configuration.getLifeCycle();
46
        IConfiguration configuration = ConfigurationReader.getConfiguration();
46
      ClassLoader classLoader = LifeCycleFactoryInstance.class.getClassLoader();
47
        lifeCycleClassName = configuration.getLifeCycle();
47
      result = ( LifeCycle )ClassUtil.newInstance( classLoader, lifeCycleClassName );
48
        Class lifeCycleClass = Class.forName( lifeCycleClassName );
48
      if( result.getScope().equals( Scope.APPLICATION ) ) {
49
        result = ( LifeCycle )lifeCycleClass.newInstance();
49
        globalLifeCycle = result;
50
        if( result.getScope().equals( Scope.APPLICATION ) ) {
51
          globalLifeCycle = result;
52
        }
53
      } catch( Exception ex ) {
54
        String text = "Could not load life cycle implementation {0}: {1}";
55
        Object[] args = new Object[] { lifeCycleClassName, ex.toString() };
56
        String msg = MessageFormat.format( text, args );
57
        throw new IllegalStateException( msg );
58
      }
50
      }
59
    }
51
    }
60
    return result;
52
    return result;
(-)src/org/eclipse/rwt/internal/service/ServiceManagerInstance.java (-4 / +3 lines)
Lines 24-29 Link Here
24
import javax.xml.parsers.DocumentBuilderFactory;
24
import javax.xml.parsers.DocumentBuilderFactory;
25
25
26
import org.eclipse.rwt.internal.resources.ResourceManager;
26
import org.eclipse.rwt.internal.resources.ResourceManager;
27
import org.eclipse.rwt.internal.util.ClassUtil;
27
import org.eclipse.rwt.resources.IResourceManager;
28
import org.eclipse.rwt.resources.IResourceManager;
28
import org.eclipse.rwt.service.IServiceHandler;
29
import org.eclipse.rwt.service.IServiceHandler;
29
import org.w3c.dom.*;
30
import org.w3c.dom.*;
Lines 72-79 Link Here
72
        Enumeration resources = manager.getResources( SERVICEHANDLER_XML );
73
        Enumeration resources = manager.getResources( SERVICEHANDLER_XML );
73
        while( resources != null && resources.hasMoreElements() ) {
74
        while( resources != null && resources.hasMoreElements() ) {
74
          URL url = ( URL )resources.nextElement();
75
          URL url = ( URL )resources.nextElement();
75
          DocumentBuilderFactory factory
76
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
76
            = DocumentBuilderFactory.newInstance();
77
          DocumentBuilder builder = factory.newDocumentBuilder();
77
          DocumentBuilder builder = factory.newDocumentBuilder();
78
          URLConnection con = url.openConnection();
78
          URLConnection con = url.openConnection();
79
          con.setUseCaches( false );
79
          con.setUseCaches( false );
Lines 92-99 Link Here
92
            String name = attributes.getNamedItem( "class" ).getNodeValue();
92
            String name = attributes.getNamedItem( "class" ).getNodeValue();
93
            String param = "requestparameter";
93
            String param = "requestparameter";
94
            String id = attributes.getNamedItem( param ).getNodeValue();
94
            String id = attributes.getNamedItem( param ).getNodeValue();
95
            Class clazz = getClass().getClassLoader().loadClass( name );
95
            Object handlerInstance = ClassUtil.newInstance( getClass().getClassLoader(), name );
96
            Object handlerInstance = clazz.newInstance();
97
            customHandlers.put( id, handlerInstance );
96
            customHandlers.put( id, handlerInstance );
98
          }
97
          }
99
        }
98
        }
(-)src/org/eclipse/rwt/internal/theme/ThemeAdapterUtil.java (-11 / +4 lines)
Lines 17-22 Link Here
17
17
18
import org.eclipse.rwt.internal.engine.RWTContext;
18
import org.eclipse.rwt.internal.engine.RWTContext;
19
import org.eclipse.rwt.internal.lifecycle.LifeCycleAdapterUtil;
19
import org.eclipse.rwt.internal.lifecycle.LifeCycleAdapterUtil;
20
import org.eclipse.rwt.internal.util.ClassInstantiationException;
21
import org.eclipse.rwt.internal.util.ClassUtil;
20
import org.eclipse.swt.widgets.Widget;
22
import org.eclipse.swt.widgets.Widget;
21
23
22
24
Lines 77-94 Link Here
77
  {
79
  {
78
    IThemeAdapter result = null;
80
    IThemeAdapter result = null;
79
    try {
81
    try {
80
      Class adapterClass = classLoader.loadClass( className );
82
      result = ( IThemeAdapter )ClassUtil.newInstance( classLoader, className );
81
      result = ( IThemeAdapter )adapterClass.newInstance();
83
    } catch( ClassInstantiationException cie ) {
82
    } catch( final ClassNotFoundException e ) {
83
      // ignore, try to load from next package name variant
84
      // ignore, try to load from next package name variant
84
    } catch( final InstantiationException e ) {
85
      String message =   "Failed to instantiate theme adapter class "
86
                       + className;
87
      throw new ThemeManagerException( message, e );
88
    } catch( final IllegalAccessException e ) {
89
      String message =   "Failed to instantiate theme adapter class "
90
                       + className;
91
      throw new ThemeManagerException( message, e );
92
    }
85
    }
93
    return result;
86
    return result;
94
  }
87
  }
(-)src/org/eclipse/rwt/internal/util/ClassInstantiationException.java (+21 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2002, 2011 Innoopract Informationssysteme GmbH.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Innoopract Informationssysteme GmbH - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.rwt.internal.util;
12
13
14
public final class ClassInstantiationException extends RuntimeException {
15
16
  private static final long serialVersionUID = 1L;
17
  
18
  public ClassInstantiationException( String msg, Throwable cause ) {
19
    super( msg, cause );
20
  }
21
}
(-)src/org/eclipse/rwt/internal/util/ClassUtil.java (+57 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 Rüdiger Herrmann and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    Rüdiger Herrmann - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.rwt.internal.util;
12
13
import java.lang.reflect.Constructor;
14
15
16
public final class ClassUtil {
17
  
18
  public static Object newInstance( ClassLoader classLoader, String className ) {
19
    ParamCheck.notNull( className, "className" );
20
    ParamCheck.notNull( classLoader, "classLoader" );
21
    Class type;
22
    try {
23
      type = classLoader.loadClass( className );
24
    } catch( ClassNotFoundException cnfe ) {
25
      throw new ClassInstantiationException( "Failed to load type: " + className, cnfe );
26
    }
27
    return newInstance( type );
28
  }
29
  
30
  public static Object newInstance( Class type ) {
31
    return newInstance( type, null, null );
32
  }
33
34
  public static Object newInstance( Class type, Class[] paramTypes, Object[] paramValues ) {
35
    ParamCheck.notNull( type, "type" );
36
    try {
37
      return createInstance( type, paramTypes, paramValues );
38
    } catch( Exception e ) {
39
      String msg = "Failed to create instance of type: " + type.getName();
40
      throw new ClassInstantiationException( msg, e );
41
    }
42
  }
43
44
  private static Object createInstance( Class type, Class[] paramTypes, Object[] paramValues ) 
45
    throws Exception 
46
  {
47
    Constructor constructor = type.getDeclaredConstructor( paramTypes );
48
    if( !constructor.isAccessible() ) {
49
      constructor.setAccessible( true );
50
    } 
51
    return constructor.newInstance( paramValues );
52
  }
53
54
  private ClassUtil() {
55
    // prevent instantiation
56
  }
57
}
(-)src/org/eclipse/swt/internal/graphics/ImageFactoryInstance.java (-12 / +4 lines)
Lines 17-22 Link Here
17
import java.util.Map;
17
import java.util.Map;
18
18
19
import org.eclipse.rwt.internal.resources.ResourceManager;
19
import org.eclipse.rwt.internal.resources.ResourceManager;
20
import org.eclipse.rwt.internal.util.ClassUtil;
20
import org.eclipse.rwt.resources.IResourceManager;
21
import org.eclipse.rwt.resources.IResourceManager;
21
import org.eclipse.swt.graphics.Device;
22
import org.eclipse.swt.graphics.Device;
22
import org.eclipse.swt.graphics.Image;
23
import org.eclipse.swt.graphics.Image;
Lines 80-97 Link Here
80
  }
81
  }
81
82
82
  private static Image createImageInstance( Device device, InternalImage internalImage ) {
83
  private static Image createImageInstance( Device device, InternalImage internalImage ) {
83
    Image result;
84
    Class[] paramTypes = new Class[] { Device.class, InternalImage.class };
84
    try {
85
    Object[] paramValues = new Object[] { device, internalImage };
85
      Class imageClass = Image.class;
86
    return ( Image )ClassUtil.newInstance( Image.class, paramTypes, paramValues );
86
      Class[] paramList = new Class[] { Device.class, InternalImage.class };
87
      Constructor constructor = imageClass.getDeclaredConstructor( paramList );
88
      constructor.setAccessible( true );
89
      Object[] args = new Object[] { device, internalImage };
90
      result = ( Image )constructor.newInstance( args );
91
    } catch( final Exception e ) {
92
      throw new RuntimeException( "Failed to instantiate Image", e );
93
    }
94
    return result;
95
  }
87
  }
96
88
97
  private static InputStream getInputStream( String path, ClassLoader imageLoader ) {
89
  private static InputStream getInputStream( String path, ClassLoader imageLoader ) {
(-)src/org/eclipse/swt/internal/graphics/ResourceFactoryInstance.java (-34 / +10 lines)
Lines 16-21 Link Here
16
import java.util.HashMap;
16
import java.util.HashMap;
17
import java.util.Map;
17
import java.util.Map;
18
18
19
import org.eclipse.rwt.internal.util.ClassUtil;
19
import org.eclipse.swt.graphics.*;
20
import org.eclipse.swt.graphics.*;
20
21
21
22
Lines 90-134 Link Here
90
  }
91
  }
91
92
92
  private static Color createColorInstance( final int colorNr ) {
93
  private static Color createColorInstance( final int colorNr ) {
93
    Color result = null;
94
    Class[] paramTypes = new Class[] { int.class };
94
    try {
95
    Object[] paramValues = new Object[] { new Integer( colorNr ) };
95
      Class[] paramList = new Class[] { int.class };
96
    return ( Color )ClassUtil.newInstance( Color.class, paramTypes, paramValues );
96
      Constructor constr = Color.class.getDeclaredConstructor( paramList );
97
      constr.setAccessible( true );
98
      Object[] args = new Object[] { new Integer( colorNr ) };
99
      result = ( Color )constr.newInstance( args );
100
    } catch( final Exception e ) {
101
      throw new RuntimeException( "Failed to instantiate Color", e );
102
    }
103
    return result;
104
  }
97
  }
105
98
106
  private static Font createFontInstance( final FontData fontData ) {
99
  private static Font createFontInstance( final FontData fontData ) {
107
    Font result = null;
100
    Class[] paramTypes = new Class[] { FontData.class };
108
    try {
101
    Object[] paramValues = new Object[] { fontData };
109
      Class[] paramList = new Class[] { FontData.class };
102
    return ( Font )ClassUtil.newInstance( Font.class, paramTypes, paramValues );
110
      Constructor constr = Font.class.getDeclaredConstructor( paramList );
111
      constr.setAccessible( true );
112
      result = ( Font )constr.newInstance( new Object[] { fontData } );
113
    } catch( final Exception e ) {
114
      throw new RuntimeException( "Failed to instantiate Font", e );
115
    }
116
    return result;
117
  }
103
  }
118
104
119
  private static Cursor createCursorInstance( final int style ) {
105
  private static Cursor createCursorInstance( final int style ) {
120
    Cursor result = null;
106
    Class[] paramTypes = new Class[] { int.class };
121
    try {
107
    Object[] paramValues = new Object[] { new Integer( style ) };
122
      Class cursorClass = Cursor.class;
108
    return ( Cursor )ClassUtil.newInstance( Cursor.class, paramTypes, paramValues );
123
      Class[] paramList = new Class[] { int.class };
124
      Constructor constr = cursorClass.getDeclaredConstructor( paramList );
125
      constr.setAccessible( true );
126
      result = ( Cursor )constr.newInstance( new Object[] {
127
        new Integer( style )
128
      } );
129
    } catch( final Exception e ) {
130
      throw new RuntimeException( "Failed to instantiate Cursor", e );
131
    }
132
    return result;
133
  }
109
  }
134
}
110
}
(-)src/org/eclipse/RWTHostTestSuite.java (+2 lines)
Lines 26-31 Link Here
26
import org.eclipse.rwt.internal.service.*;
26
import org.eclipse.rwt.internal.service.*;
27
import org.eclipse.rwt.internal.theme.*;
27
import org.eclipse.rwt.internal.theme.*;
28
import org.eclipse.rwt.internal.theme.css.*;
28
import org.eclipse.rwt.internal.theme.css.*;
29
import org.eclipse.rwt.internal.util.ClassUtil_Test;
29
import org.eclipse.rwt.internal.widgets.JSExecutor_Test;
30
import org.eclipse.rwt.internal.widgets.JSExecutor_Test;
30
import org.eclipse.rwt.lifecycle.*;
31
import org.eclipse.rwt.lifecycle.*;
31
import org.eclipse.rwt.service.*;
32
import org.eclipse.rwt.service.*;
Lines 115-120 Link Here
115
    suite.addTestSuite( LifeCycleServiceHandler_Test.class );
116
    suite.addTestSuite( LifeCycleServiceHandler_Test.class );
116
    suite.addTestSuite( StartupPageTemplateHolder_Test.class );
117
    suite.addTestSuite( StartupPageTemplateHolder_Test.class );
117
    suite.addTestSuite( EncodingUtil_Test.class );
118
    suite.addTestSuite( EncodingUtil_Test.class );
119
    suite.addTestSuite( ClassUtil_Test.class );
118
120
119
    suite.addTestSuite( Display_Test.class );
121
    suite.addTestSuite( Display_Test.class );
120
    suite.addTestSuite( Monitor_Test.class );
122
    suite.addTestSuite( Monitor_Test.class );
(-)src/org/eclipse/rwt/internal/engine/RWTContext_Test.java (-1 / +2 lines)
Lines 14-19 Link Here
14
import junit.framework.TestCase;
14
import junit.framework.TestCase;
15
15
16
import org.eclipse.rwt.internal.engine.RWTContext.InstanceTypeFactory;
16
import org.eclipse.rwt.internal.engine.RWTContext.InstanceTypeFactory;
17
import org.eclipse.rwt.internal.util.ClassInstantiationException;
17
18
18
19
19
public class RWTContext_Test extends TestCase {
20
public class RWTContext_Test extends TestCase {
Lines 81-87 Link Here
81
    try {
82
    try {
82
      new RWTContext( new Class[] { AbstractClass.class } );
83
      new RWTContext( new Class[] { AbstractClass.class } );
83
      fail();
84
      fail();
84
    } catch( IllegalArgumentException expected ) {
85
    } catch( ClassInstantiationException expected ) {
85
    }    
86
    }    
86
  }
87
  }
87
  
88
  
(-)src/org/eclipse/rwt/internal/util/ClassUtil_Test.java (+153 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 Rüdiger Herrmann and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *    Rüdiger Herrmann - initial API and implementation
10
 ******************************************************************************/
11
package org.eclipse.rwt.internal.util;
12
13
import java.lang.reflect.InvocationTargetException;
14
15
import junit.framework.TestCase;
16
17
18
public class ClassUtil_Test extends TestCase {
19
  
20
  private ClassLoader classLoader;
21
  
22
  static class ConstructorException extends RuntimeException {
23
    private static final long serialVersionUID = 1L;
24
  }
25
  
26
  public static abstract class AbstractClass {
27
  }
28
29
  public static class PublicClass {
30
    Object objectParam;
31
    String stringParam;
32
    Long longParam;
33
    public PublicClass() {
34
    }
35
    private PublicClass( Object objectParam ) {
36
      this.objectParam = objectParam;
37
    }
38
    public PublicClass( String stringParam, Long longParam ) {
39
      this.stringParam = stringParam;
40
      this.longParam = longParam;
41
    }
42
  }
43
  
44
  static class ClassWithPrivateConstructor {
45
    private ClassWithPrivateConstructor() {
46
    }
47
  }
48
  
49
  static class ClassWithExceptionInConstructor {
50
    ClassWithExceptionInConstructor() {
51
      throw new ConstructorException();
52
    }
53
  }
54
  
55
  public void testNewInstanceWithNullClass() {
56
    try {
57
      ClassUtil.newInstance( ( Class )null );
58
      fail();
59
    } catch( NullPointerException expected ) {
60
    }
61
  }
62
  
63
  public void testNewInstanceWithAbstractClass() {
64
    try {
65
      ClassUtil.newInstance( AbstractClass.class );
66
      fail();
67
    } catch( ClassInstantiationException expected ) {
68
    }
69
  }
70
  
71
  public void testNewInstanceWithInterface() {
72
    try {
73
      ClassUtil.newInstance( Runnable.class );
74
      fail();
75
    } catch( ClassInstantiationException expected ) {
76
    }
77
  }
78
  
79
  public void testNewInstanceWithPublicClass() {
80
    Object instance = ClassUtil.newInstance( ClassWithPrivateConstructor.class );
81
    assertEquals( instance.getClass(), ClassWithPrivateConstructor.class );
82
  }
83
  
84
  public void testNewInstanceWithPrivateDefaultConstructor() {
85
    Object instance = ClassUtil.newInstance( PublicClass.class );
86
    assertEquals( instance.getClass(), PublicClass.class );
87
  }
88
  
89
  public void testNewInstanceWithEceptionInConstructor() {
90
    try {
91
      ClassUtil.newInstance( ClassWithExceptionInConstructor.class );
92
      fail();
93
    } catch( ClassInstantiationException e ) {
94
      assertEquals( e.getCause().getClass(), InvocationTargetException.class );
95
    }
96
  }
97
  
98
  public void testNewInstanceWithNullClassNameAndClassLoader() {
99
    try {
100
      ClassUtil.newInstance( classLoader, null );
101
      fail();
102
    } catch( NullPointerException expected ) {
103
    }
104
  }
105
  
106
  public void testNewInstanceWithNullClassLoader() {
107
    try {
108
      ClassUtil.newInstance( null, "" );
109
      fail();
110
    } catch( NullPointerException expected ) {
111
    }
112
  }
113
  
114
  public void testNewInstanceWithNonExistingClassName() {
115
    try {
116
      ClassUtil.newInstance( classLoader, "does.not.exist" );
117
      fail();
118
    } catch( ClassInstantiationException expected ) {
119
    }
120
  }
121
  
122
  public void testNewInstanceWithExistingClassName() {
123
    Object instance = ClassUtil.newInstance( classLoader, PublicClass.class.getName() );
124
    assertEquals( instance.getClass(), PublicClass.class );
125
  }
126
  
127
  
128
  public void testNewInstanceWithPublicParameterizedConstructor() {
129
    String stringValue = "string";
130
    Long longValue = new Long( 0 );
131
    Class[] paramTypes = new Class[] { String.class, Long.class };
132
    Object[] paramValues = new Object[] { stringValue, longValue };
133
    Object instance = ClassUtil.newInstance( PublicClass.class, paramTypes, paramValues );
134
    assertEquals( instance.getClass(), PublicClass.class );
135
    PublicClass publicClass = ( PublicClass )instance;
136
    assertEquals( stringValue, publicClass.stringParam );
137
    assertEquals( longValue, publicClass.longParam );
138
  }
139
  
140
  public void testNewInstanceWithPrivateParameterizedConstructor() {
141
    Object objectValue = new Object();
142
    Class[] paramTypes = new Class[] { Object.class };
143
    Object[] paramValues = new Object[] { objectValue };
144
    Object instance = ClassUtil.newInstance( PublicClass.class, paramTypes, paramValues );
145
    assertEquals( instance.getClass(), PublicClass.class );
146
    PublicClass publicClass = ( PublicClass )instance;
147
    assertEquals( objectValue, publicClass.objectParam );
148
  }
149
  
150
  protected void setUp() throws Exception {
151
    classLoader = ClassUtil_Test.class.getClassLoader();
152
  }
153
}

Return to bug 339827