Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab0ad2832d078e5f5cd3077a40e3a9d054d39109 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.constraints.util;

import org.eclipse.papyrus.infra.constraints.Activator;

/**
 * A Helper class for Class Loading.
 *
 * @author Camille Letavernier
 */
public class ClassLoader {

	/**
	 * Loads the class matching the given className. Exceptions are caught and sent
	 * to the Logger.
	 *
	 * @param className
	 *            The qualified name of the Class to load.
	 * @return
	 *         The loaded Class, or null if an error occured
	 */
	public Class<?> loadClass(String className) {
		try {
			Class<?> clazz = Activator.getDefault().getBundle().loadClass(className);
			return clazz;
		} catch (ClassNotFoundException ex) {
			Activator.log.error("Cannot load class " + className, ex); //$NON-NLS-1$
		} catch (ClassCastException ex) {
			Activator.log.error("Cannot load class " + className, ex); //$NON-NLS-1$
		} catch (NullPointerException ex) {
			Activator.log.error("Cannot load class " + className, ex); //$NON-NLS-1$
		}

		return null;
	}

	/**
	 * Returns a new Instance of the given class
	 *
	 * @param className
	 *            The qualified name of the Class to instantiate
	 * @return
	 *         A new instance of the given class, or null if the class couldn't be
	 *         instantiated
	 */
	public Object newInstance(String className) {
		return newInstance(loadClass(className));
	}

	/**
	 * Returns a new Instance of the given class
	 *
	 * @param theClass
	 *            The Class to instantiate
	 * @return
	 *         A new instance of the given class, or null if the class couldn't be
	 *         instantiated
	 */
	public <T extends Object> T newInstance(Class<T> theClass) {
		if (theClass == null) {
			return null;
		}

		try {
			return theClass.newInstance();
		} catch (IllegalAccessException ex) {
			Activator.log.error(ex);
		} catch (InstantiationException ex) {
			Activator.log.error(ex);
		}

		return null;
	}
}

Back to the top