amberenhancer.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 743 行 · 第 1/2 页

JAVA
743
字号
/* * Copyright (c) 1998-2008 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.amber.gen;import com.caucho.amber.field.AmberField;import com.caucho.amber.manager.AmberContainer;import com.caucho.amber.type.*;import com.caucho.bytecode.Analyzer;import com.caucho.bytecode.CodeVisitor;import com.caucho.bytecode.ConstantPool;import com.caucho.bytecode.FieldRefConstant;import com.caucho.bytecode.JClass;import com.caucho.bytecode.JavaClass;import com.caucho.bytecode.JavaMethod;import com.caucho.bytecode.MethodRefConstant;import com.caucho.config.ConfigException;import com.caucho.java.JavaCompiler;import com.caucho.java.WorkDir;import com.caucho.java.gen.ClassComponent;import com.caucho.java.gen.DependencyComponent;import com.caucho.java.gen.GenClass;import com.caucho.java.gen.JavaClassGenerator;import com.caucho.loader.*;import com.caucho.loader.enhancer.ClassEnhancer;import com.caucho.loader.enhancer.EnhancerPrepare;import com.caucho.util.L10N;import com.caucho.vfs.Path;import com.caucho.vfs.Vfs;import java.io.IOException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.logging.Level;import java.util.logging.Logger;/** * Enhancing the java objects for Amber mapping. */public class AmberEnhancer implements AmberGenerator, ClassEnhancer {  private static final L10N L = new L10N(AmberEnhancer.class);  private static final Logger log     = Logger.getLogger(AmberEnhancer.class.getName());  private Path _configDirectory;  private boolean _useHibernateFiles;  private AmberContainer _amberContainer;  private EnhancerPrepare _prepare;  private Path _workDir;  private Path _postWorkDir;  private ArrayList<String> _pendingClassNames = new ArrayList<String>();  public AmberEnhancer(AmberContainer amberContainer)  {    _amberContainer = amberContainer;    _workDir = WorkDir.getLocalWorkDir().lookup("pre-enhance");    _postWorkDir = WorkDir.getLocalWorkDir().lookup("post-enhance");    _prepare = new EnhancerPrepare();    _prepare.setClassLoader(Thread.currentThread().getContextClassLoader());    _prepare.setWorkPath(WorkDir.getLocalWorkDir());    _prepare.addEnhancer(this);  }  /**   * Sets the config directory.   */  public void setConfigDirectory(Path dir)  {    _configDirectory = dir;  }  /**   * Returns the work directory.   */  public Path getWorkDir()  {    return _workDir;  }  /**   * Returns the work directory.   */  public Path getPostWorkDir()  {    return _postWorkDir;  }  /**   * Initialize the enhancer.   */  public void init()    throws Exception  {  }  /**   * Checks to see if the preloaded class is modified.   */  protected boolean isModified(Class preloadedClass)  {    try {      Method init = preloadedClass.getMethod("_caucho_init",                                             new Class[] { Path.class });      if (_configDirectory != null)        init.invoke(null, new Object[] { _configDirectory });      else        init.invoke(null, new Object[] { Vfs.lookup() });      Method isModified = preloadedClass.getMethod("_caucho_is_modified",                                                   new Class[0]);      Object value = isModified.invoke(null, new Object[0]);      if (Boolean.FALSE.equals(value)) {        loadEntityType(preloadedClass, preloadedClass.getClassLoader());        return false;      }      else        return true;    } catch (Throwable e) {      log.log(Level.FINER, e.toString(), e);      return true;    }  }  /**   * Returns true if the class should be enhanced.   */  public boolean shouldEnhance(String className)  {    className = className.replace('/', '.');        int p = className.lastIndexOf('-');    if (p > 0)      className = className.substring(0, p);    p = className.lastIndexOf('$');    if (p > 0)      className = className.substring(0, p);    AbstractEnhancedType type;    type = _amberContainer.getEntity(className);    if (type != null && type.isEnhanced())      return true;    type = _amberContainer.getMappedSuperclass(className);    if (type != null && type.isEnhanced())      return true;    type = _amberContainer.getEmbeddable(className);    if (type != null && type.isEnhanced())      return true;    type = _amberContainer.getListener(className);    if (type != null && type.isEnhanced())      return true;    return false;    /*      Thread thread = Thread.currentThread();      ClassLoader oldLoader = thread.getContextClassLoader();      try {      thread.setContextClassLoader(getRawLoader());      Class baseClass = Class.forName(className, false, getRawLoader());      type = loadEntityType(baseClass, getRawLoader());      } catch (ClassNotFoundException e) {      return false;      } finally {      thread.setContextClassLoader(oldLoader);      }      if (type == null)      return false;      return className.equals(type.getName()) || type.isFieldAccess();    */  }  /**   * Returns true if the class should be enhanced.   */  private EntityType loadEntityType(Class cl, ClassLoader loader)  {    EntityType parentType = null;    for (; cl != null; cl = cl.getSuperclass()) {      java.net.URL url;      String className = cl.getName();      EntityType type = _amberContainer.getEntity(className);      if (parentType == null)        parentType = type;      if (type != null && ! type.startConfigure())        return type;      type = loadEntityTypeImpl(cl, loader);      if (type != null && ! type.startConfigure())        return type;    }    return parentType;  }  protected EntityType loadEntityTypeImpl(Class cl, ClassLoader rawLoader)  {    return null;  }  /**   * Enhances the class.   */  public void preEnhance(JavaClass baseClass)    throws Exception  {    EntityType type = _amberContainer.getEntity(baseClass.getName());    if (type == null)      type = _amberContainer.getMappedSuperclass(baseClass.getName());    if (type != null && type.getParentType() != null) {      String parentClass = type.getParentType().getInstanceClassName();      baseClass.setSuperClass(parentClass.replace('.', '/'));    }  }  /**   * Enhances the class.   */  public void enhance(GenClass genClass,                      JClass baseClass,                      String extClassName)    throws Exception  {    String className = baseClass.getName();    EntityType type = _amberContainer.getEntity(className);    if (type == null)      type = _amberContainer.getMappedSuperclass(className);    // Type can be null for subclasses and inner classes that need fixups    if (type != null) {      // type is EntityType or MappedSuperclassType      log.info("Amber enhancing class " + className);      // XXX: _amberContainerenceUnitenceUnit.configure();      type.init();      genClass.addInterfaceName(type.getComponentInterfaceName());      genClass.addImport("java.util.logging.*");      genClass.addImport("com.caucho.amber.manager.*");      genClass.addImport("com.caucho.amber.entity.*");      genClass.addImport("com.caucho.amber.type.*");      AmberMappedComponent componentGenerator	= (AmberMappedComponent) type.getComponentGenerator();            componentGenerator.setRelatedType((EntityType) type);      componentGenerator.setBaseClassName(baseClass.getName());      componentGenerator.setExtClassName(extClassName);      genClass.addComponent(componentGenerator);      DependencyComponent dependency = genClass.addDependencyComponent();      dependency.addDependencyList(type.getDependencies());      return;      //_amberContainerenceUnitenceUnit.generate();      // generate(type);      // compile();      // XXX: _amberContainerenceUnitenceUnit.initEntityHomes();    }    ListenerType listenerType = _amberContainer.getListener(className);    // Type can be null for subclasses and inner classes that need fixups    if (listenerType != null) {      if (log.isLoggable(Level.INFO))        log.log(Level.INFO, "Amber enhancing class " + className);      listenerType.init();      genClass.addInterfaceName("com.caucho.amber.entity.Listener");      ListenerComponent listener = new ListenerComponent();      listener.setListenerType(listenerType);      listener.setBaseClassName(baseClass.getName());      listener.setExtClassName(extClassName);      genClass.addComponent(listener);    }    EmbeddableType embeddableType = _amberContainer.getEmbeddable(className);    // Type can be null for subclasses and inner classes that need fixups    if (embeddableType != null) {      if (log.isLoggable(Level.INFO))        log.log(Level.INFO, "Amber enhancing class " + className);      embeddableType.init();      genClass.addInterfaceName("com.caucho.amber.entity.Embeddable");      EmbeddableComponent embeddable = new EmbeddableComponent();      embeddable.setEmbeddableType(embeddableType);      embeddable.setBaseClassName(baseClass.getName());      embeddable.setExtClassName(extClassName);      genClass.addComponent(embeddable);    }  }  /**   * Generates the type.   */  public void generate(AbstractEnhancedType type)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?