Clover coverage report -
Coverage timestamp: Thu Jun 22 2006 14:24:50 CEST
file stats: LOC: 120   Methods: 3
NCLOC: 51   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ClassesEnhancer.java 100% 100% 100% 100%
coverage
 1    /**
 2    * EasyBeans
 3    * Copyright (C) 2006 Bull S.A.S.
 4    * Contact: easybeans@objectweb.org
 5    *
 6    * This library is free software; you can redistribute it and/or
 7    * modify it under the terms of the GNU Lesser General Public
 8    * License as published by the Free Software Foundation; either
 9    * version 2.1 of the License, or any later version.
 10    *
 11    * This library is distributed in the hope that it will be useful,
 12    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 14    * Lesser General Public License for more details.
 15    *
 16    * You should have received a copy of the GNU Lesser General Public
 17    * License along with this library; if not, write to the Free Software
 18    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 19    * USA
 20    *
 21    * --------------------------------------------------------------------------
 22    * $Id: ClassesEnhancer.java 408 2006-04-25 14:56:11Z benoitf $
 23    * --------------------------------------------------------------------------
 24    */
 25    package org.objectweb.easybeans.tests.common.enhancer;
 26   
 27    import java.io.InputStream;
 28    import java.net.URL;
 29    import java.net.URLClassLoader;
 30    import java.util.List;
 31   
 32    import org.objectweb.asm.ClassReader;
 33    import org.objectweb.easybeans.deployment.annotations.analyzer.ScanClassVisitor;
 34    import org.objectweb.easybeans.deployment.annotations.helper.ResolverHelper;
 35    import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
 36    import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
 37    import org.objectweb.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata;
 38    import org.objectweb.easybeans.enhancer.Enhancer;
 39   
 40   
 41    /**
 42    * Enhance a set of given classes.
 43    * Rely on core enhancer.
 44    * @author Florent Benoit
 45    */
 46    public final class ClassesEnhancer extends Enhancer {
 47   
 48    /**
 49    * Type available (for adding adapter).
 50    */
 51    public static enum TYPE {INTERCEPTOR, CALLBACK, ALL}
 52   
 53    /**
 54    * Class extension.
 55    */
 56    public static final String EXT_CLASS = ".class";
 57   
 58    /**
 59    * Creates an new enhancer.
 60    * @param loader classloader where to define enhanced classes.
 61    * @param ejbJarAnnotationMetadata object with references to the metadata.
 62    */
 63  7 public ClassesEnhancer(final ClassLoader loader, final EjbJarAnnotationMetadata ejbJarAnnotationMetadata) {
 64  7 super(loader, ejbJarAnnotationMetadata, null);
 65    }
 66   
 67    /**
 68    * Enhance the classes simulating a Bean. It uses a child classloder of the current thread.<br>
 69    * @param classesToEnhance the list of class on which run class adapters
 70    * @param type the type of adapter to run.
 71    * @throws Exception if it fails
 72    */
 73  21 public static void enhanceNewClassLoader(final List<String> classesToEnhance, final TYPE type) throws Exception {
 74  21 ClassLoader loader = Thread.currentThread().getContextClassLoader();
 75  21 ClassLoader childLoader = new URLClassLoader(new URL[]{}, loader);
 76  21 Thread.currentThread().setContextClassLoader(childLoader);
 77  21 try{
 78  21 enhance(classesToEnhance, TYPE.INTERCEPTOR);
 79    }finally{
 80  21 Thread.currentThread().setContextClassLoader(loader);
 81    }
 82    }
 83   
 84    /**
 85    * Enhance the classes simulating a Bean.<br>
 86    * @param classesToEnhance the list of class on which run class adapters
 87    * @param type the type of adapter to run.
 88    * @throws Exception if it fails
 89    */
 90  25 public static void enhance(final List<String> classesToEnhance, final TYPE type) throws Exception {
 91  25 EjbJarAnnotationMetadata ejbJarAnnotationMetadata = new EjbJarAnnotationMetadata();
 92  25 ScanClassVisitor scanVisitor = new ScanClassVisitor(ejbJarAnnotationMetadata);
 93  25 ClassLoader loader = Thread.currentThread().getContextClassLoader();
 94   
 95  25 InputStream is = null;
 96  25 for (String clazz : classesToEnhance) {
 97  80 is = loader.getResourceAsStream(clazz);
 98  80 new ClassReader(is).accept(scanVisitor, false);
 99    }
 100   
 101  25 ResolverHelper.resolve(ejbJarAnnotationMetadata);
 102   
 103   
 104    // Remove some TX interceptors (so it works offline)
 105  7 for (ClassAnnotationMetadata classAnnotationMetadata : ejbJarAnnotationMetadata
 106    .getClassAnnotationMetadataCollection()) {
 107  34 if (classAnnotationMetadata.isBean()) {
 108    // Remove global EasyBeans interceptors
 109  13 classAnnotationMetadata.setGlobalEasyBeansInterceptors(null);
 110  13 for (MethodAnnotationMetadata m : classAnnotationMetadata.getMethodAnnotationMetadataCollection()) {
 111  153 m.setInterceptors(null);
 112    }
 113    }
 114    }
 115   
 116  7 ClassesEnhancer classesEnhancer = new ClassesEnhancer(loader, ejbJarAnnotationMetadata);
 117  7 classesEnhancer.enhance();
 118    }
 119   
 120    }