Package org.apache.bcel.classfile

Examples of org.apache.bcel.classfile.Field


   
    String field_name = o.getFieldName(cpg);
   
    JavaClass jc = Repository.lookupClass(o.getClassType(cpg).getClassName());
    Field[] fields = jc.getFields();
    Field f = null;
    for (int i=0; i<fields.length; i++){
      if (fields[i].getName().equals(field_name)){
          Type f_type = Type.getType(fields[i].getSignature());
          Type o_type = o.getType(cpg);
          /* TODO: Check if assignment compatibility is sufficient.
           * What does Sun do?
           */
          if (f_type.equals(o_type)){
            f = fields[i];
            break;
          }
      }
    }
    if (f == null){
      throw new AssertionViolatedException("Field not found?!?");
    }

    if (f.isProtected()){
      ObjectType classtype = o.getClassType(cpg);
      ObjectType curr = new ObjectType(mg.getClassName());

      if classtype.equals(curr) ||
            curr.subclassOf(classtype)  ){
        Type t = stack().peek();
        if (t == Type.NULL){
          return;
        }
        if (! (t instanceof ObjectType) ){
          constraintViolated(o, "The 'objectref' must refer to an object that's not an array. Found instead: '"+t+"'.");
        }
        ObjectType objreftype = (ObjectType) t;
        if (! ( objreftype.equals(curr) ||
                objreftype.subclassOf(curr) ) ){
          //TODO: One day move to Staerk-et-al's "Set of object types" instead of "wider" object types
          //      created during the verification.
          //      "Wider" object types don't allow us to check for things like that below.
          //constraintViolated(o, "The referenced field has the ACC_PROTECTED modifier, and it's a member of the current class or a superclass of the current class. However, the referenced object type '"+stack().peek()+"' is not the current class or a subclass of the current class.");
        }
      }
    }
   
    // TODO: Could go into Pass 3a.
    if (f.isStatic()){
      constraintViolated(o, "Referenced field '"+f+"' is static which it shouldn't be.");
    }

      } catch (ClassNotFoundException e) {
    // FIXME: maybe not the best way to handle this
View Full Code Here


   
    String field_name = o.getFieldName(cpg);
   
    JavaClass jc = Repository.lookupClass(o.getClassType(cpg).getClassName());
    Field[] fields = jc.getFields();
    Field f = null;
    for (int i=0; i<fields.length; i++){
      if (fields[i].getName().equals(field_name)){
          Type f_type = Type.getType(fields[i].getSignature());
          Type o_type = o.getType(cpg);
          /* TODO: Check if assignment compatibility is sufficient.
           * What does Sun do?
           */
          if (f_type.equals(o_type)){
            f = fields[i];
            break;
          }
      }
    }
    if (f == null){
      throw new AssertionViolatedException("Field not found?!?");
    }

    Type value = stack().peek();
    Type t = Type.getType(f.getSignature());
    Type shouldbe = t;
    if (shouldbe == Type.BOOLEAN ||
        shouldbe == Type.BYTE ||
        shouldbe == Type.CHAR ||
        shouldbe == Type.SHORT){
      shouldbe = Type.INT;
    }
    if (t instanceof ReferenceType){
      ReferenceType rvalue = null;
      if (value instanceof ReferenceType){
        rvalue = (ReferenceType) value;
        referenceTypeIsInitialized(o, rvalue);
      }
      else{
        constraintViolated(o, "The stack top type '"+value+"' is not of a reference type as expected.");
      }
      // TODO: This can possibly only be checked using Staerk-et-al's "set-of-object types", not
      // using "wider cast object types" created during verification.
      // Comment it out if you encounter problems. See also the analogon at visitPUTSTATIC.
      if (!(rvalue.isAssignmentCompatibleWith(shouldbe))){
        constraintViolated(o, "The stack top type '"+value+"' is not assignment compatible with '"+shouldbe+"'.");
      }
    }
    else{
      if (shouldbe != value){
        constraintViolated(o, "The stack top type '"+value+"' is not of type '"+shouldbe+"' as expected.");
      }
    }
   
    if (f.isProtected()){
      ObjectType classtype = o.getClassType(cpg);
      ObjectType curr = new ObjectType(mg.getClassName());

      if classtype.equals(curr) ||
            curr.subclassOf(classtype)  ){
        Type tp = stack().peek(1);
        if (tp == Type.NULL){
          return;
        }
        if (! (tp instanceof ObjectType) ){
          constraintViolated(o, "The 'objectref' must refer to an object that's not an array. Found instead: '"+tp+"'.");
        }
        ObjectType objreftype = (ObjectType) tp;
        if (! ( objreftype.equals(curr) ||
                objreftype.subclassOf(curr) ) ){
          constraintViolated(o, "The referenced field has the ACC_PROTECTED modifier, and it's a member of the current class or a superclass of the current class. However, the referenced object type '"+stack().peek()+"' is not the current class or a subclass of the current class.");
        }
      }
    }

    // TODO: Could go into Pass 3a.
    if (f.isStatic()){
      constraintViolated(o, "Referenced field '"+f+"' is static which it shouldn't be.");
    }

      } catch (ClassNotFoundException e) {
    // FIXME: maybe not the best way to handle this
View Full Code Here

  public void visitPUTSTATIC(PUTSTATIC o){
      try {
    String field_name = o.getFieldName(cpg);
    JavaClass jc = Repository.lookupClass(o.getClassType(cpg).getClassName());
    Field[] fields = jc.getFields();
    Field f = null;
    for (int i=0; i<fields.length; i++){
      if (fields[i].getName().equals(field_name)){
          Type f_type = Type.getType(fields[i].getSignature());
          Type o_type = o.getType(cpg);
          /* TODO: Check if assignment compatibility is sufficient.
           * What does Sun do?
           */
          if (f_type.equals(o_type)){
            f = fields[i];
            break;
          }
      }
    }
    if (f == null){
      throw new AssertionViolatedException("Field not found?!?");
    }
    Type value = stack().peek();
    Type t = Type.getType(f.getSignature());
    Type shouldbe = t;
    if (shouldbe == Type.BOOLEAN ||
        shouldbe == Type.BYTE ||
        shouldbe == Type.CHAR ||
        shouldbe == Type.SHORT){
View Full Code Here

                        found = true;
                    }
                }
                if (!found)
                {
                    Field field = BCELUtils.getFieldByName(fieldName, newClass);
                    if (field == null)
                    {
                        if (!fmd.isProperty())
                        {
                            throw new RuntimeException(LOCALISER.msg("Enhancer.ClassHasNoSuchField", newClass.getClassName(), fieldName));
View Full Code Here

        for (int i = 0; i < fieldsToAdd.size(); i++)
        {
            ClassField cf = (ClassField) fieldsToAdd.get(i);

            FieldGen gen = new FieldGen(cf.getAccess(), (Type) cf.getType(), cf.getName(), constantPoolGen);
            Field f = gen.getField();
            newClass.addField(f);
            BCELUtils.addSynthetic(f, constantPoolGen);
            if (JPOXLogger.ENHANCER.isDebugEnabled())
            {
                JPOXLogger.ENHANCER.debug(LOCALISER.msg("Enhancer.AddField", f.getType() + " " + f.getName()));
            }
            newClass.update();
        }
    }
View Full Code Here

        {
            Instruction i = ih.getInstruction();

            if ((i instanceof GETFIELD) || (i instanceof PUTFIELD))
            {
                Field f;
                FieldInstruction field = (FieldInstruction) i;
                Constant c = m.getConstantPool().getConstant(field.getIndex());
                ConstantFieldref fieldRef = (ConstantFieldref) c;

                ConstantClass cclass = (ConstantClass) m.getConstantPool().getConstant(fieldRef.getClassIndex());
                ConstantUtf8 utfClassName = (ConstantUtf8) m.getConstantPool().getConstant(cclass.getNameIndex());
                String utfClassNameString = StringUtils.replaceAll(utfClassName.getBytes().toString(), "/", ".");
                JavaClass fieldJavaClass = null;
                try
                {
                    fieldJavaClass = Repository.lookupClass(utfClassNameString);
                }
                catch (Throwable ex)
                {
                    // catch Throwable, so it is compatible with latest BCEL changes in methods signature.
                    // It nows raises ClassNotFoundException. In order to be able to compible this code
                    // with bcel-5.1 or bcel-5.1+, we catch as throwable
                    JPOXLogger.ENHANCER.error(LOCALISER.msg("Enhancer.ClassNotFound", utfClassNameString, ex));
                    throw new JDOFatalException(LOCALISER.msg("Enhancer.ClassNotFound", utfClassNameString, ex));
                }
                if (fieldJavaClass == null)
                {
                    throw new JDOFatalException(LOCALISER.msg("Enhancer.ClassNotFound", utfClassNameString,
                        new NullPointerException()));
                }

                f = BCELUtils.getFieldByName(field.getName(constantPoolGen), fieldJavaClass);
                if (f == null)
                {
                    String message = LOCALISER.msg("Enhancer.FieldIsNull", className, m.getName(), field.getName(constantPoolGen));
                    JPOXLogger.ENHANCER.error(message);
                    throw new NullPointerException(message);
                }

                ClassGen cg = BCELUtils.getClassByFieldByName(field.getName(constantPoolGen), fieldJavaClass);
                BCELFieldPropertyMetaData fieldConfig = null;
                BCELClassMetaData jdoConfigClass = ((BCELClassMetaData) cmd);

                // the accessing class is not this
                if (!cg.getClassName().equals(newClass.getClassName()))
                {
                    jdoConfigClass = (BCELClassMetaData) cmd.getPackageMetaData().getFileMetaData().getMetaDataManager().getMetaDataForClass(
                        cg.getClassName(), clr);
                }
                if( jdoConfigClass != null )
                {
                    AbstractMemberMetaData apmd = jdoConfigClass.findField(f);

                    if (apmd == null)
                    {
                        //check if a property(getter,setter) exists with this field name
                        if( jdoConfigClass.findProperty(f)==null )
                        {
                            //no fields netiher properties in the class, so something is wrong
                            String message = LOCALISER.msg("Enhancer.FieldConfigIsNullError", className + "." + f.getName());
                            JPOXLogger.ENHANCER.fatal(message);
                            throw new RuntimeException(message);
                        }
                    }
                    if( apmd != null && apmd.getPersistenceModifier() != FieldPersistenceModifier.NONE )
                    {
                        // do nothing
                    }
                    if (!isFieldAccessInPersistenceCapableClass(ih, m.getConstantPool()))
                    {
                        // do nothing
                    }
                    else if (fieldConfig != null && fieldConfig.getJdoFieldFlag() == 0)
                    {
                        // do nothing
                    }
                    else if (f.isStatic() || f.isFinal())
                    {
                        // do nothing
                    }
                    else if (BCELUtils.isSynthetic(f))
                    {
                        // do nothing
                    }
                    else
                    {
                        if (isDebugEnabled)
                        {
                            JPOXLogger.ENHANCER.debug(LOCALISER.msg("Enhancer.EnhanceOriginalMethod",
                                className + "." + m.getName(), f.getName()));
                        }
                        if( apmd != null && !apmd.isProperty() && apmd.getPersistenceModifier() != FieldPersistenceModifier.NONE)
                        {
                            //properties do not use jdoXXX methods
                            if (i instanceof GETFIELD)
View Full Code Here

                // ----------------------------------------------------------------
                // fields
                Arrays.sort(fields, FIELD_OR_METHOD_COMPARATOR);
                for (int i = 0; i < fields.length; i++)
                {
                    Field field = fields[i];
                    int mods = fields[i].getAccessFlags();
                    if (((mods & Modifier.PRIVATE) == 0) || ((mods & (Modifier.STATIC | Modifier.TRANSIENT)) == 0))
                    {
                        dout.writeUTF(field.getName());
                        dout.writeInt(mods);
                        dout.writeUTF(field.getSignature());
                    }
                }
                // ----------------------------------------------------------------
                // methods
                Arrays.sort(methods, FIELD_OR_METHOD_COMPARATOR);
View Full Code Here

    private void initializeEnhanceField()
    {
        if (isFieldBelongsToThisClass())
        {
            // BCEL Field for use in enhancement process.
            Field field = null;

            // BCEL Method for use in enhancement process.
            Method method = null;
           
            // If the field is for this class, save the field for use in BCEL enhancing later
View Full Code Here

     * @param classGen class gen
     * @return field object. return null if field not found.
     */
    public static Field getFieldByName(final String fieldName, final ClassGen classGen)
    {
        Field f[] = classGen.getFields();
        if( classGen == null )
        {
            return null;
        }
        for (int i = 0; i < f.length; i++)
View Full Code Here

     * @param classGen class gen
     * @return field object. return null if field not found.
     */
    public static Field getFieldByName(final String fieldName, final JavaClass classGen)
    {
        Field f[] = classGen.getFields();
        if( classGen == null )
        {
            return null;
        }
        for (int i = 0; i < f.length; i++)
View Full Code Here

TOP

Related Classes of org.apache.bcel.classfile.Field

Copyright © 2018 www.massapicom. 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.