All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.interceptor.reader.DefaultMethodMetadata Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual
 * contributors by the @authors tag. See the copyright.txt in the
 * distribution for a full listing of individual contributors.
 *
 * Licensed 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 org.jboss.interceptor.reader;

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

import org.jboss.interceptor.builder.MethodReference;
import org.jboss.interceptor.spi.metadata.MethodMetadata;
import org.jboss.interceptor.spi.model.InterceptionType;
import org.jboss.interceptor.util.InterceptionTypeRegistry;

/**
 * Represents information about an interceptor method
 *
 * @author Marius Bogoevici
 */
public class DefaultMethodMetadata implements MethodMetadata, Serializable
{
   
   private static final long serialVersionUID = -4538617003189564552L;
   
   private Method javaMethod;

   private Set supportedInterceptorTypes = new HashSet();

   private DefaultMethodMetadata(M methodReference, AnnotatedMethodReader annotationReader)
   {
      this.javaMethod = annotationReader.getJavaMethod(methodReference);
      for (InterceptionType interceptionType: InterceptionTypeRegistry.getSupportedInterceptionTypes())
      {
         if (annotationReader.getAnnotation(InterceptionTypeRegistry.getAnnotationClass(interceptionType), methodReference) != null)
         {
            supportedInterceptorTypes.add(interceptionType);
         }
      }
   }

   private DefaultMethodMetadata(Set interceptionTypes, MethodReference methodReference)
   {
      this.supportedInterceptorTypes = interceptionTypes;
      try
      {
         this.javaMethod = methodReference.getDeclaringClass().getDeclaredMethod(methodReference.getMethodName(), methodReference.getParameterTypes());
      }
      catch (NoSuchMethodException e)
      {
         throw new IllegalStateException(e);
      }
   }

   public static  MethodMetadata of(M methodReference, AnnotatedMethodReader methodReader)
   {
      return new DefaultMethodMetadata(methodReference, methodReader);
   }

   public static MethodMetadata of(Method method)
   {
      return new DefaultMethodMetadata(method, new ReflectiveAnnotatedMethodReader());
   }


   public Set getSupportedInterceptionTypes()
   {
      return supportedInterceptorTypes;
   }

   public Method getJavaMethod()
   {
      return javaMethod;
   }

   public Class getReturnType()
   {
      return javaMethod.getReturnType();
   }

   private Object writeReplace()
   {
      return new DefaultMethodMetadataSerializationProxy(supportedInterceptorTypes, MethodReference.of(this, true));
   }


   private static class DefaultMethodMetadataSerializationProxy implements Serializable
   {
      private Set supportedInterceptionTypes;
      private MethodReference methodReference;

      private DefaultMethodMetadataSerializationProxy(Set supportedInterceptionTypes, MethodReference methodReference)
      {
         this.supportedInterceptionTypes = supportedInterceptionTypes;
         this.methodReference = methodReference;
      }

      private Object readResolve() throws ObjectStreamException
      {
         return new DefaultMethodMetadata(supportedInterceptionTypes, methodReference);
      }

   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy