<commit_before>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat Middleware LLC, 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.webbeans.mock;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.MessageDriven;
import javax.ejb.Remove;
import javax.ejb.Singleton;
import javax.ejb.Stateful;
import javax.ejb.Stateless;

import org.jboss.webbeans.ejb.spi.BusinessInterfaceDescriptor;
import org.jboss.webbeans.ejb.spi.EjbDescriptor;

public class MockEjbDescriptor<T> implements EjbDescriptor<T>
{
   private final Class<T> type;
   private final String ejbName;
   private final List<BusinessInterfaceDescriptor<?>> localInterfaces;
   private final HashSet<Method> removeMethods;
   
   public static <T> MockEjbDescriptor<T> of(Class<T> type)
   {
      return new MockEjbDescriptor<T>(type);
   }

   private MockEjbDescriptor(final Class<T> type)
   {
      this.type = type;
      this.ejbName = type.getSimpleName();
      this.localInterfaces = new ArrayList<BusinessInterfaceDescriptor<?>>();
      for (final Class<?> clazz : type.getInterfaces())
      {
         if (clazz.isAnnotationPresent(Local.class))
         {
            localInterfaces.add(new BusinessInterfaceDescriptor<Object>()
            {
   
               @SuppressWarnings("unchecked")
               public Class<Object> getInterface()
               {
                  return (Class<Object>) clazz;
               }
   
               public String getJndiName()
               {
                  return clazz.getSimpleName() + "/local";
               }
         
            });
         }
      }
      // cope with EJB 3.1 style no-interface views
      if (localInterfaces.size() == 0)
      {
         localInterfaces.add(new BusinessInterfaceDescriptor<Object>()
         {

            public Class<Object> getInterface()
            {
               return (Class<Object>) type;
            }

            public String getJndiName()
            {
               return type.getSimpleName() +"/local";
            }
            
         });
      }
      this.removeMethods = new HashSet<Method>();
      for (final Method method : type.getMethods())
      {
         if (method.isAnnotationPresent(Remove.class))
         {
            removeMethods.add(method);
         }
      }
   }

   public String getEjbName()
   {
      return ejbName;
   }

   public Iterable<BusinessInterfaceDescriptor<?>> getLocalBusinessInterfaces()
   {
      return localInterfaces;
   }
   
   public Iterable<BusinessInterfaceDescriptor<?>> getRemoteBusinessInterfaces()
   {
      return Collections.emptyList();
   }

   public Iterable<Method> getRemoveMethods()
   {

      return removeMethods;
   }

   public Class<T> getType()
   {<commit_msg>WBRI-151 - thanks to <NAME><commit_after>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat Middleware LLC, 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.webbeans.mock;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.MessageDriven;
import javax.ejb.Remove;
import javax.ejb.Singleton;
import javax.ejb.Stateful;
import javax.ejb.Stateless;

import org.jboss.webbeans.ejb.spi.BusinessInterfaceDescriptor;
import org.jboss.webbeans.ejb.spi.EjbDescriptor;

public class MockEjbDescriptor<T> implements EjbDescriptor<T>
{
   private final Class<T> type;
   private final String ejbName;
   private final List<BusinessInterfaceDescriptor<?>> localInterfaces;
   private final HashSet<Method> removeMethods;
   
   public static <T> MockEjbDescriptor<T> of(Class<T> type)
   {
      return new MockEjbDescriptor<T>(type);
   }

   private MockEjbDescriptor(final Class<T> type)
   {
      this.type = type;
      this.ejbName = type.getSimpleName();
      this.localInterfaces = new ArrayList<BusinessInterfaceDescriptor<?>>();
      
      Local localAnnotation = type.getAnnotation(Local.class);
      if (localAnnotation != null)
      {
         for (final Class<?> clazz : localAnnotation.value())
         {
            localInterfaces.add(createBusinessInterfaceDescriptor(clazz));
         }
      }
      
      for (final Class<?> clazz : type.getInterfaces())
      {
         if (clazz.isAnnotationPresent(Local.class))
         {
            localInterfaces.add(createBusinessInterfaceDescriptor(clazz));
         }
      }
      // cope with EJB 3.1 style no-interface views
      if (localInterfaces.size() == 0)
      {
         localInterfaces.add(createBusinessInterfaceDescriptor(type));
      }
      this.removeMethods = new HashSet<Method>();
      for (final Method method : type.getMethods())
      {
         if (method.isAnnotationPresent(Remove.class))
         {
            removeMethods.add(method);
         }
      }
   }

   private BusinessInterfaceDescriptor<Object> createBusinessInterfaceDescriptor(final Class<?> clazz)
   {
      return new BusinessInterfaceDescriptor<Object>()
      {
  
         @SuppressWarnings("unchecked")
         public Class<Object> getInterface()
         {
            return (Class<Object>) clazz;
         }
  
         public String getJndiName()
         {
            return clazz.getSimpleName() + "/local";
         }
      
      };
   }

   public String getEjbName()
   {
      return ejbName;
   }

   public Iterable<BusinessInterfaceDescriptor<?>> getLocalBusinessInterfaces()
   {
      return localInterfaces;
   }
   
   public Iterable<BusinessInterfaceDescriptor<?>> getRemoteBusinessInterfaces()
   {
      return Collections.emptyList();
   }

   public Iterable<Method> getRemoveMethods()
   {

      return removeMethods;
   }

   public Class<T> getType()
   {
{'fraction_non_alphanumeric': 0.09382784389703847, 'fraction_numerical': 0.0034597287572654305, 'mean_word_length': 2.708055413032324, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6125683', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1951, 'n_words': 529}