Example usage for java.lang.reflect Method getDeclaringClass

List of usage examples for java.lang.reflect Method getDeclaringClass

Introduction

In this page you can find the example usage for java.lang.reflect Method getDeclaringClass.

Prototype

@Override
public Class<?> getDeclaringClass() 

Source Link

Document

Returns the Class object representing the class or interface that declares the method represented by this object.

Usage

From source file:org.springframework.cloud.netflix.feign.support.SpringMvcContractTests.java

@Test
public void testProcessAnnotations_Aliased() throws Exception {
    Method method = TestTemplate_Advanced.class.getDeclaredMethod("getTest2", String.class, Integer.class);
    MethodMetadata data = this.contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

    assertEquals("/advanced/test2", data.template().url());
    assertEquals("PUT", data.template().method());
    assertEquals(MediaType.APPLICATION_JSON_VALUE, data.template().headers().get("Accept").iterator().next());

    assertEquals("Authorization", data.indexToName().get(0).iterator().next());
    assertEquals("amount", data.indexToName().get(1).iterator().next());

    assertEquals("{Authorization}", data.template().headers().get("Authorization").iterator().next());
    assertEquals("{amount}", data.template().queries().get("amount").iterator().next());
}

From source file:org.kmnet.com.fw.web.logging.TraceLoggingInterceptor.java

/**
 * Logic to output end log/*from  w  w w .j a v  a2 s  .  com*/
 * <p>
 * Outputs the end log.<br>
 * Outputs warning log if difference of time between start time and end time is more than the nano-seconds value<br>
 * set as warning log output timing.
 * </p>
 * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#postHandle(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
 */
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) {

    if (!(handler instanceof HandlerMethod)) {
        return;
    }

    long startTime = 0;
    if (request.getAttribute(START_ATTR) != null) {
        startTime = ((Long) request.getAttribute(START_ATTR)).longValue();
    }
    long handlingTime = System.nanoTime() - startTime;
    request.removeAttribute(START_ATTR);
    request.setAttribute(HANDLING_ATTR, handlingTime);
    String formattedHandlingTime = String.format("%1$,3d", handlingTime);

    boolean isWarnHandling = (handlingTime > warnHandlingNanos);

    if (isWarnHandling) {
        if (!logger.isWarnEnabled()) {
            return;
        }
    } else if (!logger.isTraceEnabled()) {
        return;
    }

    HandlerMethod handlerMethod = (HandlerMethod) handler;
    Method m = handlerMethod.getMethod();
    Object view = null;
    Map<String, Object> model = null;
    if (modelAndView != null) {
        view = modelAndView.getView();
        model = modelAndView.getModel();
        if (view == null) {
            view = modelAndView.getViewName();
        }
    }

    logger.trace("[END CONTROLLER  ] {}.{}({})-> view={}, model={}",
            new Object[] { m.getDeclaringClass().getSimpleName(), m.getName(), buildMethodParams(handlerMethod),
                    view, model });
    String handlingTimeMessage = "[HANDLING TIME   ] {}.{}({})-> {} ns";
    if (isWarnHandling) {
        logger.warn(handlingTimeMessage + " > {}", new Object[] { m.getDeclaringClass().getSimpleName(),
                m.getName(), buildMethodParams(handlerMethod), formattedHandlingTime, warnHandlingNanos });
    } else {
        logger.trace(handlingTimeMessage, new Object[] { m.getDeclaringClass().getSimpleName(), m.getName(),
                buildMethodParams(handlerMethod), formattedHandlingTime });
    }
}

From source file:org.springframework.cloud.netflix.feign.support.SpringMvcContractTests.java

@Test(expected = IllegalStateException.class)
public void testProcessQueryMapMoreThanOnce() throws Exception {
    Method method = TestTemplate_QueryMap.class.getDeclaredMethod("queryMapMoreThanOnce", MultiValueMap.class,
            MultiValueMap.class);
    this.contract.parseAndValidateMetadata(method.getDeclaringClass(), method);
}

From source file:org.springframework.cloud.netflix.feign.support.SpringMvcContractTests.java

