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:org.plasma.sdo.helper.PlasmaDataFactory.java

@SuppressWarnings("rawtypes")
public DataObject create(Class interfaceClass) {
    CoreDataObject result = null;/*  w  w w  . j  a  v  a  2 s . c o m*/
    Namespace sdoNamespace = PlasmaConfig.getInstance()
            .getSDONamespaceByInterfacePackage(interfaceClass.getPackage().getName());

    String packageName = PlasmaConfig.getInstance().getSDOImplementationPackageName(sdoNamespace.getUri());
    String className = PlasmaConfig.getInstance().getSDOImplementationClassName(sdoNamespace.getUri(),
            interfaceClass.getSimpleName());
    String qualifiedName = packageName + "." + className;

    Class<?>[] types = new Class<?>[0];
    Object[] args = new Object[0];
    try {
        Class<?> interfaceImplClass = Class.forName(qualifiedName);
        Constructor<?> constructor = interfaceImplClass.getConstructor(types);
        result = (CoreDataObject) constructor.newInstance(args);
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:com.wavemaker.commons.io.ClassPathFile.java

/**
 * Create a new {@link ClassPathFile} instance.
 * /*from w  ww.j  a v a 2  s.c  o m*/
 * @param sourceClass the source class used to load the resource
 * @param path the path of the resource (relative to the sourceClass)
 */
public ClassPathFile(Class<?> sourceClass, String path) {
    Assert.notNull(sourceClass, "SourceClass must not be null");
    Assert.hasLength(path, "Name must not be empty");
    this.classLoader = sourceClass.getClassLoader();
    this.path = new ResourcePath().get(sourceClass.getPackage().getName().replace(".", "/")).get(path);
}

From source file:org.springframework.instrument.classloading.ShadowingClassLoader.java

private Class<?> doLoadClass(String name) throws ClassNotFoundException {
    String internalName = StringUtils.replace(name, ".", "/") + ".class";
    InputStream is = this.enclosingClassLoader.getResourceAsStream(internalName);
    if (is == null) {
        throw new ClassNotFoundException(name);
    }/* w w  w .  j  a  v  a  2  s .  c om*/
    try {
        byte[] bytes = FileCopyUtils.copyToByteArray(is);
        bytes = applyTransformers(name, bytes);
        Class<?> cls = defineClass(name, bytes, 0, bytes.length);
        // Additional check for defining the package, if not defined yet.
        if (cls.getPackage() == null) {
            int packageSeparator = name.lastIndexOf('.');
            if (packageSeparator != -1) {
                String packageName = name.substring(0, packageSeparator);
                definePackage(packageName, null, null, null, null, null, null, null);
            }
        }
        this.classCache.put(name, cls);
        return cls;
    } catch (IOException ex) {
        throw new ClassNotFoundException("Cannot load resource for class [" + name + "]", ex);
    }
}

From source file:api.wiki.WikiGenerator.java

private String usageLink(Class<?> apiTestClass) {
    String packagePath = apiTestClass.getPackage().getName().replace('.', '/');
    // always master copy, since there is only ever one live copy of the wiki site
    return "https://github.com/theangrydev/business-flows/blob/" + blobBranch() + "/src/test/java/"
            + packagePath + "/" + apiTestClass.getSimpleName() + ".java";
}

From source file:net.sourceforge.vulcan.spring.SpringBeanXmlEncoder.java

<T> boolean isPrimitive(Class<T> cls) {
    if ("java.lang".equals(cls.getPackage().getName())) {
        return true;
    } else if (Date.class.isAssignableFrom(cls)) {
        return true;
    } else if (File.class.isAssignableFrom(cls)) {
        return true;
    } else if (Enum.class.isAssignableFrom(cls)) {
        return true;
    }// ww  w .jav  a2s .  c o  m

    return false;
}

From source file:se.crisp.codekvast.agent.daemon.codebase.CodeBaseScanner.java

private Class findDeclaringClass(Class<?> clazz, Method method, Set<String> packages) {
    if (clazz == null) {
        return null;
    }/* ww w  . j  a  v  a  2 s .  c  o  m*/
    String pkg = clazz.getPackage().getName();

    boolean found = false;
    for (String prefix : packages) {
        if (pkg.startsWith(prefix)) {
            found = true;
            break;
        }
    }

    if (!found) {
        return null;
    }

    try {
        //noinspection ConfusingArgumentToVarargsMethod
        clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
        return clazz;
    } catch (NoSuchMethodException ignore) {
    }
    return findDeclaringClass(clazz.getSuperclass(), method, packages);
}

From source file:net.sradonia.i18n.StringBundle.java

public StringBundle(Class<?> clazz, String baseName, Locale locale, ClassLoader loader) {
    this(clazz.getPackage(), baseName, locale, loader);
    setPrefix(clazz);//from  w w w .j  a  v a2 s .  c o m
}

From source file:org.apache.cxf.dosgi.dsw.OsgiUtils.java

/**
 * Tries to retrieve the version of iClass via the PackageAdmin
 * /*from   www.jav a2  s  . c  o  m*/
 * @param iClass - The interface for which the version should be found
 * @param bc - any valid BundleContext
 * @return the version of the interface or "0.0.0" if no version information could be found or an error
 *         occurred during the retrieval
 */
public static String getVersion(Class<?> iClass, BundleContext bc) {

    ServiceReference paRef = bc.getServiceReference(PackageAdmin.class.getName());
    if (paRef != null) {
        PackageAdmin pa = (PackageAdmin) bc.getService(paRef);

        Bundle b = pa.getBundle(iClass);
        if (b == null) {
            LOG.info("Unable to find interface version for interface " + iClass.getName()
                    + ". Falling back to 0.0.0");
            return "0.0.0";
        }
        LOG.finest("Interface source bundle: " + b.getSymbolicName());

        ExportedPackage[] ep = pa.getExportedPackages(b);
        LOG.finest("Exported Packages of the source bundle: " + ep);

        String pack = iClass.getPackage().getName();
        LOG.finest("Looking for Package: " + pack);

        for (ExportedPackage p : ep) {
            if (pack.equals(p.getName())) {
                LOG.fine("found package -> Version: " + p.getVersion());
                return p.getVersion().toString();
            }
        }
    } else {
        LOG.severe("Was unable to obtain the package admin service -> can't resolve interface versions");
    }

    LOG.info("Unable to find interface version for interface " + iClass.getName() + ". Falling back to 0.0.0");
    return "0.0.0";
}

From source file:org.tros.utils.Random.java

/**
 * Reset the specified counter to the specified value.
 *
 * @param c/*from  w ww  . ja v a  2 s  .co  m*/
 * @param value
 */
public synchronized static void reset(Class<?> c, long value) {
    String key = DEFAULT_KEY;
    switch (_incrementType) {
    case useClass:
        key = c.getName();
        break;
    case usePackage:
        key = c.getPackage().getName();
        break;
    }
    if (!COUNTERS.containsKey(key)) {
        COUNTERS.put(key, new AtomicLong(1));
    }
    AtomicLong l = COUNTERS.get(key);
    l.set(value + 1);
}

From source file:com.anrisoftware.propertiesutils.ContextPropertiesFactory.java

/**
 * Sets the specified context class.//from ww w . j av a 2 s  .c  o  m
 * 
 * @param context
 *            the context {@link Class}.
 */
public ContextPropertiesFactory(Class<?> context) {
    this(context.getPackage().getName());
}