001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.insync.java;
042:
043: import java.lang.reflect.Array;
044: import java.util.HashMap;
045: import org.netbeans.modules.visualweb.jsfsupport.container.FacesContainer;
046:
047: /**
048: * Provides utilities to obtain the class given the identifier or type name
049: *
050: * @author jdeva
051: *
052: */
053: public class ClassUtil {
054:
055: /**
056: * Get the actual class given its type. The type may be a primitive and/or it
057: * may be an array.
058: *
059: * @param type The type retrieve the Class for.
060: * @return The Class that represents the given type.
061: * @throws ClassNotFoundException
062: */
063: public static Class getClass(String type, ClassLoader cl)
064: throws ClassNotFoundException {
065: if (type == null)
066: return null;
067:
068: if (type.endsWith("[]")) {
069: String ctype = type.substring(0, type.length() - 2);
070: return Array.newInstance(getClass(ctype, cl), 0).getClass();
071: }
072:
073: if (type.equals("boolean") || type.equals("Z")) //NOI18N
074: return Boolean.TYPE;
075: if (type.equals("byte") || type.equals("B")) //NOI18N
076: return Byte.TYPE;
077: if (type.equals("char") || type.equals("C")) //NOI18N
078: return Byte.TYPE;
079: if (type.equals("double") || type.equals("D")) //NOI18N
080: return Double.TYPE;
081: if (type.equals("float") || type.equals("F")) //NOI18N
082: return Float.TYPE;
083: if (type.equals("int") || type.equals("I")) //NOI18N
084: return Integer.TYPE;
085: if (type.equals("long") || type.equals("J")) //NOI18N
086: return Long.TYPE;
087: if (type.equals("short") || type.equals("S")) //NOI18N
088: return Short.TYPE;
089: if (type.equals("void")) //NOI18N
090: return Void.TYPE;
091:
092: return Class.forName(type, true, cl);
093: }
094:
095: /**
096: * Get the actual class given its type. The type may be a primitive and/or it
097: * may be an array.
098: *
099: * @param type The type retrieve the Class for.
100: * @return The Class that represents the given type.
101: * @throws ClassNotFoundException
102: */
103: public static Class getClass(String type)
104: throws ClassNotFoundException {
105: //Use the project classloader to load the class
106: ClassLoader contextClassLoader = Thread.currentThread()
107: .getContextClassLoader();
108: // assert contextClassLoader.getClass() == org.netbeans.modules.visualweb.insync.ModelSet.ProjectClassLoader.class;
109: return getClass(type, contextClassLoader);
110: }
111: }
|