@Test(expected = IllegalStateException.class)
public void testProcessHeaderMapMoreThanOnce() throws Exception {
    Method method = TestTemplate_HeaderMap.class.getDeclaredMethod("headerMapMoreThanOnce", MultiValueMap.class,
            MultiValueMap.class);
    this.contract.parseAndValidateMetadata(method.getDeclaringClass(), method);
}

From source file:org.terasoluna.gfw.web.logging.TraceLoggingInterceptor.java

/**
 * Logic to output end log/*from   ww w  .j  ava 2  s  .c o  m*/
 * <p>
 * Outputs the end log.<br>
 * Outputs warning log if difference of time between start time and end time is more than the nano-seconds value<br>
 * set as warning log output timing.
 * </p>
 * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#postHandle(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
 */
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) {

    if (!(handler instanceof HandlerMethod)) {
        return;
    }

    long startTime = 0;
    if (request.getAttribute(START_ATTR) != null) {
        startTime = ((Long) request.getAttribute(START_ATTR)).longValue();
    }
    long handlingTime = System.nanoTime() - startTime;
    request.removeAttribute(START_ATTR);
    request.setAttribute(HANDLING_ATTR, handlingTime);
    String formattedHandlingTime = String.format("%1$,3d", handlingTime);

    boolean isWarnHandling = handlingTime > warnHandlingNanos;

    if (!isEnabledLogLevel(isWarnHandling)) {
        return;
    }

    HandlerMethod handlerMethod = (HandlerMethod) handler;
    Method m = handlerMethod.getMethod();
    Object view = null;
    Map<String, Object> model = null;
    if (modelAndView != null) {
        view = modelAndView.getView();
        model = modelAndView.getModel();
        if (view == null) {
            view = modelAndView.getViewName();
        }
    }

    logger.trace("[END CONTROLLER  ] {}.{}({})-> view={}, model={}",
            new Object[] { m.getDeclaringClass().getSimpleName(), m.getName(), buildMethodParams(handlerMethod),
                    view, model });
    String handlingTimeMessage = "[HANDLING TIME   ] {}.{}({})-> {} ns";
    if (isWarnHandling) {
        logger.warn(handlingTimeMessage + " > {}", new Object[] { m.getDeclaringClass().getSimpleName(),
                m.getName(), buildMethodParams(handlerMethod), formattedHandlingTime, warnHandlingNanos });
    } else {
        logger.trace(handlingTimeMessage, new Object[] { m.getDeclaringClass().getSimpleName(), m.getName(),
                buildMethodParams(handlerMethod), formattedHandlingTime });
    }
}

From source file:jenkins.scm.api.SCMHeadMixinEqualityGenerator.java

/**
 * Generates {@link SCMHeadMixin.Equality#equals(SCMHeadMixin, SCMHeadMixin)}.
 *
 * @param cw      the {@link ClassWriter}
 * @param methods the property getters.//from   ww w  . j a v a  2s.  co m
 */
