Example usage for java.lang Class getAnnotations

List of usage examples for java.lang Class getAnnotations

Introduction

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

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:org.apache.axis2.jaxws.marshaller.impl.alt.DocLitWrappedPlusMethodMarshaller.java

public Object[] demarshalRequest(Message message, OperationDescription operationDesc)
        throws WebServiceException {

    if (log.isDebugEnabled()) {
        log.debug("Calling DocLitWrapperPlusMethodMarshaller.demarshalRequest");
        log.debug(/*from w w w .j  av a  2s. co m*/
                "  The DocLitWrapped Plus marshaller is used when the web service method deviates from the normal doc/lit rules.");
    }
    EndpointInterfaceDescription ed = operationDesc.getEndpointInterfaceDescription();
    EndpointDescription endpointDesc = ed.getEndpointDescription();

    // Note all exceptions are caught and rethrown with a WebServiceException
    try {
        // Sample Document message
        // ..
        // <soapenv:body>
        //    <m:operation>
        //      <param>hello</param>
        //    </m:operation>
        // </soapenv:body>
        //
        // Important points.
        //   1) There is no operation element under the body.
        //   2) The data blocks are located underneath the body.  
        //   3) The name of the data block (m:operation) is defined by the schema and match the name of the operation.
        //      This is called the wrapper element.  The wrapper element has a corresponding JAXB element pojo.
        //   4) The parameters (m:param) are child elements of the wrapper element.
        ParameterDescription[] pds = operationDesc.getParameterDescriptions();
        MarshalServiceRuntimeDescription marshalDesc = MethodMarshallerUtils.getMarshalDesc(endpointDesc);
        TreeSet<String> packages = marshalDesc.getPackages();
        String packagesKey = marshalDesc.getPackagesKey();

        // In usage=WRAPPED, there will be a single JAXB block inside the body.
        // Get this block
        JAXBBlockContext blockContext = new JAXBBlockContext(packages, packagesKey);
        blockContext.setWebServiceNamespace(ed.getTargetNamespace());

        // If the wrapper is not a root element, then the process type
        // must be set on the context so that "by type" unmarshal is performed.
        if (!isRequestWrapperAnXmlRootElement(operationDesc, marshalDesc, endpointDesc)) {
            String clsName = marshalDesc.getRequestWrapperClassName(operationDesc);
            Class cls = loadClass(clsName, endpointDesc);
            blockContext.setProcessType(cls);
        }
        JAXBBlockFactory factory = (JAXBBlockFactory) FactoryRegistry.getFactory(JAXBBlockFactory.class);
        Block block = message.getBodyBlock(blockContext, factory);
        Object wrapperObject = block.getBusinessObject(true);

        // The child elements are within the object that 
        // represents the type
        if (wrapperObject instanceof JAXBElement) {
            wrapperObject = ((JAXBElement) wrapperObject).getValue();
        }

        // Use the wrapper tool to get the child objects.
        JAXBWrapperTool wrapperTool = new JAXBWrapperToolImpl();

        // Get the list of names for the input parameters
        List<String> xmlNames = new ArrayList<String>();
        List<ParameterDescription> pdList = new ArrayList<ParameterDescription>();
        for (int i = 0; i < pds.length; i++) {
            ParameterDescription pd = pds[i];
            if (pd.getMode() == Mode.IN || pd.getMode() == Mode.INOUT) {
                if (!pd.isHeader()) {
                    // Only the body parameters will be present in the wrapper object
                    xmlNames.add(pd.getParameterName());
                }
                pdList.add(pd);
            }

        }

        // Get the child objects
        Object[] objects = wrapperTool.unWrap(wrapperObject, xmlNames,
                marshalDesc.getPropertyDescriptorMap(wrapperObject.getClass()));

        // Now create a list of paramValues 
        List<PDElement> pvList = new ArrayList<PDElement>();
        int bodyIndex = 0;
        for (int i = 0; i < pdList.size(); i++) {
            ParameterDescription pd = pdList.get(i);
            if (!pd.isHeader()) {
                // Normal case: Get the parameter value from the list of objects
                Object value = objects[bodyIndex];

                //  The object in the PDElement must be an element
                QName qName = new QName(pd.getTargetNamespace(), pd.getPartName());
                Element element = null;
                if (!marshalDesc.getAnnotationDesc(pd.getParameterActualType()).hasXmlRootElement()) {
                    element = new Element(value, qName, pd.getParameterActualType());
                } else {
                    element = new Element(value, qName);
                }
                pvList.add(new PDElement(pd, element, null));
                bodyIndex++;
            } else {
                // Header
                // Get the Block from the header
                String localName = pd.getParameterName();
                JAXBBlockContext blkContext = new JAXBBlockContext(packages, marshalDesc.getPackagesKey());
                // Set up "by java type" unmarshalling if necessary
                if (blkContext.getConstructionType() != JAXBUtils.CONSTRUCTION_TYPE.BY_CONTEXT_PATH) {
                    Class actualType = pd.getParameterActualType();
                    if (MethodMarshallerUtils.isNotJAXBRootElement(actualType, marshalDesc)) {
                        blkContext.setProcessType(actualType);
                    } else {
                        Annotation annos[] = actualType.getAnnotations();
                        if (annos == null || annos.length == 0) {
                            blkContext.setProcessType(actualType);
                            blkContext.setIsxmlList(pd.isListType());
                        }
                    }
                }
                block = message.getHeaderBlock(pd.getTargetNamespace(), localName, blkContext, factory);
                Object value = block.getBusinessObject(true);
                Element element = new Element(value, new QName(pd.getTargetNamespace(), localName));
                pvList.add(new PDElement(pd, element, blkContext.getProcessType()));
            }

        }

        // Build the signature arguments
        Object[] sigArguments = MethodMarshallerUtils.createRequestSignatureArgs(pds, pvList);

        return sigArguments;
    } catch (Exception e) {
        throw ExceptionFactory.makeWebServiceException(e);
    }
}

