Example usage for java.lang Class getPackage

List of usage examples for java.lang Class getPackage

Introduction

In this page you can find the example usage for java.lang Class getPackage.

Prototype

public Package getPackage() 

Source Link

Document

Gets the package of this class.

Usage

From source file:cascading.util.Util.java

public static String captureDebugTrace(Class type) {
    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

    for (int i = 3; i < stackTrace.length; i++) {
        StackTraceElement stackTraceElement = stackTrace[i];

        Package aPackage = type.getPackage();

        if (aPackage != null && stackTraceElement.getClassName().startsWith(aPackage.getName()))
            continue;

        return stackTraceElement.toString();
    }//from   www .  j  a v  a2s.  c  o m

    return null;
}

From source file:org.alfresco.rest.framework.core.ResourceInspector.java

/**
 * Inspects the resource to determine what api it belongs to.
 * It does this by looking for the WebApi package annotation.
 * //  w ww .  ja  va2s . c  o  m
 * @return Api
 */
public static Api inspectApi(Class<?> resource) {
    Package myPackage = resource.getPackage();
    Annotation annot = myPackage.getAnnotation(WebApi.class);

    if (annot != null) {
        Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot);
        String apiName = String.valueOf(annotAttribs.get("name"));
        String apiScope = String.valueOf(annotAttribs.get("scope"));
        String apiVersion = String.valueOf(annotAttribs.get("version"));
        return Api.valueOf(apiName, apiScope, apiVersion);
    }
    return null;
}

From source file:com.threewks.thundr.module.Modules.java

protected static Module loadModule(Class<? extends Module> moduleClass) {
    try {/*  w  w w . j a va 2 s .  co m*/
        Object newInstance = moduleClass.newInstance();
        Module configuration = Cast.as(newInstance, Module.class);
        if (configuration == null) {
            throw new ModuleLoadingException(
                    "Failed to load module '%s' - the configuration class %s does not implement '%s'",
                    moduleClass.getName(), Module.class.getName());
        }
        return configuration;
    } catch (InstantiationException e) {
        throw new ModuleLoadingException(e, moduleClass.getPackage().getName(),
                "failed to instantiate configuration class %s: %s", moduleClass.getName(), e.getMessage());
    } catch (IllegalAccessException e) {
        throw new ModuleLoadingException(e, moduleClass.getPackage().getName(),
                "cannot instantiate configuration class %s: %s", moduleClass.getName(), e.getMessage());
    }
}

From source file:io.apiman.manager.api.es.EsMarshallingTest.java

/**
 * Populate the given set with one or two items of the given type.
 * @param collection//from   ww  w.j a  v  a2s .c  o  m
 * @param typeClass
 */
private static void populateSet(Set collection, Class<?> typeClass)
        throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException,
        SecurityException, ClassNotFoundException {
    if (typeClass.isEnum()) {
        collection.add(typeClass.getEnumConstants()[0]);
        collection.add(typeClass.getEnumConstants()[1]);
    } else if (typeClass == String.class) {
        collection.add("VALUE_1");
        collection.add("VALUE_2");
    } else if (typeClass.getPackage().getName().startsWith("io.apiman.manager.api.beans")) {
        Object bean1 = createBean(typeClass);
        Object bean2 = createBean(typeClass);
        collection.add(bean1);
        collection.add(bean2);
    } else {
        throw new IllegalAccessException("Failed to populate Set of type: " + typeClass.getSimpleName());
    }
}

From source file:org.eclipse.che.vfs.impl.fs.LocalFileSystemTest.java

private static void enableAssertion(Class<?> clazz) {
    clazz.getClassLoader().setPackageAssertionStatus(clazz.getPackage().getName(), true);
}

From source file:net.ymate.platform.plugin.Plugins.java

/**
 * @param clazz ?/*from   w w  w  .  ja  v  a 2s. c  om*/
 * @return ????
 * @throws Exception ??
 */
private static DefaultPluginConfig loadConfig(Class<? extends IPluginFactory> clazz) throws Exception {
    DefaultPluginConfig _config = null;
    if (clazz != null && clazz.isAnnotationPresent(PluginFactory.class)) {
        _config = new DefaultPluginConfig();
        //
        PluginFactory _factoryAnno = clazz.getAnnotation(PluginFactory.class);
        if (StringUtils.isNotBlank(_factoryAnno.pluginHome())) {
            _config.setPluginHome(new File(_factoryAnno.pluginHome()));
        }
        String[] _packages = _factoryAnno.autoscanPackages();
        if (ArrayUtils.isEmpty(_packages)) {
            _packages = new String[] { clazz.getPackage().getName() };
        }
        _config.setAutoscanPackages(Arrays.asList(_packages));
        _config.setIncludedClassPath(_factoryAnno.includedClassPath());
        _config.setAutomatic(_factoryAnno.automatic());
        //
        IPluginEventListener _listener = ClassUtils.impl(_factoryAnno.listenerClass(),
                IPluginEventListener.class);
        if (_listener != null) {
            _config.setPluginEventListener(_listener);
        } else {
            _config.setPluginEventListener(new DefaultPluginEventListener());
        }
    }
    return _config;
}

From source file:org.apache.axis2.jaxws.message.databinding.JAXBContextFromClasses.java

/**
 * Utility class that quickly divides a list of classes into two categories.
 * The primary category classes contains classes that are referenced.
 * The secondary category classes are the remaining classes
 * @param original/*from w  ww. ja  v a2  s  .c  o m*/
 * @param primary
 * @param secondary
 */
