abstractfield.java

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

JAVA
1,025
字号
/* * 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.field;import com.caucho.amber.expr.AmberExpr;import com.caucho.amber.expr.PathExpr;import com.caucho.amber.manager.AmberConnection;import com.caucho.amber.manager.AmberPersistenceUnit;import com.caucho.amber.query.QueryParser;import com.caucho.amber.table.AmberTable;import com.caucho.amber.table.AmberColumn;import com.caucho.amber.type.BeanType;import com.caucho.amber.type.EntityType;import com.caucho.bytecode.JType;import com.caucho.bytecode.JTypeWrapper;import com.caucho.config.ConfigException;import com.caucho.java.JavaWriter;import com.caucho.util.CharBuffer;import com.caucho.util.L10N;import java.io.IOException;import java.io.Serializable;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashSet;import java.util.logging.Level;import java.util.logging.Logger;/** * Configuration for a bean's property */abstract public class AbstractField implements AmberField {  private static final L10N L = new L10N(AbstractField.class);  private static final Logger log    = Logger.getLogger(AbstractField.class.getName());  final BeanType _sourceType;  private String _name;  private JType _type;  private Method _getterMethod;  private Method _setterMethod;  private boolean _isLazy = true;  private boolean _isOverride;  private int _updateIndex;  private int _loadGroupIndex = -1;  AbstractField(BeanType sourceType)  {    _sourceType = sourceType;  }  AbstractField(BeanType sourceType, String name)    throws ConfigException  {    this(sourceType);    setName(name);        if (log.isLoggable(Level.FINER))      log.finer(_sourceType + " field " + this);  }  /**   * Sets the name.   */  public void setName(String name)    throws ConfigException  {    _name = name;    ClassLoader loader      = getSourceType().getPersistenceUnit().getTempClassLoader();          if (! isFieldAccess()) {      char ch = name.charAt(0);      if (Character.isLowerCase(ch))        name = Character.toUpperCase(ch) + name.substring(1);      String getter = "get" + name;      String setter = "set" + name;      _getterMethod = BeanType.getGetter(getBeanClass(), getter);      if (_getterMethod == null) {        getter = "is" + name;        _getterMethod = BeanType.getGetter(getBeanClass(), getter);      }      /* jpa/0u21      if (_getterMethod == null)        throw new ConfigException(L.l("{0}: {1} has no matching getter.",                                      getBeanClass().getName(), name));      */          if (_getterMethod == null) {        Field field = BeanType.getField(getBeanClass(), _name);        if (field == null)          throw new ConfigException(L.l("{0}: {1} has no matching field.",                                        getBeanClass().getName(), _name));        _type = JTypeWrapper.create(field.getGenericType(), loader);      }      else {        _type = JTypeWrapper.create(_getterMethod.getGenericReturnType(),				    loader);        _setterMethod = BeanType.getSetter(getBeanClass(), setter);      }    }    else {      Field field = BeanType.getField(getBeanClass(), name);      if (field == null)        throw new ConfigException(L.l("{0}: {1} has no matching field.",                                      getBeanClass().getName(), name));      _type = JTypeWrapper.create(field.getGenericType(), loader);    }    /*      if (_setterMethod == null && ! isAbstract())      throw new ConfigException(L.l("{0}: {1} has no matching setter.",      getBeanClass().getName(), name));    */  }  /**   * Returns the field name.   */  public String getName()  {    return _name;  }  /**   * Sets the java type.   */  protected void setJavaType(JType type)  {    _type = type;  }  /**   * Returns the owning entity class.   */  public BeanType getSourceType()  {    return _sourceType;  }  /**   * Returns the amber manager.   */  public AmberPersistenceUnit getPersistenceUnit()  {    return getSourceType().getPersistenceUnit();  }  /**   * Returns the bean class.   */  public Class getBeanClass()  {    return getSourceType().getBeanClass();  }  /**   * Returns the source type as   * entity or mapped-superclass.   */  public EntityType getEntitySourceType()  {    return (EntityType) getSourceType();  }  /**   * Returns the table containing the field's columns.   */  public AmberTable getTable()  {    return getEntitySourceType().getTable();  }  /**   * Returns the column for the field   */  public AmberColumn getColumn()  {    return null;  }  /**   * Returns the column for the field   */  public void setColumn(AmberColumn column)  {  }  /**   * Returns the property index.   */  public int getIndex()  {    return _updateIndex;  }  /**   * Set the property index.   */  public void setIndex(int index)  {    _updateIndex = index;  }  /**   * Returns the property's group index.   */  public int getLoadGroupIndex()  {    return _loadGroupIndex;  }  /**   * Returns the property's group index.   */  protected void setLoadGroupIndex(int index)  {    _loadGroupIndex = index;  }  /**   * Returns the load group mask.   */  public long getCreateLoadMask(int group)  {    int index = getLoadGroupIndex();    if (64 * group <= index && index < 64 * (group + 1))      return 1L << (index % 64);    else      return 0;  }  /**   * Returns true for a lazy field.   */  public boolean isLazy()  {    return _isLazy;  }  /**   * Set true for a lazy field.   */  public void setLazy(boolean isLazy)  {    _isLazy = isLazy;  }  /**   * Returns true for an override   */  public boolean isOverride()  {    return _isOverride;  }  /**   * Returns true for an override   */  public void setOverride(boolean isOverride)  {    _isOverride = isOverride;  }  /**   * Returns true for a key   */  public boolean isKey()  {    return false;  }  /**   * Returns the getter name.   */  public String getJavaTypeName()  {    return getJavaTypeName(getJavaClass());  }  /**   * Returns the Java code for the type.   */  private String getJavaTypeName(Class cl)  {    if (cl.isArray())      return getJavaTypeName(cl.getComponentType()) + "[]";    else      return cl.getName();  }  /**   * Returns the field's type   */  public JType getJavaType()  {    return _type;  }    /**   * Returns the field's class   */  public Class getJavaClass()  {    return getJavaType().getRawType().getJavaClass();  }  /**   * Returns true if values are accessed by the fields.   */  public boolean isFieldAccess()  {    return getSourceType().isFieldAccess();  }  /**   * Returns true if the methods are abstract.   */  public boolean isAbstract()  {    // jpa/0u21    return (_getterMethod != null            && Modifier.isAbstract(_getterMethod.getModifiers()));  }  /**   * Returns true if the field is cascadable.   */  public boolean isCascadable()  {    return false;  }  /**   * Returns true if the methods are abstract.   */  public boolean isUpdateable()  {    return true;  }  /**   * Creates a copy of the field for a parent   */  public AmberField override(BeanType table)  {    throw new UnsupportedOperationException(getClass().getName());  }  /**   * Initialize the field.   */  public void init()    throws ConfigException  {    if (_loadGroupIndex < 0) {      if (_isLazy)        _loadGroupIndex = getEntitySourceType().nextLoadGroupIndex();      else        _loadGroupIndex = getEntitySourceType().getDefaultLoadGroupIndex();    }  }  /**   * Generates the post constructor initialization.   */  public void generatePostConstructor(JavaWriter out)    throws IOException  {  }  /**   * Generates any prologue.   */  public void generatePrologue(JavaWriter out, HashSet<Object> completedSet)    throws IOException  {    // CMP    if (isAbstract()) {      out.println();      out.print("public ");      out.print(getJavaTypeName());      out.print(" " + getFieldName() + ";");    }  }  //  // getter/setter code generation  //  /**   * Returns the getter method.   */  public Method getGetterMethod()  {    return _getterMethod;  }  /**   * Returns the setter method.   */  public Method getSetterMethod()  {    return _setterMethod;  }  /**   * Returns the getter name.   */  public String getGetterName()  {    if (isFieldAccess())      return "__caucho_get_" + getName();    else      return _getterMethod.getName();  }  /**   * Returns the setter name.   */  public String getSetterName()  {    if (isFieldAccess())      return "__caucho_set_" + getName();    else if (_setterMethod != null)      return _setterMethod.getName();    else      return "set" + getGetterName().substring(3);  }    /**   * Returns the actual data.   */  public String generateSuperGetter(String objThis)  {    if (! getSourceType().isEmbeddable())      return objThis + ".__caucho_super_get_" + getName() + "()";    else if (isFieldAccess())      return objThis + "." + getName();    else      return objThis + "." + getGetterMethod().getName() + "()";  }  /**   * Sets the actual data.   */  public String generateSuperSetter(String objThis, String value)  {    if (! getSourceType().isEmbeddable())      return objThis + "." + "__caucho_super_set_" + getName() + "(" + value + ")";    else if (isFieldAccess())      return objThis + "." + getName() + " = " + value;    else      return objThis + "." + getSetterMethod().getName() + "(" + value + ")";  }  /**   * Generates the field getter.   *   * @param value the non-null value   */  public String generateGet(String objThis)  {    if (objThis == null)      return generateNull();

⌨️ 快捷键说明

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