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:net.sf.firemox.xml.XmlConfiguration.java

/**
 * Return the method name corresponding to the specified TAG.
 * /* www.j a  va2 s .  co  m*/
 * @param tagName
 * @return the method name corresponding to the specified TAG.
 */
static XmlToMDB getXmlClass(String tagName, Map<String, XmlToMDB> instances, Class<?> nameSpaceCall) {
    if (!nameSpaceCall.getSimpleName().startsWith("Xml"))
        throw new InternalError("Caller should be an Xml class : " + nameSpaceCall);

    XmlToMDB nodeClass = instances.get(tagName);
    if (nodeClass != null) {
        return nodeClass;
    }

    String simpleClassName = StringUtils.capitalize(tagName.replaceAll("-", ""));
    String packageName = nameSpaceCall.getPackage().getName();
    String namespace = nameSpaceCall.getSimpleName().substring(3).toLowerCase();
    String className = packageName + "." + namespace + "." + simpleClassName;
    XmlToMDB result;
    try {
        result = (XmlToMDB) Class.forName(className).newInstance();
    } catch (Throwable e) {
        Class<?> mdbClass = null;
        simpleClassName = WordUtils.capitalize(tagName.replaceAll("-", " ")).replaceAll(" ", "");
        try {
            result = (XmlToMDB) Class.forName(packageName + "." + namespace + "." + simpleClassName)
                    .newInstance();
        } catch (Throwable e1) {
            try {
                className = StringUtils.chomp(packageName, ".xml") + "." + namespace + "." + simpleClassName;
                mdbClass = Class.forName(className);
                if (!mdbClass.isAnnotationPresent(XmlTestElement.class)) {
                    result = (XmlToMDB) mdbClass.newInstance();
                } else {
                    result = getAnnotedBuilder(mdbClass, tagName, packageName, namespace);
                }
            } catch (Throwable ei2) {
                error("Unsupported " + namespace + " '" + tagName + "'");
                result = DummyBuilder.instance();
            }
        }
    }
    instances.put(tagName, result);
    return result;
}

From source file:IntrospectionUtil.java

protected static Method findInheritedMethod(Package pack, Class clazz, String methodName, Class[] args,
        boolean strictArgs) throws NoSuchMethodException {
    if (clazz == null)
        throw new NoSuchMethodException("No class");
    if (methodName == null)
        throw new NoSuchMethodException("No method name");

    Method method = null;/*from w ww.java2 s. c  o  m*/
    Method[] methods = clazz.getDeclaredMethods();
    for (int i = 0; i < methods.length && method == null; i++) {
        if (methods[i].getName().equals(methodName) && isInheritable(pack, methods[i])
                && checkParams(methods[i].getParameterTypes(), args, strictArgs))
            method = methods[i];
    }
    if (method != null) {
        return method;
    } else
        return findInheritedMethod(clazz.getPackage(), clazz.getSuperclass(), methodName, args, strictArgs);
}

From source file:com.evolveum.midpoint.prism.marshaller.PrismBeanInspector.java

public static QName determineTypeForClassUncached(Class<?> beanClass) {
    XmlType xmlType = beanClass.getAnnotation(XmlType.class);
    if (xmlType == null) {
        return null;
    }//from  w w  w. j av a2s.  c  om

    String namespace = xmlType.namespace();
    if (BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) {
        XmlSchema xmlSchema = beanClass.getPackage().getAnnotation(XmlSchema.class);
        namespace = xmlSchema.namespace();
    }
    if (StringUtils.isBlank(namespace) || BeanMarshaller.DEFAULT_PLACEHOLDER.equals(namespace)) {
        return null;
    }

    return new QName(namespace, xmlType.name());
}

From source file:com.wit.and.dialog.manage.DialogFactory.java

/**
 * <p>//w  w  w  . j  a  va2  s.c o m
 * Creates dialog tag in required format depends on the passed
 * <var>factory</var> package name and <var>dialogName</var>.
 * </p>
 * <p>
 * Example format: <u>com.android.app.dialog.factories.ScreenLoginFactory.ConfirmDialog.TAG</u><br/><br/>
 * - where <b>com.android.app.dialog.factories</b> is name of package where <var>factory</var> is placed,
 * <b>ScreenLoginFactory</b> is factory class name, <b>ConfirmDialog</b> is <var>dialogName</var> and
 * <b>TAG</b> is tag identifier.
 * </p>
 *
 * @param factoryClass Class of factory for which is creation of dialog tag requested.
 * @param dialogName   Dialog name (can be dialog class name) for which tag should be created.
 * @return Dialog tag in required format, or <code>null</code> if <var>dialogName</var> is <code>null</code> or empty.
 */