From source file:org.grails.datastore.mapping.model.config.GormMappingConfigurationStrategy.java

/**
 * Tests whether an class is a persistent entity
 *
 * Based on the same method in Grails core within the DomainClassArtefactHandler class
 * @param clazz The java class/*www . j  a v  a 2 s  . c  o  m*/
 *
 * @return True if it is a persistent entity
 */
public boolean isPersistentEntity(Class clazz) {
    // its not a closure
    if (clazz == null)
        return false;
    if (Closure.class.isAssignableFrom(clazz)) {
        return false;
    }
    if (Enum.class.isAssignableFrom(clazz))
        return false;
    if (clazz.isAnnotationPresent(Entity.class)) {
        return true;
    }
    // this is done so we don't need a statically typed reference to the Grails annotation
    for (Annotation annotation : clazz.getAnnotations()) {
        if (annotation.toString().equals("@grails.persistence.Entity()"))
            return true;
    }
    Class testClass = clazz;
    boolean result = false;
    while (testClass != null && !testClass.equals(GroovyObject.class) && !testClass.equals(Object.class)) {
        try {
            // make sure the identify and version field exist
            testClass.getDeclaredField(IDENTITY_PROPERTY);
            testClass.getDeclaredField(VERSION_PROPERTY);

            // passes all conditions return true
            result = true;
            break;
        } catch (SecurityException e) {
            // ignore
        } catch (NoSuchFieldException e) {
            // ignore
        }
        testClass = testClass.getSuperclass();
    }
    return result;
}

From source file:org.castor.jaxb.reflection.ClassInfoBuilder.java

/**
 * Build the ClassInfo representation for a Class.
 * /* www. j  av  a2 s  . c o  m*/
 * @param type
 *            the Class to introspect
 * @return ClassInfo build from the Class
 */
public ClassInfo buildClassInfo(final Class<?> type) {
    if (type == null) {
        String message = "Argument type must not be null.";
        LOG.warn(message);
        throw new IllegalArgumentException(message);
    }
    if (!isDescribeable(type)) {
        if (LOG.isDebugEnabled()) {
            String message = "Class: " + type + " cannot be described using this builder.";
            LOG.debug(message);
        }
        return null;
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("Now starting to build ClassInfo for: " + type);
    }

    ClassInfo classInfo = createClassInfo(javaNaming.getClassName(type));
    JaxbClassNature jaxbClassNature = new JaxbClassNature(classInfo);

    jaxbClassNature.setType(type);
    jaxbClassNature.setSupertype(type.getSuperclass());
    jaxbClassNature.setInterfaces(type.getInterfaces());
    jaxbClassNature.setHasPublicEmptyConstructor(hasPublicEmptyConstructor(type));

    Annotation[] unprocessedClassAnnotations = this.classAnnotationProcessingService
            .processAnnotations(jaxbClassNature, type.getAnnotations());
    for (Field field : type.getDeclaredFields()) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Now evaluating field: " + field);
        }
        if (isDescribeable(type, field)) {
            buildFieldInfo(classInfo, field);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Ignoring field: " + field + " of type: " + type.getName()
                        + " it is not useable for mapping.");
            }
        }
    }
    for (Method method : type.getDeclaredMethods()) {
        if (LOG.isInfoEnabled()) {
            LOG.info("Now evaluating method: " + method);
        }
        if (isDescribeable(type, method)) {
            buildFieldInfo(classInfo, method);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Ignoring method: " + method + " of type: " + type.getName()
                        + " it is not useable for mapping.");
            }
        }
    }
    PackageInfo pi = buildPackageInfo(type.getPackage());
    classInfo.setPackageInfo(pi);
    if (LOG.isInfoEnabled()) {
        LOG.info("ClassInfo for: " + type + " build is: " + classInfo);
    }
    return classInfo;
}

From source file:org.integratedmodelling.thinklab.plugin.ThinklabPlugin.java

protected void loadRESTHandlers() throws ThinklabException {

    String ipack = this.getClass().getPackage().getName() + ".rest";

    for (Class<?> cls : MiscUtilities.findSubclasses(IRESTHandler.class, ipack, getClassLoader())) {

        /*/*  w ww .ja v a  2  s .c o m*/
         * lookup annotation, ensure we can use the class
         */
        if (cls.isInterface() || Modifier.isAbstract(cls.getModifiers()))
            continue;

        for (Annotation annotation : cls.getAnnotations()) {
            if (annotation instanceof RESTResourceHandler) {

                String path = ((RESTResourceHandler) annotation).path();
                String description = ((RESTResourceHandler) annotation).description();
                RESTManager.get().registerService(path, (Class<?>) cls);

                break;
            }
        }
    }

}

From source file:org.integratedmodelling.thinklab.plugin.ThinklabPlugin.java

protected void loadKnowledgeImporters() {

    String ipack = this.getClass().getPackage().getName() + ".importers";

    for (Class<?> cls : MiscUtilities.findSubclasses(IKnowledgeImporter.class, ipack, getClassLoader())) {

        /*// w  ww. java 2s .  c o m
         * lookup annotation, ensure we can use the class
         */
        if (cls.isInterface() || Modifier.isAbstract(cls.getModifiers()))
            continue;

        for (Annotation annotation : cls.getAnnotations()) {
            if (annotation instanceof org.integratedmodelling.thinklab.interfaces.annotations.KnowledgeLoader) {

                String fmt = ((org.integratedmodelling.thinklab.interfaces.annotations.KnowledgeLoader) annotation)
                        .format();
                KBoxManager.get().registerImporterClass(fmt, cls);

                break;
            }
        }
    }

}

From source file:org.integratedmodelling.thinklab.plugin.ThinklabPlugin.java

protected void loadTransformations() throws ThinklabException {

    String ipack = this.getClass().getPackage().getName() + ".transformations";

    for (Class<?> cls : MiscUtilities.findSubclasses(ITransformation.class, ipack, getClassLoader())) {

        /*//from   ww  w . j  ava 2 s  .c  om
         * lookup annotation, ensure we can use the class
         */
        if (cls.isInterface() || Modifier.isAbstract(cls.getModifiers()))
            continue;

        for (Annotation annotation : cls.getAnnotations()) {
            if (annotation instanceof DataTransformation) {

                String name = ((DataTransformation) annotation).id();
                try {
                    TransformationFactory.get().registerTransformation(name,
                            (ITransformation) cls.newInstance());
                } catch (Exception e) {
                    throw new ThinklabValidationException(e);
                }

                break;
            }
        }
    }

}

From source file:org.integratedmodelling.thinklab.plugin.ThinklabPlugin.java

