All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.deltaspike.core.util.ClassUtils Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.deltaspike.core.util;

import javax.enterprise.inject.Typed;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.jar.Manifest;
import java.util.jar.Attributes;
import java.net.URL;

/**
 * Util methods for classes, {@link ClassLoader} and {@link Manifest} handling
 */
//X TODO quite a few of this methods needs merging with Seam Solder and a few can get dropped at all.
@Typed()
public abstract class ClassUtils
{
    /**
     * Constructor which prevents the instantiation of this class
     */
    private ClassUtils()
    {
        // prevent instantiation
    }

    /**
     * Detect the right ClassLoader.
     * The lookup order is determined by:
     * 
    *
  1. ContextClassLoader of the current Thread
  2. *
  3. ClassLoader of the given Object 'o'
  4. *
  5. ClassLoader of this very ClassUtils class
  6. *
* * @param o if not null it may get used to detect the classloader. * @return The {@link ClassLoader} which should get used to create new instances */ public static ClassLoader getClassLoader(Object o) { if (System.getSecurityManager() != null) { return AccessController.doPrivileged(new GetClassLoaderAction(o)); } else { return getClassLoaderInternal(o); } } static class GetClassLoaderAction implements PrivilegedAction { private Object object; GetClassLoaderAction(Object object) { this.object = object; } @Override public ClassLoader run() { try { return getClassLoaderInternal(object); } catch (Exception e) { return null; } } } private static ClassLoader getClassLoaderInternal(Object o) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader == null && o != null) { loader = o.getClass().getClassLoader(); } if (loader == null) { loader = ClassUtils.class.getClassLoader(); } return loader; } /** * Tries to load a class based on the given name and interface or abstract class. * @param name name of the concrete class * @param targetType target type (interface or abstract class) * @param current type * @return loaded class or null if it isn't in the classpath */ public static Class tryToLoadClassForName(String name, Class targetType) { return (Class) tryToLoadClassForName(name); } /** * Tries to load a class based on the given name * @param name name of the class * @return loaded class or null if it isn't in the classpath */ public static Class tryToLoadClassForName(String name) { try { return loadClassForName(name); } catch (ClassNotFoundException e) { //do nothing - it's just a try return null; } } /** * Loads class for the given name * @param name name of the class * @return loaded class * @throws ClassNotFoundException if the class can't be loaded */ public static Class loadClassForName(String name) throws ClassNotFoundException { try { // Try WebApp ClassLoader first return Class.forName(name, false, // do not initialize for faster startup getClassLoader(null)); } catch (ClassNotFoundException ignore) { // fallback: Try ClassLoader for ClassUtils (i.e. the myfaces.jar lib) return Class.forName(name, false, // do not initialize for faster startup ClassUtils.class.getClassLoader()); } } /** * Instantiates a given class via the default constructor * @param targetClass class which should be instantiated * @param current type * @return created instance or null if the instantiation failed */ public static T tryToInstantiateClass(Class targetClass) { try { return targetClass.newInstance(); } catch (InstantiationException e) { //do nothing - it was just a try } catch (IllegalAccessException e) { //do nothing - it was just a try } return null; } /** * Tries to instantiate a class for the given name and type via the default constructor * @param className name of the class * @param targetType target type * @param current type * @return created instance or null if the instantiation failed */ public static T tryToInstantiateClassForName(String className, Class targetType) { Object result = tryToInstantiateClassForName(className); //noinspection unchecked return result != null ? (T) result : null; } /** * Tries to instantiate a class for the given name via the default constructor * @param className name of the class * @return created instance or null if the instantiation failed */ public static Object tryToInstantiateClassForName(String className) { try { return instantiateClassForName(className); } catch (Exception e) { //do nothing - it was just a try } return null; } /** * Creates an instance for the given class-name * @param className name of the class which should be instantiated * @return created instance * @throws ClassNotFoundException if the instantiation failed * @throws IllegalAccessException if the instantiation failed * @throws InstantiationException if the instantiation failed */ public static Object instantiateClassForName(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException { return loadClassForName(className).newInstance(); } /** * Reads the version of the jar which contains the given class * @param targetClass class within the jar * @return version-string which has been found in the manifest or null if there is no version information available */ public static String getJarVersion(Class targetClass) { String manifestFileLocation = getManifestFileLocationOfClass(targetClass); try { return new Manifest(new URL(manifestFileLocation).openStream()) .getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION); } catch (Exception e) { return null; } } /** * Reads the VCS revision which was used for creating the jar * @param targetClass class within the jar * @return revision-string which has been found in the manifest or null if there is no information available */ public static String getRevision(Class targetClass) { String manifestFileLocation = getManifestFileLocationOfClass(targetClass); try { return new Manifest(new URL(manifestFileLocation).openStream()) .getMainAttributes().getValue("Revision"); } catch (Exception e) { return null; } } private static String getManifestFileLocationOfClass(Class targetClass) { String manifestFileLocation; try { manifestFileLocation = getManifestLocation(targetClass); } catch (Exception e) { //in this case we have a proxy manifestFileLocation = getManifestLocation(targetClass.getSuperclass()); } return manifestFileLocation; } private static String getManifestLocation(Class targetClass) { String classFilePath = targetClass.getCanonicalName().replace('.', '/') + ".class"; String manifestFilePath = "/META-INF/MANIFEST.MF"; String classLocation = targetClass.getResource(targetClass.getSimpleName() + ".class").toString(); return classLocation.substring(0, classLocation.indexOf(classFilePath) - 1) + manifestFilePath; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy