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

com.caucho.config.gen.AbstractAspectGenerator Maven / Gradle / Ivy

/*
 * Copyright (c) 1998-2018 Caucho Technology -- all rights reserved
 *
 * This file is part of Resin(R) Open Source
 *
 * Each copy or derived work must preserve the copyright notice and this
 * notice unmodified.
 *
 * Resin Open Source is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Resin Open Source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
 * of NON-INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Resin Open Source; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307  USA
 *
 * @author Scott Ferguson
 */
package com.caucho.config.gen;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import javax.ejb.ApplicationException;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.AnnotatedType;

import com.caucho.config.reflect.BaseTypeAnnotated;
import com.caucho.config.reflect.VarType;
import com.caucho.inject.Module;
import com.caucho.java.JavaWriter;

/**
 * Represents a filter for invoking a method
 */
@Module
abstract public class AbstractAspectGenerator implements AspectGenerator {
  private AspectFactory _factory;
  private AnnotatedMethod _method;
  private AspectGenerator _next;

  protected AbstractAspectGenerator(AspectFactory factory,
                                    AnnotatedMethod method,
                                    AspectGenerator next)
  {
    _factory = factory;
    _method = method;
    _next = next;
    
    if (next == null)
      throw new NullPointerException();
  }

  protected AspectFactory getFactory()
  {
    return _factory;
  }

  protected AspectBeanFactory getBeanFactory()
  {
    return _factory.getAspectBeanFactory();
  }
  
  protected String getInstanceName()
  {
    return getBeanFactory().getBeanInstance();
  }
  
  /**
   * Returns the owning bean type.
   */
  protected AnnotatedType getBeanType()
  {
    return _factory.getBeanType();
  }

  protected Class getJavaClass()
  {
    return getBeanType().getJavaClass();
  }
   
  @Override
  public AnnotatedMethod getMethod()
  {
    return _method;
  }
  
  protected Set> getTypeVariables()
  {
    AnnotatedMethod method = getMethod();
    
    if (method instanceof BaseTypeAnnotated) {
      BaseTypeAnnotated annType = (BaseTypeAnnotated) getMethod();
    
      Set> varSet = annType.getTypeVariables();
    
      return varSet;
    }
    else {
      return new HashSet>();
    }
  }
  
  /**
   * Returns the JavaMethod for this aspect.
   */
  protected Method getJavaMethod()
  {
    return _method.getJavaMember();
  }
  
  /**
   * Top-level generator.
   */
  public void generate(JavaWriter out,
                       HashMap prologueMap)
    throws IOException
  {
    throw new UnsupportedOperationException(getClass().getName());
  }
  
  /**
   * Generates the body of the method.
   *
   * 
   * MyType myMethod(...)
   * {
   *   [pre-try]
   *   try {
   *     [pre-call]
   *     [call]   // retValue = super.myMethod(...)
   *     [post-call]
   *     return retValue;
   *   } catch (RuntimeException e) {
   *     [system-exception]
   *     throw e;
   *   } catch (Exception e) {
   *     [application-exception]
   *     throw e;
   *   } finally {
   *     [finally]
   *   }
   * 
*/ protected void generateContent(JavaWriter out, HashMap map) throws IOException { generatePreTry(out); out.println(); out.println("try {"); out.pushDepth(); AnnotatedMethod method = getMethod(); if (! void.class.equals(method.getBaseType())) { out.printType(method.getBaseType()); out.println(" result;"); } generatePreCall(out); generateCall(out); generatePostCall(out); if (! void.class.equals(method.getBaseType())) out.println("return result;"); out.popDepth(); generateExceptions(out); out.println("} finally {"); out.pushDepth(); generateFinally(out); out.popDepth(); out.println("}"); generatePostFinally(out); } private void generateExceptions(JavaWriter out) throws IOException { if (! isApplicationExceptionThrown()) return; HashSet> exceptionSet = new HashSet>(); for (Class exn : getThrowsExceptions()) { exceptionSet.add(exn); } exceptionSet.add(RuntimeException.class); exceptionSet.add(Error.class); Class exn; while ((exn = selectException(exceptionSet)) != null) { boolean isSystemException = ((RuntimeException.class.isAssignableFrom(exn) || Error.class.isAssignableFrom(exn)) && ! exn.isAnnotationPresent(ApplicationException.class)); out.print("} catch ("); out.printClass(exn); out.println(" e) {"); out.pushDepth(); if (isSystemException) generateSystemException(out, exn); else generateApplicationException(out, exn); out.println(); out.println("throw e;"); out.popDepth(); } } private Class selectException(HashSet> exnSet) { for (Class exn : exnSet) { if (isMostSpecific(exn, exnSet)) { exnSet.remove(exn); return exn; } } return null; } private boolean isMostSpecific(Class exn, HashSet> exnSet) { for (Class testExn : exnSet) { if (exn == testExn) continue; if (exn.isAssignableFrom(testExn)) return false; } return true; } protected Class []getThrowsExceptions() { return getJavaMethod().getExceptionTypes(); } // // bean instance generation // /** * Generates the static class prologue */ @Override public void generateBeanPrologue(JavaWriter out, HashMap map) throws IOException { _next.generateBeanPrologue(out, map); } /** * Generates initialization in the constructor */ @Override public void generateBeanConstructor(JavaWriter out, HashMap map) throws IOException { _next.generateBeanConstructor(out, map); } /** * Generates initialization in the proxy constructor */ @Override public void generateProxyConstructor(JavaWriter out, HashMap map) throws IOException { _next.generateProxyConstructor(out, map); } /** * Generates extra inject code */ @Override public void generateInject(JavaWriter out, HashMap map) throws IOException { _next.generateInject(out, map); } /** * Generates @PostConstruct code */ @Override public void generatePostConstruct(JavaWriter out, HashMap map) throws IOException { _next.generatePostConstruct(out, map); } /** * Generates @PreDestroy code */ @Override public void generatePreDestroy(JavaWriter out, HashMap map) throws IOException { _next.generatePreDestroy(out, map); } /** * Generates epilogue */ @Override public void generateEpilogue(JavaWriter out, HashMap map) throws IOException { _next.generateEpilogue(out, map); } /** * Generates destroy code */ @Override public void generateDestroy(JavaWriter out, HashMap map) throws IOException { _next.generateDestroy(out, map); } // // business method interception // /** * Generates the static class prologue */ @Override public void generateMethodPrologue(JavaWriter out, HashMap map) throws IOException { _next.generateMethodPrologue(out, map); } /** * Generates pre-async dispatch code. */ @Override public void generateAsync(JavaWriter out) throws IOException { _next.generateAsync(out); } /** * Generates code before the try block */ @Override public void generatePreTry(JavaWriter out) throws IOException { _next.generatePreTry(out); } /** * Generates code before the call, in the try block. *
   * retType myMethod(...)
   * {
   *   try {
   *     [pre-call]
   *     value = bean.myMethod(...);
   *     ...
   * }
   * 
*/ @Override public void generatePreCall(JavaWriter out) throws IOException { _next.generatePreCall(out); } /** * Generates the method interception code */ @Override public void generateCall(JavaWriter out) throws IOException { _next.generateCall(out); } /** * Generates code after the call, before the return. *
   * retType myMethod(...)
   * {
   *   try {
   *     ...
   *     value = bean.myMethod(...);
   *     [post-call]
   *     return value;
   *   } finally {
   *     ...
   *   }
   * }
   * 
*/ @Override public void generatePostCall(JavaWriter out) throws IOException { _next.generatePostCall(out); } /** * Returns true if the application exception can be thrown. */ @Override public boolean isApplicationExceptionThrown() { return _next.isApplicationExceptionThrown(); } /** * Generates application (checked) exception code for * the method. */ @Override public void generateApplicationException(JavaWriter out, Class exn) throws IOException { _next.generateApplicationException(out, exn); } /** * Generates system (runtime) exception code for * the method. */ @Override public void generateSystemException(JavaWriter out, Class exn) throws IOException { _next.generateSystemException(out, exn); } /** * Generates finally code for the method */ @Override public void generateFinally(JavaWriter out) throws IOException { _next.generateFinally(out); } @Override public void generatePostFinally(JavaWriter out) throws IOException { _next.generatePostFinally(out); } protected Z getAnnotation(Class annotationType, AnnotatedMethod apiMethod, AnnotatedMethod implMethod) { Z annotation; annotation = apiMethod.getAnnotation(annotationType); if ((annotation == null) && (implMethod != null)) { annotation = implMethod.getAnnotation(annotationType); } return annotation; } protected Z getAnnotation(Class annotationType, AnnotatedType apiClass, AnnotatedType implClass) { Z annotation = null; if (apiClass != null) annotation = apiClass.getAnnotation(annotationType); if ((annotation == null) && (implClass != null)) { annotation = implClass.getAnnotation(annotationType); } return annotation; } protected Z getAnnotation(Class annotationType, AnnotatedMethod apiMethod, AnnotatedType apiClass, AnnotatedMethod implementationMethod, AnnotatedType implementationClass) { Z annotation = null; if (apiMethod != null) { annotation = apiMethod.getAnnotation(annotationType); } if (annotation == null && apiClass != null) { annotation = apiClass.getAnnotation(annotationType); } if ((annotation == null) && (implementationMethod != null)) { annotation = implementationMethod.getAnnotation(annotationType); } if ((annotation == null) && (implementationClass != null)) { annotation = implementationClass.getAnnotation(annotationType); } return annotation; } @Override public String toString() { return getClass().getSimpleName() + "[" + _method + "]"; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy