introspectionmodeller.java

来自「resetful样式的ws样例,一种面向资源的webservices服务」· Java 代码 · 共 545 行 · 第 1/2 页

JAVA
545
字号
/* * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. *  * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. *  * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License").  You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html * or jersey/legal/LICENSE.txt.  See the License for the specific * language governing permissions and limitations under the License. *  * When distributing the software, include this License Header Notice in each * file and include the License file at jersey/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code.  If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" *  * Contributor(s): *  * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license."  If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above.  However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */package com.sun.jersey.impl.modelapi.annotation;import com.sun.jersey.api.model.AbstractField;import com.sun.jersey.api.model.AbstractResource;import com.sun.jersey.api.model.AbstractResourceConstructor;import com.sun.jersey.api.model.AbstractResourceMethod;import com.sun.jersey.api.model.AbstractSetterMethod;import com.sun.jersey.api.model.AbstractSubResourceLocator;import com.sun.jersey.api.model.AbstractSubResourceMethod;import com.sun.jersey.api.model.Parameter;import com.sun.jersey.api.model.Parameter.Source;import com.sun.jersey.api.model.Parameterized;import com.sun.jersey.api.model.UriPathValue;import com.sun.jersey.impl.ImplMessages;import com.sun.jersey.impl.model.MediaTypeHelper;import java.lang.annotation.Annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Type;import java.security.AccessController;import java.security.PrivilegedAction;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Map;import java.util.WeakHashMap;import java.util.logging.Level;import java.util.logging.Logger;import javax.ws.rs.ConsumeMime;import javax.ws.rs.CookieParam;import javax.ws.rs.DefaultValue;import javax.ws.rs.Encoded;import javax.ws.rs.HeaderParam;import javax.ws.rs.HttpMethod;import javax.ws.rs.MatrixParam;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.ProduceMime;import javax.ws.rs.QueryParam;import javax.ws.rs.core.Context;/** * * @author japod */public class IntrospectionModeller {    private static final Logger LOGGER = Logger.getLogger(IntrospectionModeller.class.getName());    public static final AbstractResource createResource(Class<?> resourceClass) {        final Path rPathAnnotation = resourceClass.getAnnotation(Path.class);        final boolean isRootResourceClass = (null != rPathAnnotation);        final boolean isEncodedAnotOnClass =                 (null != resourceClass.getAnnotation(Encoded.class));        AbstractResource resource;        if (isRootResourceClass) {            resource = new AbstractResource(resourceClass,                    new UriPathValue(rPathAnnotation.value(),                         rPathAnnotation.encode(),                         rPathAnnotation.limited()));        } else { // just a subresource class            resource = new AbstractResource(resourceClass);        }        workOutConstructorsList(resource, resourceClass.getConstructors(),                 isEncodedAnotOnClass);        workOutFieldsList(resource, isEncodedAnotOnClass);                final MethodList methodList = new MethodList(resourceClass);        workOutSetterMethodsList(resource, methodList, isEncodedAnotOnClass);                final ConsumeMime classScopeConsumeMimeAnnotation =                 resourceClass.getAnnotation(ConsumeMime.class);        final ProduceMime classScopeProduceMimeAnnotation =                 resourceClass.getAnnotation(ProduceMime.class);        workOutResourceMethodsList(resource, methodList, isEncodedAnotOnClass,                 classScopeConsumeMimeAnnotation, classScopeProduceMimeAnnotation);        workOutSubResourceMethodsList(resource, methodList, isEncodedAnotOnClass,                 classScopeConsumeMimeAnnotation, classScopeProduceMimeAnnotation);        workOutSubResourceLocatorsList(resource, methodList, isEncodedAnotOnClass);                logNonPublicMethods(resourceClass);        if (LOGGER.isLoggable(Level.FINEST)) {            LOGGER.finest(ImplMessages.NEW_AR_CREATED_BY_INTROSPECTION_MODELER(                    resource.toString()));        }        return resource;    }        private static final void addConsumeMime(            AnnotatedMethod am,                        AbstractResourceMethod resourceMethod,             ConsumeMime consumeMimeAnnotation) {        // Override annotation is present in method        if (am.isAnnotationPresent(ConsumeMime.class))            consumeMimeAnnotation = am.getAnnotation(ConsumeMime.class);                resourceMethod.getSupportedInputTypes().addAll(                MediaTypeHelper.createMediaTypes(consumeMimeAnnotation));    }    private static final void addProduceMime(            AnnotatedMethod am,            AbstractResourceMethod resourceMethod,             ProduceMime produceMimeAnnotation) {        // Override annotation is present in method        if (am.isAnnotationPresent(ProduceMime.class))            produceMimeAnnotation = am.getAnnotation(ProduceMime.class);        resourceMethod.getSupportedOutputTypes().addAll(                MediaTypeHelper.createMediaTypes(produceMimeAnnotation));    }    private static final void workOutConstructorsList(            AbstractResource resource,             Constructor[] ctorArray,             boolean isEncoded) {        if (null != ctorArray) {            for (Constructor ctor : ctorArray) {                final AbstractResourceConstructor aCtor =                         new AbstractResourceConstructor(ctor);                processParameters(aCtor, ctor, isEncoded);                resource.getConstructors().add(aCtor);            }        }    }    private static final void workOutFieldsList(            AbstractResource resource,             boolean isEncoded) {                Class c = resource.getResourceClass();        if (c.isInterface())            return;                while (c != Object.class) {             for (final Field f : c.getDeclaredFields()) {                    final AbstractField af = new AbstractField(f);                    Parameter p = createParameter(f.toString(), 1, isEncoded,                             f.getType(),                            f.getGenericType(),                            f.getAnnotations());                    if (null != p) {                        af.getParameters().add(p);                        resource.getFields().add(af);                    }             }             c = c.getSuperclass();        }    }        private static final void workOutSetterMethodsList(            AbstractResource resource,             MethodList methodList,            boolean isEncoded) {        for (AnnotatedMethod m : methodList.                hasNotMetaAnnotation(HttpMethod.class).                hasNotAnnotation(Path.class).                hasNumParams(1).                hasReturnType(void.class).                nameStartsWith("set")) {                        final AbstractSetterMethod asm = new AbstractSetterMethod(m.getMethod());            Parameter p = createParameter(m.toString(), 1, isEncoded,                     m.getParameterTypes()[0],                    m.getGenericParameterTypes()[0],                    m.getAnnotations());            if (null != p) {                asm.getParameters().add(p);                resource.getSetterMethods().add(asm);            }        }            }        private static final void workOutResourceMethodsList(            AbstractResource resource,             MethodList methodList,             boolean isEncoded,            ConsumeMime classScopeConsumeMimeAnnotation,             ProduceMime classScopeProduceMimeAnnotation) {        // TODO what to do about multiple meta HttpMethod annotations?        // Should the method be added more than once to the model and        // then validation could reject?        for (AnnotatedMethod m : methodList.hasMetaAnnotation(HttpMethod.class).                hasNotAnnotation(Path.class)) {            final AbstractResourceMethod resourceMethod = new AbstractResourceMethod(                    resource,                    m.getMethod(),                     m.getMetaMethodAnnotations(HttpMethod.class).get(0).value());            addConsumeMime(m, resourceMethod, classScopeConsumeMimeAnnotation);            addProduceMime(m, resourceMethod, classScopeProduceMimeAnnotation);            processParameters(resourceMethod, m, isEncoded);            resource.getResourceMethods().add(resourceMethod);        }    }        private static final void workOutSubResourceMethodsList(            AbstractResource resource,             MethodList methodList,             boolean isEncoded,            ConsumeMime classScopeConsumeMimeAnnotation,             ProduceMime classScopeProduceMimeAnnotation) {        // TODO what to do about multiple meta HttpMethod annotations?        // Should the method be added more than once to the model and        // then validation could reject?        for (AnnotatedMethod m : methodList.hasMetaAnnotation(HttpMethod.class).                hasAnnotation(Path.class)) {            final Path mPathAnnotation = m.getAnnotation(Path.class);            final AbstractSubResourceMethod subResourceMethod = new AbstractSubResourceMethod(                    resource,                    m.getMethod(),                    new UriPathValue(                        mPathAnnotation.value(),                         mPathAnnotation.encode(),                        mPathAnnotation.limited()),                    m.getMetaMethodAnnotations(HttpMethod.class).get(0).value());                   addConsumeMime(m, subResourceMethod, classScopeConsumeMimeAnnotation);            addProduceMime(m, subResourceMethod, classScopeProduceMimeAnnotation);            processParameters(subResourceMethod, m, isEncoded);            resource.getSubResourceMethods().add(subResourceMethod);        }    }        /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?