Package de.petris.dynamicaspects.test

Source Code of de.petris.dynamicaspects.test.Test

/*
* Copyright (c) 2004, Marco Petris
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     - Redistributions of source code must retain the above copyright notice,
*       this list of conditions and the following disclaimer.
*     - Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     - Neither the name of Marco Petris nor the names of its
*       contributors may be used to endorse or promote products derived from
*       this software without specific prior written permission.
*      
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

package de.petris.dynamicaspects.test;


import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import de.petris.dynamicaspects.AspectAgent;
import de.petris.dynamicaspects.CFlow;
import de.petris.dynamicaspects.CFlowCondition;
import de.petris.dynamicaspects.DefaultBeforeAfterAdvice;
import de.petris.dynamicaspects.PointcutFactory;
import de.petris.dynamicaspects.WeaveType;
import de.petris.dynamicaspects.test.aspects.MyCallAspect;
import de.petris.dynamicaspects.test.aspects.MyMixInInterface;
import de.petris.dynamicaspects.test.aspects.MyMixin;
import de.petris.dynamicaspects.test.aspects.PrintoutAspect;
import de.petris.dynamicaspects.test.targets.MultipleTargets;
import de.petris.dynamicaspects.test.targets.SimpleTarget;
import de.petris.dynamicaspects.util.Reflection;


/**
* Test class.
*
* @author mp
*/
public class Test {

    public Test() {
   }
  
   public void multiTest() {
        try {
           
//          List<String> packageList = new ArrayList<String>();
//          List packageList = new ArrayList();
//          packageList.add( "de.petris.dynamicaspects.test.aspects" );
//          AspectAgent.setPackageList( packageList );

          // boolean argument-constructor
         
//          new PrintoutAspect().installAroundExecution(
//            MultipleTargets.class,
//            new JoinpointPatternFactory().
//              addMethodName(
//                JoinpointPatternFactory.CONSTRUCTOR_NAME ).
//              addParamTypeList( new Class[] { boolean.class } ).
//              getPattern() );
         
          // all constructors
//          new PrintoutAspect().installAroundExecution( MultipleTargets.class,
//              JoinpointPatternFactory.PATTERN_ALL_CONSTRUCTORS );
         
          // the two 'setter'-methods
          new PrintoutAspect().installAroundExecution(
            MultipleTargets.class, PointcutFactory.PATTERN_ALL_SETTER );
         
          // all private methods
//          new PrintoutAspect().installAroundExecution(
//            MultipleTargets.class, JoinpointPatternFactory.PATTERN_ALL_PRIVATE_METHODS );

          // all public methods
//          new PrintoutAspect().installAroundExecution(
//            MultipleTargets.class, JoinpointPatternFactory.PATTERN_ALL_PUBLIC_METHODS );
         
          // simply all
//          new PrintoutAspect().installAroundExecution(
//            MultipleTargets.class, new JoinpointPatternFactory().getPattern() );
         
          MultipleTargets mt = new MultipleTargets();
          mt.trycatch( "test string" );
          mt.trycatch( null );
          mt.conv( new Integer( 7 ) );
//          mt.setD( 5.0, "test string" );
          mt.objectGetter();
          mt.simpleGetter();
          mt.setB( true );
          MultipleTargets.getClassName();
          try {
            mt.iamthrowing(new boolean[] {true});
          }
          catch( Throwable t ) {
            System.out.println( "rethrowed: " + t );
          }
         

          AspectAgent.deinstall( MultipleTargets.class, PrintoutAspect.class );
         
         
          mt.trycatch( "test string" );
          mt.trycatch( null );
          mt.conv( new Integer( 7 ) );
          mt.setD( 5.0, "test string" );
          mt.objectGetter();
          mt.simpleGetter();
          mt.setB( true );
          MultipleTargets.getClassName();
          try {
            mt.iamthrowing( new boolean[] {true});
          }
          catch( Throwable t ) {
            System.out.println( "without aspect: " + t );
          }
         
        }catch (Throwable e){
          e.printStackTrace()
        }
   }
  
   public void simpleTest() {
     DefaultBeforeAfterAdvice a = new PrintoutAspect();
    
     // apply aspect to one method
//     a.install(
//       SimpleTarget.class,
//       "public .* doIt.*" );
    
     a.installAroundExecution(
//            Reflection.getInnerClassOf( "SimpleInner", SimpleTarget.class ),
            SimpleTarget.class,
       new PointcutFactory().addMethodName( "doIt" ).
//       addParamTypeList(
//                    double.class, int.class,    long.class, boolean[].class ).
           addParamTypeList(
                    double.class, ArrayList.class).
                getPattern() );
    
     SimpleTarget t = new SimpleTarget();
     t.catchMe( "i am the argument" );
    

        a.deinstall( SimpleTarget.class );

//     a.deinstall( Reflection.getInnerClassOf( "SimpleInner", SimpleTarget.class ) );
    
     t.catchMe( "i am the argument" );
   }
  
   public void simpleCallTest() {
    
//     SimpleTarget s = new SimpleTarget();
//        System.out.println( "annotation present: "
//                + SimpleTarget.class.isAnnotationPresent( OnLoadMixIn.class ) );
//       MyCallAspect a = new MyCallAspect();
     PointcutFactory factory = new PointcutFactory();
       
    factory.
            addModifierList( Modifier.PUBLIC ).
            addModifierList( Modifier.PRIVATE ).
            addReturnType( long.class ).
//            addDeclaringClass( SimpleTarget.class ).
            addMethodName( "doIt" ).
            addParamTypeList( new Class[] { double.class, int.class, long.class, boolean[].class } ).
            addParamTypeList( new Class[] { double.class, ArrayList.class } );
   
        AspectAgent.install(
                SimpleTarget.class.getPackage(),
                MyCallAspect.class, factory.getPattern(), WeaveType.CALL );

//        a.installAroundCall( SimpleTarget.class, factory.getPattern() );
    
        SimpleTarget s = new SimpleTarget();
        s.catchMe( "arg1" );
       
     AspectAgent.deinstall( SimpleTarget.class.getPackage(), MyCallAspect.class );
//    
     s.catchMe( "arg1" );
       
   }
  
   
    public void cFlowTest() {
    
       
        DefaultBeforeAfterAdvice a = new PrintoutAspect();
        List<CFlowCondition> l = new ArrayList<CFlowCondition>();
       
        l.add( new CFlowCondition( new Class[] { SimpleTarget.class },
                new PointcutFactory().addMethodName("doIt").addParamTypeList(
                        double.class, ArrayList.class ).getPattern(), true ) );
       
        a.install(
            Reflection.getInnerClassOf( "SimpleInner", SimpleTarget.class ),
            new PointcutFactory().addMethodName( "doIt" ).getPattern(),
            new CFlow( WeaveType.EXECUTION, l ) );
           
        SimpleTarget t = new SimpleTarget();
        t.catchMe( "i am the argument" );
           
        new Thread( new Runnable() {
            public void run() {
                System.out.println( "im a different thread" );
                new SimpleTarget().difThread();
            }
        } ).start();
       
//        a.deinstall( Reflection.getInnerClassOf( "SimpleInner", SimpleTarget.class ) );
//           
//        t.catchMe( "i am the argument" );
    }
  
     public static void main (String[] args) {
       
          new Test().simpleTest();

//        new Test().simpleCallTest();
       
//        new Test().cFlowTest();
       
//         AspectAgent.addMixIn( new MyMixin() );
//         AspectAgent.installMixIns();
//        
//         SimpleTarget t = new SimpleTarget();
//        
//         if( t instanceof MyMixInInterface ) {
//             String s = ((MyMixInInterface)t).doMe(7l, 3);
//             System.out.println( s );
//         }
        
       
//       new Test().multiTest();
     }
}
TOP

Related Classes of de.petris.dynamicaspects.test.Test

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.