ambermappedcomponent.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,961 行 · 第 1/4 页

JAVA
1,961
字号
/* * 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.*;import com.caucho.amber.table.AmberColumn;import com.caucho.amber.table.AmberTable;import com.caucho.amber.type.EntityType;import com.caucho.amber.type.MappedSuperclassType;import com.caucho.amber.type.EntityType;import com.caucho.bytecode.*;import com.caucho.java.JavaWriter;import com.caucho.java.gen.ClassComponent;import com.caucho.loader.Environment;import com.caucho.util.L10N;import com.caucho.vfs.PersistentDependency;import javax.persistence.CascadeType;import java.io.IOException;import java.lang.reflect.Constructor;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.util.ArrayList;import java.util.HashSet;/** * Generates the Java code for the wrapped object. */abstract public class AmberMappedComponent extends ClassComponent {  private static final L10N L = new L10N(AmberMappedComponent.class);  String _baseClassName;  String _extClassName;  EntityType _entityType;  private ArrayList<PersistentDependency> _dependencies    = new ArrayList<PersistentDependency>();  public AmberMappedComponent()  {  }  /**   * Sets the bean info for the generator   */  void setRelatedType(EntityType entityType)  {    _entityType = entityType;    _dependencies.addAll(entityType.getDependencies());    for (int i = 0; i < _dependencies.size(); i++)      Environment.addDependency(_dependencies.get(i));  }  public EntityType getEntityType()  {    return _entityType;  }  /**   * Sets the base class name   */  public void setBaseClassName(String baseClassName)  {    _baseClassName = baseClassName;  }  /**   * Gets the base class name   */  public String getBaseClassName()  {    return _baseClassName;  }  /**   * Sets the ext class name   */  public void setExtClassName(String extClassName)  {    _extClassName = extClassName;  }  /**   * Sets the ext class name   */  public String getClassName()  {    return _extClassName;  }  /**   * Get bean class name.   */  public String getBeanClassName()  {    // return _entityType.getBeanClass().getName();    return _baseClassName;  }  /**   * Returns the dependencies.   */  public ArrayList<PersistentDependency> getDependencies()  {    return _dependencies;  }  protected boolean isEntityParent()  {    EntityType parentType = getEntityType().getParentType();      // jpa/0gg0    return ((parentType != null) && parentType.isEntity());  }  /**   * Starts generation of the Java code   */  @Override  public final void generate(JavaWriter out)    throws IOException  {    try {      EntityType parentType = getEntityType().getParentType();      generateHeader(out, isEntityParent());      generateInit(out);      HashSet<Object> completedSet = new HashSet<Object>();      generatePrologue(out, completedSet);      generateGetCacheEntity(out);      generateGetEntityType(out);      if (! isEntityParent())        generateGetEntityState(out);      generateIsLoaded(out);            generateIsDirty(out);      generateMatch(out);      generateFields(out);      generateMethods(out);      generateDetach(out, isEntityParent());      generateLoad(out, isEntityParent());      int min = 0;      if (isEntityParent())        min = getEntityType().getParentType().getLoadGroupIndex() + 1;      int max = getEntityType().getLoadGroupIndex();      for (int i = min; i <= max; i++)        generateLoadGroup(out, i);      generateResultSetLoad(out, isEntityParent());      generateSetQuery(out, isEntityParent());      generateMerge(out);      generateSetLoadMask(out);      generateMakePersistent(out);      generateCascadePersist(out);      generateCascadeRemove(out);      generateCreate(out);      generateDelete(out);      generateDeleteForeign(out);      generateFlush(out);      generateIncrementVersion(out);      generateAfterCommit(out, isEntityParent());      generateAfterRollback(out);      generateLoadKey(out);      generateHome(out);      // printDependList(out, _dependencies);    } catch (IOException e) {      throw e;    }  }  /**   * Generates the class header for the generated code.   */  void generateHeader(JavaWriter out,                      boolean isEntityParent)    throws IOException  {    out.println("/*");    out.println(" * Generated by Resin Amber");    out.println(" * " + com.caucho.Version.VERSION);    out.println(" */");    out.print("private static final java.util.logging.Logger __caucho_log = ");    out.println("java.util.logging.Logger.getLogger(\"" + getBeanClassName() + "\");");    // jpa/0ge3 if (! isEntityParent) {    if (_entityType.getParentType() == null) {      out.println();      out.println("protected transient com.caucho.amber.type.EntityType __caucho_home;");      out.println("public transient com.caucho.amber.entity.EntityItem __caucho_cacheItem;");      out.println("protected transient com.caucho.amber.manager.AmberConnection __caucho_session;");      out.println("protected transient com.caucho.amber.entity.EntityState __caucho_state = com.caucho.amber.entity.EntityState.TRANSIENT;");      // XXX: needs to generate load masks for groups in the subclasses,      // but the group numbering should not always start at zero.      int loadCount = _entityType.getLoadGroupIndex();      for (int i = 0; i <= loadCount / 64; i++) {        out.println("protected transient long __caucho_loadMask_" + i + ";");      }      int dirtyCount = _entityType.getDirtyIndex();      for (int i = 0; i <= dirtyCount / 64; i++) {        out.println("protected transient long __caucho_dirtyMask_" + i + ";");        out.println("protected transient long __caucho_updateMask_" + i + ";");      }      out.println("protected transient boolean __caucho_inc_version;");    }  }  /**   * Generates the init generated code.   */  void generateInit(JavaWriter out)    throws IOException  {    if (isEntityParent())      return;    String className = getClassName();    int p = className.lastIndexOf('.');    if (p > 0)      className = className.substring(p + 1);    ArrayList<AmberField> fields = _entityType.getFields();    Class beanClass = _entityType.getBeanClass();    for (Constructor ctor : beanClass.getDeclaredConstructors()) {      out.println();      // XXX: s/b actual access type?      out.print("public ");      out.print(className);      out.print("(");      Class []args = ctor.getParameterTypes();      for (int i = 0; i < args.length; i++) {        if (i != 0)          out.print(", ");        out.printClass(args[i]);        out.print(" a" + i);      }      out.println(")");      out.println("{");      out.pushDepth();      out.print("super(");      for (int i = 0; i < args.length; i++) {        if (i != 0)          out.print(", ");        out.print("a" + i);      }      out.println(");");      // jpa/0l14      out.println("__caucho_state = com.caucho.amber.entity.EntityState.TRANSIENT;");      // jpa/0gh2: compound pk and constructor with arguments.      if (_entityType.getId() instanceof CompositeId) {        out.println("try {");        out.println("  __caucho_setPrimaryKey(__caucho_getPrimaryKey());");        out.println("} catch (Exception e) {");        out.println("  __caucho_log.fine(\"amber unable to set primary key within argument constructor \" + this.getClass().getName() + \"[PK: unknown]\");");        out.println("}");      }      for (AmberField field : fields) {        field.generatePostConstructor(out);      }      out.popDepth();      out.println("}");    }    Id id = _entityType.getId();    if (id == null && _entityType.isEntity())        throw new IllegalStateException(L.l("'{0}' is missing a key.",                                            _entityType.getName()));    boolean isAbstract      = Modifier.isAbstract(_entityType.getBeanClass().getModifiers());    out.println();    out.println("public void __caucho_setPrimaryKey(Object key)");    out.println("{");    out.pushDepth();    if (id != null && ! isAbstract)      id.generateSet(out, "super", id.generateCastFromObject("key"));    out.popDepth();    out.println("}");    out.println();    out.println("public Object __caucho_getPrimaryKey()");    out.println("{");    out.pushDepth();    out.println("try {");    out.pushDepth();    out.print("return ");    if (id == null || isAbstract)      out.print("null");    else      out.print(id.toObject(id.generateGet("super")));    out.println(";");    out.popDepth();    out.println("} catch (Exception e) {");    out.println("  throw new com.caucho.amber.AmberRuntimeException(e);");    out.println("}");    out.popDepth();    out.println("}");    out.println();    out.println("public void __caucho_setConnection(com.caucho.amber.manager.AmberConnection aConn)");    out.println("{");    out.println("  __caucho_session = aConn;");    out.println("}");    out.println();    out.println("public com.caucho.amber.manager.AmberConnection __caucho_getConnection()");    out.println("{");    out.println("  return __caucho_session;");    out.println("}");    generateExpire(out);  }  /**   * Generates the expire code.   */  void generateExpire(JavaWriter out)    throws IOException  {    out.println();    out.println("public void __caucho_expire()");    out.println("{");    out.pushDepth();    generateLogFine(out, " amber expire");    out.println();    int loadCount = _entityType.getLoadGroupIndex();    for (int i = 0; i <= loadCount / 64; i++) {      out.println("__caucho_loadMask_" + i + " = 0L;");    }    _entityType.generateExpire(out);    out.popDepth();    out.println("}");  }  /**   * Generates the isDirty code.   */  void generateIsDirty(JavaWriter out)    throws IOException  {    out.println();    out.println("public boolean __caucho_isDirty()");    out.println("{");    out.pushDepth();    int dirtyCount = _entityType.getDirtyIndex();    for (int i = 0; i <= dirtyCount / 64; i++) {      out.println("if (__caucho_dirtyMask_" + i + " != 0L)");      out.println("  return true;");      out.println();    }    out.println("return false;");    out.popDepth();    out.println("}");  }  /**   * Generates the isLoaded code.   */  void generateIsLoaded(JavaWriter out)    throws IOException  {    out.println();    out.println("public boolean __caucho_isLoaded()");    out.println("{");    out.pushDepth();    out.println("return __caucho_loadMask_0 != 0L;");    out.popDepth();    out.println("}");  }  /**   * Generates the match code.   */  void generateMatch(JavaWriter out)    throws IOException  {    out.println();    out.println("public boolean __caucho_match(Class cl, Object key)");    out.println("{");    out.pushDepth();    /*      out.println("if (! (" + getBeanClassName() + ".class.isAssignableFrom(cl)))");      out.println("  return false;");    */    out.println("if (" + getBeanClassName() + ".class  != cl)");    out.println("  return false;");    out.println("else {");    out.pushDepth();    Id id = _entityType.getId();    // jpa/0gg0    if (id == null || Modifier.isAbstract(_entityType.getBeanClass().getModifiers())) {      // jpa/0ge6: MappedSuperclass

⌨️ 快捷键说明

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