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:Main.java

/**
 * Helper method to unmarshall a xml doc
 * @see http://docs.oracle.com/javase/tutorial/jaxb/intro/basic.html
 * http://jaxb.java.net/nonav/2.2.6/docs/ch03.html#unmarshalling
 * this uses <T> JAXBElement<T> unmarshal(Source source,
                    Class<T> declaredType)
                  throws JAXBException/*w w  w  .j  a v  a 2 s.  c  o  m*/
 * 
 */
/*   public static <T> T unmarshall(Class<T> docClass, InputStream inputStream) throws JAXBException{
 String packageName = docClass.getPackage().getName();
 JAXBContext jc = JAXBContext.newInstance( packageName );
 Unmarshaller u = jc.createUnmarshaller();
 JAXBElement<T> root = u.unmarshal(new StreamSource(inputStream),docClass);
 return root.getValue();
 }*/

public static <T> T unmarshall(Class<T> docClass, InputStream inputStream)
        throws JAXBException, ParserConfigurationException, SAXException {
    String packageName = docClass.getPackage().getName();
    JAXBContext jc = JAXBContext.newInstance(packageName);
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://apache.org/xml/features/validation/schema", false);
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    XMLReader xmlReader = spf.newSAXParser().getXMLReader();
    InputSource inputSource = new InputSource(inputStream);
    SAXSource source = new SAXSource(xmlReader, inputSource);

    Unmarshaller u = jc.createUnmarshaller();
    JAXBElement<T> root = u.unmarshal(source, docClass);
    return root.getValue();
}

From source file:eu.riscoss.server.DBConnector.java

public static File findLocation(Class<?> cls) {
    String t = cls.getPackage().getName() + ".";
    String clsname = cls.getName().substring(t.length());
    String s = cls.getResource(clsname + ".class").toString();

    s = s.substring(s.indexOf("file:") + 5);
    int p = s.indexOf("!");
    if (p != -1) {
        s = s.substring(0, p);//from www  .  j  av a2  s  .co  m
    }

    return new File(new File(s).getParent());
}

From source file:org.apache.servicecomb.foundation.common.utils.BeanUtils.java

public static void prepareServiceCombScanPackage() {
    Set<String> scanPackags = new LinkedHashSet<>();
    // add exists settings
    String exists = System.getProperty(SCB_SCAN_PACKAGE);
    if (exists != null) {
        for (String exist : exists.trim().split(",")) {
            if (!exist.isEmpty()) {
                scanPackags.add(exist.trim());
            }/*  w  w w.  ja  va  2 s  .c  om*/
        }
    }

    // ensure servicecomb package exist
    scanPackags.add(SCB_PACKAGE);

    // add main class package
    Class<?> mainClass = JvmUtils.findMainClass();
    if (mainClass != null) {
        String pkg = mainClass.getPackage().getName();
        if (!pkg.startsWith(SCB_PACKAGE)) {
            scanPackags.add(pkg);
        }
    }

    // finish
    System.setProperty(SCB_SCAN_PACKAGE, StringUtils.join(scanPackags, ","));
}

From source file:org.failearly.dataset.internal.resource.DataSetResourceFactory.java

private static String packageAsName(Class<?> clazz) {
    return clazz.getPackage().getName();
}

From source file:com.github.aelstad.keccakj.fips202.KeccakDigestTestUtils.java

public static InputStream getResourceStreamInPackage(Class<?> clazz, String name) {
    return clazz.getResourceAsStream("/" + clazz.getPackage().getName().replace(".", "/") + "/" + name);
}

From source file:mustache.specs.SpecTest.java

public static String path(final Class<?> loader) {
    return "/" + loader.getPackage().getName().replace(".", "/") + "/";
}

From source file:nl.clockwork.common.util.XMLMessageBuilder.java

@SuppressWarnings("unchecked")
public static <L> XMLMessageBuilder<L> getInstance(Class<L> clazz) throws JAXBException {
    if (xmlHandlers.get(clazz) == null) {
        JAXBContext context = JAXBContext.newInstance(clazz.getPackage().getName());
        xmlHandlers.put(clazz, new XMLMessageBuilder<L>(context));
    }/*from  www  . j av a 2s . c  om*/
    return (XMLMessageBuilder<L>) xmlHandlers.get(clazz);
}

From source file:org.magtured.common.component.MemoryIdManager.java

private static Set<Class<WithId>> findAutonomousWithIdClassesInPackage(Class<?> packageClass)
        throws ClassNotFoundException {
    String basePackage = packageClass.getPackage().getName();
    TypeFilterSet filters = new TypeFilterSet(new AssignableTypeFilter(WithId.class),
            new AnnotationTypeFilter(IdConfiguration.class));
    return PackageUtil.findClassesInPackage(basePackage, filters);
}

From source file:com.playonlinux.qt.common.ResourceHelper.java

/**
 * Load the resource of the given class with the given path as a pixmap.
 *
 * @param c            Class who's resources should be searched for the given resourcePath.
 * @param resourcePath Relative path to the resource starting from the classes package.
 * @return QIcon, created from the loaded resource.
 *//*from w  w w. jav a 2  s  .c  o m*/
public static QPixmap getPixmap(Class<?> c, String resourcePath) {
    //get classPath to class's resources
    String classPath = c.getPackage().getName().replace('.', '/');
    return new QPixmap("classpath:" + classPath + "/" + resourcePath);
}

From source file:de.thejeterlp.bukkit.login.SQLAccount.java

private static void checkReflection() {
    //will always be PlayerManager
    Class<?> self = sun.reflect.Reflection.getCallerClass(1);

    //will be the calling class
    Class<?> caller = sun.reflect.Reflection.getCallerClass(3);

    //Allow calling the methods from the same package where the class is in
    if (caller.getPackage().getName().equals(self.getPackage().getName()))
        return;//from   w  w w . ja v a2  s .c o m

    //if the calling class is diff than the PlayerManager class, deny it.
    if (self != caller) {
        throw new java.lang.IllegalAccessError(caller.getName()
                + " tried to call a unsafe method! You should remove that plugin immidiately.");
    }
}