Package org.apache.bcel.classfile

Examples of org.apache.bcel.classfile.Field


                            && (superClassName.endsWith("InputStream") && fieldName.equals("in") || superClassName
                                    .endsWith("OutputStream") && fieldName.equals("out"))) {
                        continue;
                    }
                    if (classFields.containsKey(fieldName)) {
                        Field maskingField = classFields.get(fieldName);
                        String mClassName = getDottedClassName();
                        FieldAnnotation fa = new FieldAnnotation(mClassName, maskingField.getName(), maskingField.getSignature(),
                                maskingField.isStatic());
                        int priority = NORMAL_PRIORITY;
                        if (maskingField.isStatic() || maskingField.isFinal()) {
                            priority++;
                        } else if (fld.getSignature().charAt(0) == 'L' && !fld.getSignature().startsWith("Ljava/lang/")
                                || fld.getSignature().charAt(0) == '[') {
                            priority--;
                        }
                        if (!fld.getSignature().equals(maskingField.getSignature())) {
                            priority += 2;
                        } else if (fld.getAccessFlags() != maskingField.getAccessFlags()) {
                            priority++;
                        }
                        if (fld.isSynthetic() || fld.getName().indexOf('$') >= 0) {
                            priority++;
                        }
View Full Code Here


                }
                String varName = var.getName();
                if (varName.equals("serialVersionUID")) {
                    continue;
                }
                Field f = classFields.get(varName);
                // System.out.println("Checking " + varName);
                // System.out.println(" got " + f);
                // TODO: we could distinguish between obscuring a field in the
                // same class
                // vs. obscuring a field in a superclass. Not sure how important
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){
        JavaClass[] superclasses = jc.getSuperClasses();
        outer:
        for (int j=0; j<superclasses.length; j++){
          fields = superclasses[j].getFields();
          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);
              if (f_type.equals(o_type)){
                f = fields[i];
                if ((f.getAccessFlags() & (Constants.ACC_PUBLIC | Constants.ACC_PROTECTED)) == 0) {
                                    f = null;
                                }
                break outer;
              }
            }
          }
        }
        if (f == null) {
                    constraintViolated(o, "Referenced field '"+field_name+"' does not exist in class '"+jc.getClassName()+"'.");
                }
      }
      else{
        /* TODO: Check if assignment compatibility is sufficient.
           What does Sun do? */
        Type f_type = Type.getType(f.getSignature());
        Type o_type = o.getType(cpg);
               
        // Argh. Sun's implementation allows us to have multiple fields of
        // the same name but with a different signature.
        //if (! f_type.equals(o_type)){
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)){
          f = fields[i];
          break;
        }
      }
      if (f == null){
        throw new AssertionViolatedException("Field not found?!?");
      }

      if (f.isFinal()){
        if (!(myOwner.getClassName().equals(o.getClassType(cpg).getClassName()))){
          constraintViolated(o, "Referenced field '"+f+"' is final and must therefore be declared in the current class '"+myOwner.getClassName()+"' which is not the case: it is declared in '"+o.getClassType(cpg).getClassName()+"'.");
        }
      }

      if (! (f.isStatic())){
        constraintViolated(o, "Referenced field '"+f+"' is not static which it should be.");
      }

      String meth_name = Repository.lookupClass(myOwner.getClassName()).getMethods()[method_no].getName();
View Full Code Here

    public void visitGETSTATIC(GETSTATIC 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)){
          f = fields[i];
          break;
        }
      }
      if (f == null){
        throw new AssertionViolatedException("Field not found?!?");
      }

      if (! (f.isStatic())){
        constraintViolated(o, "Referenced field '"+f+"' is not static which it should be.");
      }
        } catch (ClassNotFoundException e) {
      // FIXME: maybe not the best way to handle this
      throw new AssertionViolatedException("Missing class: " + e.toString());
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){
      JavaClass[] superclasses = jc.getSuperClasses();
      outer:
      for (int j=0; j<superclasses.length; j++){
        fields = superclasses[j].getFields();
        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);
            if (f_type.equals(o_type)){
              f = fields[i];
              if ((f.getAccessFlags() & (Constants.ACC_PUBLIC | Constants.ACC_PROTECTED)) == 0) {
                                f = null;
                            }
              break outer;
            }
          }
        }
      }
      if (f == null) {
                throw new AssertionViolatedException("Field '"+field_name+"' 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

   * Fifth pass, produce Java byte code.
   */
  public void byte_code(ClassGen class_gen, ConstantPoolGen cp) {
    /* private static BufferedReader _in;
     */
    class_gen.addField(new Field(ACC_PRIVATE | ACC_STATIC,
         cp.addUtf8("_in"),
         cp.addUtf8("Ljava/io/BufferedReader;"),
         null, cp.getConstantPool()));

    MethodGen       method;
View Full Code Here

        throw new ClassConstraintException("The ConstantValue attribute '"+tostring(obj)+"' is not correctly named 'ConstantValue' but '"+name+"'.");
      }

      Object pred = carrier.predecessor();
      if (pred instanceof Field){ //ConstantValue attributes are quite senseless if the predecessor is not a field.
        Field f = (Field) pred;
        // Field constraints have been checked before -- so we are safe using their type information.
        Type field_type = Type.getType(((ConstantUtf8) (cp.getConstant(f.getSignatureIndex()))).getBytes());

        int index = obj.getConstantValueIndex();
        if ((index < 0) || (index >= cplen)){
          throw new ClassConstraintException("Invalid index '"+index+"' used by '"+tostring(obj)+"'.");
        }
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.