private void generateEquals(@NonNull ClassWriter cw, @NonNull Collection<Method> methods) {
    String scmHeadMixinDescriptor = Type.getDescriptor(SCMHeadMixin.class);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "equals",
            "(" + scmHeadMixinDescriptor + scmHeadMixinDescriptor + ")Z", null, null);
    mv.visitCode();
    boolean bigStack = false;
    for (Method m : methods) {
        String declClass = Type.getInternalName(m.getDeclaringClass());
        Class<?> returnType = m.getReturnType();
        String methodDesc = "()" + Type.getDescriptor(returnType);
        if (boolean.class.equals(returnType) || byte.class.equals(returnType) || char.class.equals(returnType)
                || int.class.equals(returnType) || short.class.equals(returnType)) {
            // all these primitive types are
            // int p1 = ((T)o1).get___();
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, declClass);
            mv.visitMethodInsn(INVOKEINTERFACE, declClass, m.getName(), methodDesc, true);
            mv.visitVarInsn(ISTORE, 3);
            // int p2 = ((T)o2).get___();
            mv.visitVarInsn(ALOAD, 2);
            mv.visitTypeInsn(CHECKCAST, declClass);
            mv.visitMethodInsn(INVOKEINTERFACE, declClass, m.getName(), methodDesc, true);
            // if (p2 != p1) return false;
            mv.visitVarInsn(ILOAD, 3);
            Label l1 = new Label();
            mv.visitJumpInsn(IF_ICMPEQ, l1);
            mv.visitInsn(ICONST_0);
            mv.visitInsn(IRETURN);
            mv.visitLabel(l1);
        } else if (long.class.equals(returnType)) {
            bigStack = true;
            // long p1 = ((T)o1).get___();
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, declClass);
            mv.visitMethodInsn(INVOKEINTERFACE, declClass, m.getName(), methodDesc, true);
            mv.visitVarInsn(LSTORE, 3);
            // long p2 = ((T)o2).get___();
            mv.visitVarInsn(ALOAD, 2);
            mv.visitTypeInsn(CHECKCAST, declClass);
            mv.visitMethodInsn(INVOKEINTERFACE, declClass, m.getName(), methodDesc, true);
            mv.visitVarInsn(LSTORE, 5);
            // if (p2 != p1) return false;
            mv.visitVarInsn(LLOAD, 3);
            mv.visitVarInsn(LLOAD, 5);
            mv.visitInsn(LCMP);
            Label l1 = new Label();
            mv.visitJumpInsn(IFEQ, l1);
            mv.visitInsn(ICONST_0);
            mv.visitInsn(IRETURN);
            mv.visitLabel(l1);
        } else if (double.class.equals(returnType)) {
            // not expecting people to return floating point types from SCMHeadMixin properties
            // here for completeness but will compare for strict equality so should blow up in peoples faces
            // if they are not persisting the floating points correctly
            bigStack = true;
            // double p1 = ((T)o1).get___();
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, declClass);
            mv.visitMethodInsn(INVOKEINTERFACE, declClass, m.getName(), methodDesc, true);
            mv.visitVarInsn(DSTORE, 3);
            // double p2 = ((T)o2).get___();
            mv.visitVarInsn(ALOAD, 2);
            mv.visitTypeInsn(CHECKCAST, declClass);
            mv.visitMethodInsn(INVOKEINTERFACE, declClass, m.getName(), methodDesc, true);
            mv.visitVarInsn(DSTORE, 5);
            // if (p2 != p1) return false;
            mv.visitVarInsn(DLOAD, 3);
            mv.visitVarInsn(DLOAD, 5);
            mv.visitInsn(DCMPL); // HA HA HA this will likely not work for you
            Label l1 = new Label();
            mv.visitJumpInsn(IFEQ, l1);
            mv.visitInsn(ICONST_0);
            mv.visitInsn(IRETURN);
            mv.visitLabel(l1);
        } else if (float.class.equals(returnType)) {
            // not expecting people to return floating point types from SCMHeadMixin properties
            // here for completeness but will compare for strict equality so should blow up in peoples faces
            // if they are not persisting the floating points correctly

            // float p1 = ((T)o1).get___();
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, declClass);
            mv.visitMethodInsn(INVOKEINTERFACE, declClass, m.getName(), methodDesc, true);
            mv.visitVarInsn(FSTORE, 3);
            // float p2 = ((T)o2).get___();
            mv.visitVarInsn(ALOAD, 2);
            mv.visitTypeInsn(CHECKCAST, declClass);
            mv.visitMethodInsn(INVOKEINTERFACE, declClass, m.getName(), methodDesc, true);
            mv.visitVarInsn(FSTORE, 5);
            // if (p2 != p1) return false;
            mv.visitVarInsn(FLOAD, 3);
            mv.visitVarInsn(FLOAD, 5);
            mv.visitInsn(FCMPL); // HA HA HA this will likely not work for you
            Label l1 = new Label();
            mv.visitJumpInsn(IFEQ, l1);
            mv.visitInsn(ICONST_0);
            mv.visitInsn(IRETURN);
            mv.visitLabel(l1);
        } else {
            // Object p1 = ((T)o1).get___();
            mv.visitVarInsn(ALOAD, 1);
            mv.visitTypeInsn(CHECKCAST, declClass);
            mv.visitMethodInsn(INVOKEINTERFACE, declClass, m.getName(), methodDesc, true);
            mv.visitVarInsn(ASTORE, 3);
            // Object p2 = ((T)o2).get___();
            mv.visitVarInsn(ALOAD, 2);
            mv.visitTypeInsn(CHECKCAST, declClass);
            mv.visitMethodInsn(INVOKEINTERFACE, declClass, m.getName(), methodDesc, true);
            mv.visitVarInsn(ASTORE, 4);
            // if (p1 == null ? p2 != null : !p1.equals(p2)) return false;
            mv.visitVarInsn(ALOAD, 3);
            Label l1 = new Label();
            Label l2 = new Label();
            Label l3 = new Label();
            mv.visitJumpInsn(IFNONNULL, l1);
            mv.visitVarInsn(ALOAD, 4);
            mv.visitJumpInsn(IFNULL, l3);
            mv.visitJumpInsn(GOTO, l2);
            mv.visitLabel(l1);
            mv.visitVarInsn(ALOAD, 4);
            mv.visitVarInsn(ALOAD, 3);
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(Object.class), "equals",
                    "(Ljava/lang/Object;)Z", false);
            mv.visitJumpInsn(IFNE, l3);
            mv.visitLabel(l2);
            mv.visitInsn(ICONST_0);
            mv.visitInsn(IRETURN);
            mv.visitLabel(l3);
        }
    }
    // return true
    mv.visitInsn(ICONST_1);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(bigStack ? 4 : 2, bigStack ? 7 : 5);
    mv.visitEnd();
}