public static String createDialogTag(Class<? extends DialogManager.IDialogFactory> factoryClass,
        String dialogName) {
    // Only valid dialog name is allowed.
    if (dialogName == null || dialogName.length() == 0) {
        return null;
    }
    return factoryClass.getPackage().getName() + "." + factoryClass.getSimpleName() + "." + dialogName + ".TAG";
}

From source file:org.apache.axis2.jaxws.utility.XMLRootElementUtil.java

/**
 * @param clazz/*from ww  w .j av a2 s. c om*/
 * @return namespace of @XmlType or null if this is object does not represent a root element
 */
public static QName getXmlTypeQName(Class clazz) {

    // See if the object has an @XmlType
    XmlType t = (XmlType) getAnnotation(clazz, XmlType.class);
    if (t == null) {
        return null;
    }

    String name = t.name();
    String namespace = t.namespace();

    // The name may need to be defaulted
    if (name == null || name.length() == 0 || name.equals("##default")) {
        name = getSimpleName(clazz.getCanonicalName());
    }

    // The namespace may need to be defaulted
    if (namespace == null || namespace.length() == 0 || namespace.equals("##default")) {
        Package pkg = clazz.getPackage();
        XmlSchema schema = (XmlSchema) getAnnotation(pkg, XmlSchema.class);
        if (schema != null) {
            namespace = schema.namespace();
        } else {
            namespace = "";
        }
    }

    return new QName(namespace, name);
}

From source file:cross.applicationContext.ReflectionApplicationContextGenerator.java

/**
 * Creates an xml element using the given document for element creation.
 *
 * @param generator/*from  w  w w  . j  a  v a2 s.c  o m*/
 * @param document the document use for element creation
 * @param beanDescriptor
 * @return the modified element
 */
public static Element createElement(ReflectionApplicationContextGenerator generator, Document document,
        BeanDescriptor beanDescriptor) {
    Class<?> javaClass = beanDescriptor.clazz;
    System.out.println("Building bean element for " + javaClass.getName());
    //get the name of the class
    String className = javaClass.getSimpleName();

    //get the name of the package
    String packageName = javaClass.getPackage().getName();

    //create <bean /> element
    Element beanElement = document.createElement("bean");
    String classNameLower = toLowerCaseName(className);
    beanElement.setAttribute("id", classNameLower);
    String classAttr = (packageName == null) ? className : packageName + "." + className;
    beanElement.setAttribute("class", classAttr);
    beanElement.setAttribute("scope", beanDescriptor.scope);
    //constructors are not supported
    //get all the class' properties from the public fields and setter methods.

    for (Method method : javaClass.getMethods()) {
        checkMutableProperties(method, javaClass, beanDescriptor.obj, beanDescriptor.properties);
    }
    //sort by name
    Collections.sort(beanDescriptor.properties, new Comparator<ObjectProperty>() {
        @Override
        public int compare(ObjectProperty t, ObjectProperty t1) {
            return t.name.compareTo(t1.name);
        }
    });
    List<String> blackList = Arrays.asList("workflow", "progress", "cvResolver");
    //add all properties as <property /> elements
    for (ObjectProperty p : beanDescriptor.properties) {
        if (!blackList.contains(p.name)) {
            Element propertyElement = document.createElement("property");
            propertyElement.setAttribute("name", p.name);
            Comment propertyCommentElement = document
                    .createComment(AnnotationInspector.getDescriptionFor(javaClass, p.name));
            boolean append = true;
            if (p.type.startsWith("java.lang.")) {
                String shortType = p.type.substring("java.lang.".length());
                if (primitives.contains(shortType) || wrappers.contains(shortType)) {
                    propertyElement.setAttribute("value", p.value);
                }
            } else if (primitives.contains(p.type) || wrappers.contains(p.type)) {
                propertyElement.setAttribute("value", p.value);
            } else if ("Array".equals(p.type) || "List".equals(p.type) || "java.util.List".equals(p.type)) {
                Element listElement = document.createElement("list");
                String genericType = p.genericType;
                propertyElement.appendChild(listElement);
            } else if ("Set".equals(p.type) || "java.util.Set".equals(p.type)) {
                Element listElement = document.createElement("set");
                propertyElement.appendChild(listElement);
            } else if ("Map".equals(p.type) || "java.util.Map".equals(p.type)) {
                Element listElement = document.createElement("map");
                propertyElement.appendChild(listElement);
            } else if ("Properties".equals(p.type) || "java.util.Properties".equals(p.type)) {
                Element listElement = document.createElement("props");
                propertyElement.appendChild(listElement);
            } else {
                try {
                    //                    System.err.println("Skipping ref!");
                    Set<BeanDefinition> beanDefinitions = getImplementationsOf(Class.forName(p.type), "cross",
                            "maltcms", "net.sf.maltcms");
                    BeanDefinition first = null;
                    for (BeanDefinition bd : beanDefinitions) {
                        generator.addBean(bd.getBeanClassName());
                        if (first == null) {
                            first = bd;
                        }
                    }
                    if (first != null) {
                        String simpleName = first.getBeanClassName()
                                .substring(first.getBeanClassName().lastIndexOf(".") + 1);
                        propertyElement.setAttribute("ref",
                                generator.classToElement.get(toLowerCaseName(simpleName)).id);
                    }
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ReflectionApplicationContextGenerator.class.getName()).log(Level.SEVERE,
                            null, ex);
                }
                append = true;
                //                    try {
                //                        generator.addBean(p.type);
                //                        Class<?> c = Class.forName(p.type);
                //                        List<Object> objects = generator.classToObject.get(c);
                //                        if (objects != null && !objects.isEmpty()) {
                //                            propertyElement.setAttribute("ref", generator.buildBeanElement(objects.get(0)).id);
                //                        } else {
                //                            append = false;
                //                        }
                //                    } catch (ClassNotFoundException ex) {
                //                        Logger.getLogger(ReflectionApplicationContextGenerator.class.getName()).log(Level.SEVERE, null, ex);
                //                    }

            }
            if (append) {
                beanElement.appendChild(propertyCommentElement);
                beanElement.appendChild(propertyElement);
            } else {
                beanElement.appendChild(propertyCommentElement);
                Comment comment = document.createComment("<property name=\"" + p.name + "\" ref=\"\"/>");
                beanElement.appendChild(comment);
            }
        }
    }
    return beanElement;
}

From source file:org.apache.axis2.jaxws.utility.XMLRootElementUtil.java

/**
 * @param clazz/* www  . ja v  a 2 s .co  m*/
 * @return namespace of root element qname or null if this is not object does not represent a
 *         root element
 */
public static QName getXmlRootElementQName(Class clazz) {

    // See if the object represents a root element
    XmlRootElement root = (XmlRootElement) getAnnotation(clazz, XmlRootElement.class);
    if (root == null) {
        return null;
    }

    String name = root.name();
    String namespace = root.namespace();

    // The name may need to be defaulted
    if (name == null || name.length() == 0 || name.equals("##default")) {
        name = getSimpleName(clazz.getCanonicalName());
    }

    // The namespace may need to be defaulted
    if (namespace == null || namespace.length() == 0 || namespace.equals("##default")) {
        Package pkg = clazz.getPackage();
        XmlSchema schema = (XmlSchema) getAnnotation(pkg, XmlSchema.class);
        if (schema != null) {
            namespace = schema.namespace();
        } else {
            namespace = "";
        }
    }

    return new QName(namespace, name);
}

From source file:org.dcm4che.tool.common.CLIUtils.java

public static CommandLine parseComandLine(String[] args, Options opts, ResourceBundle rb2, Class<?> clazz)
        throws ParseException {
    CommandLineParser parser = new PosixParser();
    CommandLine cl = parser.parse(opts, args);
    if (cl.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(rb2.getString("usage"), rb2.getString("description"), opts,
                rb2.getString("example"));
        System.exit(0);//from   w  ww.  ja  v  a2  s. c o  m
    }
    if (cl.hasOption("V")) {
        Package p = clazz.getPackage();
        String s = p.getName();
        System.out.println(s.substring(s.lastIndexOf('.') + 1) + ": " + p.getImplementationVersion());
        System.exit(0);
    }
    return cl;
}

From source file:org.tdar.core.service.ReflectionService.java

public static List<Field> findAnnotatedFieldsOfClass(Class<?> cls,
        Class<? extends Annotation> annotationClass) {
    List<Field> result = new ArrayList<>();
    // iterate up the package hierarchy
    Class<?> actualClass = null;
    while (cls.getPackage().getName().startsWith(ORG_TDAR)) {
        // find first implemented tDAR class (actual class);
        if (actualClass == null) {
            actualClass = cls;//w  ww  . ja  v a 2s.co  m
        }
        for (Field field : cls.getDeclaredFields()) {
            Object annotation = field.getAnnotation(annotationClass);
            if (annotation != null) {
                result.add(field);
            }
        }
        cls = cls.getSuperclass();
    }
    return result;
}

From source file:grails.util.GrailsClassUtils.java

/**
 * Returns whether the specified class is either within one of the specified packages or
 * within a subpackage of one of the packages
 *
 * @param theClass The class//ww  w .j a v a 2s  .com
 * @param packageList The list of packages
 * @return true if it is within the list of specified packages
 */
public static boolean isClassBelowPackage(Class<?> theClass, List<?> packageList) {
    String classPackage = theClass.getPackage().getName();
    for (Object packageName : packageList) {
        if (packageName != null) {
            if (classPackage.startsWith(packageName.toString())) {
                return true;
            }
        }
    }
    return false;
}