static void separate(List<Class> original, List<Class> primary, List<Class> secondary, List<String> classRefs) {
    for (int i = 0; i < original.size(); i++) {
        Class cls = original.get(i);
        String clsName = cls.getCanonicalName();
        if (commonArrayClasses.contains(cls)) {
            if (log.isDebugEnabled()) {
                log.debug("This looks like a JAXB common class. Adding it to primary list:" + cls.getName());
            }
            primary.add(cls);
        } else if (classRefs.contains(clsName)) {
            if (log.isDebugEnabled()) {
                log.debug("This is a referenced class. Adding it to primary list:" + clsName);
            }
            Package pkg = cls.getPackage();
            if (pkg != null && pkg.getName().endsWith(".jaxws")) {
                if (log.isDebugEnabled()) {
                    log.debug(
                            "This looks like a jaxws generated Class. Adding it to the front of the primary list:"
                                    + cls.getName());
                }
                primary.add(0, cls); // Add to the front of the list
            } else {
                primary.add(cls);
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("This class is not referenced by the web service. Adding it to secondary list:"
                        + cls.getName());
            }
            Package pkg = cls.getPackage();
            if (pkg != null && pkg.getName().endsWith(".jaxws")) {
                if (log.isDebugEnabled()) {
                    log.debug(
                            "This looks like a jaxws generated Class. Adding it to the front of the secondary list:"
                                    + cls.getName());
                }
                secondary.add(0, cls); // Add to the front of the list
            } else {
                secondary.add(cls);
            }
        }
    }
}

From source file:org.apdplat.qa.util.Tools.java

public static String getAppPath(Class cls) {
    // ??/*from   w w  w .java  2  s.co  m*/
    if (cls == null) {
        throw new IllegalArgumentException("???");
    }
    ClassLoader loader = cls.getClassLoader();
    // ????
    String clsName = cls.getName() + ".class";
    // ?
    Package pack = cls.getPackage();
    String path = "";
    // ?????
    if (pack != null) {
        String packName = pack.getName();
        // ??JavaJDK
        if (packName.startsWith("java.") || packName.startsWith("javax.")) {
            throw new IllegalArgumentException("????");
        }
        // ??????
        clsName = clsName.substring(packName.length() + 1);
        // ?????????
        if (packName.indexOf(".") < 0) {
            path = packName + "/";
        } else {
            // ???????
            int start = 0, end = 0;
            end = packName.indexOf(".");
            while (end != -1) {
                path = path + packName.substring(start, end) + "/";
                start = end + 1;
                end = packName.indexOf(".", start);
            }
            path = path + packName.substring(start) + "/";
        }
    }
    // ClassLoadergetResource????
    URL url = loader.getResource(path + clsName);
    // URL??
    String realPath = url.getPath();
    // ?????"file:"
    int pos = realPath.indexOf("file:");
    if (pos > -1) {
        realPath = realPath.substring(pos + 5);
    }
    // ????
    pos = realPath.indexOf(path + clsName);
    realPath = realPath.substring(0, pos - 1);
    // JARJAR??
    if (realPath.endsWith("!")) {
        realPath = realPath.substring(0, realPath.lastIndexOf("/"));
    }
    /*------------------------------------------------------------  
     ClassLoadergetResourceutf-8??  
     ???  
     URLDecoderdecode?  
     ?  
     -------------------------------------------------------------*/
    try {
        realPath = URLDecoder.decode(realPath, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return realPath;
}

From source file:IntrospectionUtil.java

public static Field findField(Class clazz, String targetName, Class targetType, boolean checkInheritance,
        boolean strictType) throws NoSuchFieldException {
    if (clazz == null)
        throw new NoSuchFieldException("No class");
    if (targetName == null)
        throw new NoSuchFieldException("No field name");

    try {// ww  w .  j  a v  a  2  s .c om
        Field field = clazz.getDeclaredField(targetName);
        if (strictType) {
            if (field.getType().equals(targetType))
                return field;
        } else {
            if (field.getType().isAssignableFrom(targetType))
                return field;
        }
        if (checkInheritance) {
            return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), targetName, targetType,
                    strictType);
        } else
            throw new NoSuchFieldException("No field with name " + targetName + " in class " + clazz.getName()
                    + " of type " + targetType);
    } catch (NoSuchFieldException e) {
        return findInheritedField(clazz.getPackage(), clazz.getSuperclass(), targetName, targetType,
                strictType);
    }
}

From source file:org.eclipse.winery.common.Util.java

public static <T extends Object> JAXBElement<T> getJAXBElement(Class<T> clazz, T obj) {
    String namespace = null;//from   ww  w.j a  va 2  s .  c  om
    XmlRootElement xmlRootElement = clazz.getAnnotation(XmlRootElement.class);
    if (xmlRootElement != null) {
        namespace = xmlRootElement.namespace();
        if ("##default".equals(namespace)) {
            XmlSchema xmlSchema = clazz.getPackage().getAnnotation(XmlSchema.class);
            if (xmlSchema != null) {
                namespace = xmlSchema.namespace();
            } else {
                // trigger default handling
                namespace = null;
            }
        }
    }
    if (namespace == null) {
        // fallback non-specified namespaces
        namespace = org.eclipse.winery.common.constants.Namespaces.TOSCA_NAMESPACE;
    }
    String localName = Util.getLocalName(clazz);
    QName qname = new QName(namespace, localName);
    JAXBElement<T> rootElement = new JAXBElement<T>(qname, clazz, obj);
    return rootElement;
}