From source file:org.springframework.cloud.netflix.feign.support.SpringMvcContractTests.java

@Test
public void testProcessQueryMap() throws Exception {
    Method method = TestTemplate_QueryMap.class.getDeclaredMethod("queryMap", MultiValueMap.class,
            String.class);
    MethodMetadata data = this.contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

    assertEquals("/queryMap", data.template().url());
    assertEquals("GET", data.template().method());
    assertEquals(0, data.queryMapIndex().intValue());
    Map<String, Collection<String>> params = data.template().queries();
    assertEquals("{aParam}", params.get("aParam").iterator().next());
}

From source file:org.springframework.cloud.netflix.feign.support.SpringMvcContractTests.java

@Test
public void testProcessHeaderMap() throws Exception {
    Method method = TestTemplate_HeaderMap.class.getDeclaredMethod("headerMap", MultiValueMap.class,
            String.class);
    MethodMetadata data = this.contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

    assertEquals("/headerMap", data.template().url());
    assertEquals("GET", data.template().method());
    assertEquals(0, data.headerMapIndex().intValue());
    Map<String, Collection<String>> headers = data.template().headers();
    assertEquals("{aHeader}", headers.get("aHeader").iterator().next());
}

From source file:org.springframework.cloud.netflix.feign.support.SpringMvcContractTests.java

@Test
public void testProcessAnnotationsOnMethod_Advanced() throws Exception {
    Method method = TestTemplate_Advanced.class.getDeclaredMethod("getTest", String.class, String.class,
            Integer.class);
    MethodMetadata data = this.contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

    assertEquals("/advanced/test/{id}", data.template().url());
    assertEquals("PUT", data.template().method());
    assertEquals(MediaType.APPLICATION_JSON_VALUE, data.template().headers().get("Accept").iterator().next());
}

From source file:org.springframework.cloud.netflix.feign.support.SpringMvcContractTests.java

@Test
public void testProcessAnnotations_Class_AnnotationsGetSpecificTest() throws Exception {
    Method method = TestTemplate_Class_Annotations.class.getDeclaredMethod("getSpecificTest", String.class,
            String.class);
    MethodMetadata data = this.contract.parseAndValidateMetadata(method.getDeclaringClass(), method);

    assertEquals("/prepend/{classId}/test/{testId}", data.template().url());
    assertEquals("GET", data.template().method());

    assertEquals("classId", data.indexToName().get(0).iterator().next());
    assertEquals("testId", data.indexToName().get(1).iterator().next());
}