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.cleverbus.common.Tools.java

/**
 * Unmarshals XML into object graph.//from   ww w .j  ava  2s  .com
 *
 * @param xml the input string
 * @param targetClass the target class
 * @return object graph
 * @see XmlConverter
 */
@SuppressWarnings("unchecked")
public static <T> T unmarshalFromXml(String xml, Class<T> targetClass) {
    Jaxb2Marshaller jaxb2 = new Jaxb2Marshaller();
    jaxb2.setContextPath(targetClass.getPackage().getName());

    return (T) jaxb2.unmarshal(new StreamSource(new StringReader(xml)));
}

From source file:org.cleverbus.common.Tools.java

/**
 * Marshals object graph into XML./*from   ww  w  .j  a  va  2s  .  co  m*/
 *
 * @param obj the object graph
 * @param sourceClass the input class
 * @return XML as string
 * @see XmlConverter
 */
public static String marshalToXml(Object obj, Class sourceClass) {
    Jaxb2Marshaller jaxb2 = new Jaxb2Marshaller();
    jaxb2.setContextPath(sourceClass.getPackage().getName());

    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);

    jaxb2.marshal(obj, result);

    return writer.toString();
}

From source file:Main.java

/**
 * This method was added to address [JACKSON-53]: need to weed out
 * CGLib-injected "getCallbacks". /*  www .j ava  2  s  .  c om*/
 * At this point caller has detected a potential getter method
 * with name "getCallbacks" and we need to determine if it is
 * indeed injectect by Cglib. We do this by verifying that the
 * result type is "net.sf.cglib.proxy.Callback[]"
 *<p>
 * Also, see [JACKSON-177]; Hibernate may repackage cglib
 * it uses, so we better catch that too
 */
protected static boolean isCglibGetCallbacks(AnnotatedMethod am) {
    Class<?> rt = am.getRawType();
    // Ok, first: must return an array type
    if (rt == null || !rt.isArray()) {
        return false;
    }
    /* And that type needs to be "net.sf.cglib.proxy.Callback".
     * Theoretically could just be a type that implements it, but
     * for now let's keep things simple, fix if need be.
     */
    Class<?> compType = rt.getComponentType();
    // Actually, let's just verify it's a "net.sf.cglib.*" class/interface
    Package pkg = compType.getPackage();
    if (pkg != null) {
        String pname = pkg.getName();
        if (pname.startsWith("net.sf.cglib")
                // also, as per [JACKSON-177]
                || pname.startsWith("org.hibernate.repackage.cglib")) {
            return true;
        }
    }
    return false;
}

From source file:at.molindo.utils.reflect.ClassUtils.java

/**
 * @return the full classpath for the resource in the same package as scope
 *//*from  w w w .j a  v a2 s . c om*/
public static String getPackageResourcePath(Class<?> scope, String resource) {
    return scope.getPackage().getName().replace('.', '/') + '/' + resource;
}

From source file:com.imaginary.home.device.hue.Hue.java

static public @Nonnull Logger getLogger(@Nonnull Class<?> cls, @Nonnull String type) {
    String pkg = getLastItem(cls.getPackage().getName());

    if (pkg.equals("hue")) {
        pkg = "";
    } else {// w  w w.j ava2 s. c  om
        pkg = pkg + ".";
    }
    return Logger.getLogger("imaginary.hue." + type + "." + pkg + getLastItem(cls.getName()));
}

From source file:org.bremersee.sms.ExtensionUtils.java

private static JAXBContext getJaxbContext(Class<?> valueType) throws JAXBException {
    JAXBContext jaxbContext = JAXB_CONTEXTS.get(valueType.getPackage());
    if (jaxbContext == null) {
        jaxbContext = JAXB_CONTEXTS.get(valueType);
    }//from  ww  w .j a va  2  s  .  co m
    if (jaxbContext == null) {
        try {
            jaxbContext = JAXBContext.newInstance(valueType.getPackage().getName());
        } catch (JAXBException e) {
            jaxbContext = JAXBContext.newInstance(valueType);
            JAXB_CONTEXTS.put(valueType, jaxbContext);
        }
    }
    return jaxbContext;
}

From source file:org.rhq.bindings.util.InterfaceSimplifier.java

private static String getSimplifiedName(Class<?> interfaceClass) {
    String fullName = interfaceClass.getName();
    String simpleName = interfaceClass.getSimpleName();
    Package pkg = interfaceClass.getPackage();
    String packageName = (pkg != null) ? pkg.getName()
            : fullName.substring(0, fullName.length() - (simpleName.length() + 1));
    return packageName + ".wrapped." + simpleName + "Simple";
}

From source file:org.flowerplatform.tests.TestUtil.java

/**
 * @author Mariana Gheorghe/*from  w ww  .  ja  va2s .  com*/
 */
public static String getResourcesDir(Class<?> cls) {
    return "src/" + cls.getPackage().getName().replaceAll("\\.", "/") + "/resources/";
}

From source file:org.lunarray.model.descriptor.util.ReflectionUtil.java

/**
 * Gets all methods in a type, using tail recursion.
 * //from ww  w .ja  v  a2  s  .c o m
 * @param methods
 *            The tail. May not be null.
 * @param type
 *            The type. May not be null.
 * @param findStatic
 *            Set to true to find static methods, otherwise, only none
 *            static.
 * @param excludeJavaLang
 *            Whether or not to exclude methods from java.lang.*.
 */
public static void getMethods(final List<Method> methods, final Class<?> type, final boolean findStatic,
        final boolean excludeJavaLang) {
    Validate.notNull(methods, ReflectionUtil.TAIL_NULL);
    Validate.notNull(type, ReflectionUtil.TYPE_NULL);
    for (final Method method : type.getMethods()) {
        if ((findStatic == Modifier.isStatic(method.getModifiers())) && !method.isBridge() && ReflectionUtil
                .testPackage(method.getDeclaringClass(), ReflectionUtil.JAVA_LANG, excludeJavaLang)) {
            methods.add(method);
        }
    }
    final Class<?> superClass = type.getSuperclass();
    if (!CheckUtil.isNull(superClass)) {
        final String packageName = superClass.getPackage().getName();
        if (excludeJavaLang != ReflectionUtil.JAVA_LANG.equals(packageName)) {
            ReflectionUtil.getMethods(methods, superClass, findStatic, excludeJavaLang);
        }
    }
}

From source file:org.lunarray.model.descriptor.util.ReflectionUtil.java

/**
 * Test if the package is to be excluded.
 * /*from  w  w w. j  ava  2  s  .  c o  m*/
 * @param type
 *            The type to check.
 * @param matchPackageName
 *            The package name to check.
 * @param exclude
 *            Test if the exclude should be enabled.
 * @return True if the package matches the matchPackage and this is to be
 *         excluded.
 */
private static boolean testPackage(final Class<?> type, final String matchPackageName, final boolean exclude) {
    boolean result = false;
    final String packageName = type.getPackage().getName();
    if (matchPackageName.equals(packageName)) {
        if (!exclude) {
            result = true;
        }
    } else {
        result = true;
    }
    return result;
}