com.eincs.decanter.container.simple.route.MethodExtractor.java Source code

Java tutorial

Introduction

Here is the source code for com.eincs.decanter.container.simple.route.MethodExtractor.java

Source

/*
 * Copyright 2012 The Decanter Project
 *
 * The Decanter Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.eincs.decanter.container.simple.route;

import java.lang.reflect.Method;
import java.util.List;

import com.google.common.collect.Lists;

/**
 * @author Jung-Haeng Lee
 */
public class MethodExtractor {

    private MethodExtractor() {
    }

    /**
     * 
     * @param serviceObj
     * @return
     * @throws RouteReflectException
     */
    public static List<MethodRouteInfo> extract(Object serviceObj) throws RouteReflectException {
        List<MethodRouteInfo> result = Lists.newArrayList();
        for (Method method : serviceObj.getClass().getMethods()) {
            Route route = method.getAnnotation(Route.class);
            if (route != null) {
                result.add(MethodRouteInfo.create(method));
            }
        }
        return result;
    }

    /**
     * @author Jung-Haeng Lee
     */
    public static class MethodRouteInfo {

        private static MethodRouteInfo create(Method method) throws RouteReflectException {
            MethodRouteInfo result = new MethodRouteInfo();
            Route route = method.getAnnotation(Route.class);
            result.setMethod(method);
            result.setRoute(route);
            result.setPath(route.path());
            result.setHttpMethods(Lists.newArrayList(route.method()));
            return result;
        }

        private Method method;
        private Route route;
        private String path;
        private List<String> httpMethods;

        private MethodRouteInfo() {
        }

        public Method getMethod() {
            return method;
        }

        private void setMethod(Method method) {
            this.method = method;
        }

        public Route getRoute() {
            return route;
        }

        private void setRoute(Route route) {
            this.route = route;
        }

        public String getPath() {
            return path;
        }

        private void setPath(String path) {
            this.path = path;
        }

        public List<String> getHttpMethods() {
            return httpMethods;
        }

        private void setHttpMethods(List<String> httpMethods) {
            this.httpMethods = httpMethods;
        }

    }
}