protected void loadListingProviders() throws ThinklabException {

    String ipack = this.getClass().getPackage().getName() + ".commands";

    for (Class<?> cls : MiscUtilities.findSubclasses(IListingProvider.class, ipack, getClassLoader())) {

        /*//from   ww  w.  ja v a2 s . c  o m
         * lookup annotation, ensure we can use the class
         */
        if (cls.isInterface() || Modifier.isAbstract(cls.getModifiers()))
            continue;

        for (Annotation annotation : cls.getAnnotations()) {
            if (annotation instanceof ListingProvider) {

                String name = ((ListingProvider) annotation).label();
                String sname = ((ListingProvider) annotation).itemlabel();
                try {
                    CommandManager.get().registerListingProvider(name, sname,
                            (IListingProvider) cls.newInstance());
                } catch (Exception e) {
                    throw new ThinklabValidationException(e);
                }

                break;
            }
        }
    }
}

From source file:com.laxser.blitz.web.ControllerInterceptorAdapter.java

/**
 * true??/*  ww w .  j  a v a  2 s.  c o  m*/
 * 
 * @param controllerClazz
 * @param actionMethod
 * @return
 */
protected final boolean checkDenyAnnotations(Class<?> controllerClazz, Method actionMethod) {
    List<Class<? extends Annotation>> denyAnnotations = getDenyAnnotationClasses();
    if (denyAnnotations == null || denyAnnotations.size() == 0) {
        return false;
    }
    for (Class<? extends Annotation> denyAnnotation : denyAnnotations) {
        if (denyAnnotation == null) {
            continue;
        }
        BitSet scopeSet = getAnnotationScope(denyAnnotation);
        if (scopeSet.get(AnnotationScope.METHOD.ordinal())) {
            if (actionMethod.isAnnotationPresent(denyAnnotation)) {
                return checkAnnotation(actionMethod.getAnnotation(denyAnnotation));
            }
        }
        if (scopeSet.get(AnnotationScope.CLASS.ordinal())) {
            if (controllerClazz.isAnnotationPresent(denyAnnotation)) {
                return checkAnnotation(actionMethod.getAnnotation(denyAnnotation));
            }
        }
        if (scopeSet.get(AnnotationScope.ANNOTATION.ordinal())) {
            for (Annotation annotation : actionMethod.getAnnotations()) {
                if (annotation.annotationType().isAnnotationPresent(denyAnnotation)) {
                    return checkAnnotation(actionMethod.getAnnotation(denyAnnotation));
                }
            }
            for (Annotation annotation : controllerClazz.getAnnotations()) {
                if (annotation.annotationType().isAnnotationPresent(denyAnnotation)) {
                    return checkAnnotation(actionMethod.getAnnotation(denyAnnotation));
                }
            }
        }
    }
    return false;
}

From source file:org.integratedmodelling.thinklab.plugin.ThinklabPlugin.java

protected void loadAnnotatedClass(String subpackage, Class<?> objectClass, Class<?> annotationClass,
        AnnotatedClassHandler handler) throws ThinklabException {

    String ipack = this.getClass().getPackage().getName() + "." + subpackage;

    for (Class<?> cls : MiscUtilities.findSubclasses(objectClass, ipack, getClassLoader())) {

        /*/*from  w w w  . jav a 2  s . c o m*/
         * lookup annotation, ensure we can use the class
         */
        if (cls.isInterface() || Modifier.isAbstract(cls.getModifiers()))
            continue;

        /*
         * find class with annotation and send back to plugin to process it
         */
        for (Annotation annotation : cls.getAnnotations()) {
            if (annotation.annotationType().equals(annotationClass)) {
                handler.process(annotation, cls, this);
            }
        }
    }
}

From source file:org.integratedmodelling.thinklab.plugin.ThinklabPlugin.java

protected void loadPersistentClasses() throws ThinklabPluginException {

    String ipack = this.getClass().getPackage().getName() + ".implementations";

    for (Class<?> cls : MiscUtilities.findSubclasses(IPersistentObject.class, ipack, getClassLoader())) {

        String ext = null;//from  w  w w.  ja v  a 2  s.  c o m

        /*
         * lookup annotation, ensure we can use the class
         */
        if (cls.isInterface() || Modifier.isAbstract(cls.getModifiers()))
            continue;

        /*
         * lookup implemented concept
         */
        for (Annotation annotation : cls.getAnnotations()) {
            if (annotation instanceof PersistentObject) {
                ext = ((PersistentObject) annotation).extension();
                if (ext.equals("__NOEXT__"))
                    ext = null;
                break;
            }
        }

        PersistenceManager.get().registerSerializableClass(cls, ext);
    }
}