Esempio n. 1
0
  static synchronized void remove(LinkedList list, String name) {
    if (list == null) return;

    ListIterator iterator = list.listIterator();
    while (iterator.hasNext()) {
      AttributeInfo ai = (AttributeInfo) iterator.next();
      if (ai.getName().equals(name)) iterator.remove();
    }
  }
Esempio n. 2
0
  static void writeAll(LinkedList list, DataOutputStream out) throws IOException {
    if (list == null) return;

    int n = list.size();
    for (int i = 0; i < n; ++i) {
      AttributeInfo attr = (AttributeInfo) list.get(i);
      attr.write(out);
    }
  }
Esempio n. 3
0
  static int getLength(LinkedList list) {
    int size = 0;
    int n = list.size();
    for (int i = 0; i < n; ++i) {
      AttributeInfo attr = (AttributeInfo) list.get(i);
      size += attr.length();
    }

    return size;
  }
Esempio n. 4
0
  static AttributeInfo lookup(LinkedList list, String name) {
    if (list == null) return null;

    ListIterator iterator = list.listIterator();
    while (iterator.hasNext()) {
      AttributeInfo ai = (AttributeInfo) iterator.next();
      if (ai.getName().equals(name)) return ai;
    }

    return null; // no such attribute
  }
Esempio n. 5
0
  static LinkedList copyAll(LinkedList list, ConstPool cp) {
    if (list == null) return null;

    LinkedList newList = new LinkedList();
    int n = list.size();
    for (int i = 0; i < n; ++i) {
      AttributeInfo attr = (AttributeInfo) list.get(i);
      newList.add(attr.copy(cp, null));
    }

    return newList;
  }
Esempio n. 6
0
  /**
   * Constructs a copy of <code>Code_attribute</code>. Specified class names are replaced during the
   * copy.
   *
   * @param cp constant pool table.
   * @param src source Code attribute.
   * @param classnames pairs of replaced and substituted class names.
   */
  private CodeAttribute(ConstPool cp, CodeAttribute src, Map classnames) throws BadBytecode {
    super(cp, tag);

    maxStack = src.getMaxStack();
    maxLocals = src.getMaxLocals();
    exceptions = src.getExceptionTable().copy(cp, classnames);
    attributes = new ArrayList();
    List src_attr = src.getAttributes();
    int num = src_attr.size();
    for (int i = 0; i < num; ++i) {
      AttributeInfo ai = (AttributeInfo) src_attr.get(i);
      attributes.add(ai.copy(cp, classnames));
    }

    info = src.copyCode(cp, classnames, exceptions, this);
  }
Esempio n. 7
0
  void prune(ConstPool cp) {
    LinkedList newAttributes = new LinkedList();

    AttributeInfo invisibleAnnotations = getAttribute(AnnotationsAttribute.invisibleTag);
    if (invisibleAnnotations != null) {
      invisibleAnnotations = invisibleAnnotations.copy(cp, null);
      newAttributes.add(invisibleAnnotations);
    }

    AttributeInfo visibleAnnotations = getAttribute(AnnotationsAttribute.visibleTag);
    if (visibleAnnotations != null) {
      visibleAnnotations = visibleAnnotations.copy(cp, null);
      newAttributes.add(visibleAnnotations);
    }

    AttributeInfo parameterInvisibleAnnotations =
        getAttribute(ParameterAnnotationsAttribute.invisibleTag);
    if (parameterInvisibleAnnotations != null) {
      parameterInvisibleAnnotations = parameterInvisibleAnnotations.copy(cp, null);
      newAttributes.add(parameterInvisibleAnnotations);
    }

    AttributeInfo parameterVisibleAnnotations =
        getAttribute(ParameterAnnotationsAttribute.visibleTag);
    if (parameterVisibleAnnotations != null) {
      parameterVisibleAnnotations = parameterVisibleAnnotations.copy(cp, null);
      newAttributes.add(parameterVisibleAnnotations);
    }

    AnnotationDefaultAttribute defaultAttribute =
        (AnnotationDefaultAttribute) getAttribute(AnnotationDefaultAttribute.tag);
    if (defaultAttribute != null) newAttributes.add(defaultAttribute);

    ExceptionsAttribute ea = getExceptionsAttribute();
    if (ea != null) newAttributes.add(ea);

    AttributeInfo signature = getAttribute(SignatureAttribute.tag);
    if (signature != null) {
      signature = signature.copy(cp, null);
      newAttributes.add(signature);
    }

    attribute = newAttributes;
    name = cp.addUtf8Info(getName());
    descriptor = cp.addUtf8Info(getDescriptor());
    constPool = cp;
  }
Esempio n. 8
0
 private void read(DataInputStream in) throws IOException {
   accessFlags = in.readUnsignedShort();
   name = in.readUnsignedShort();
   descriptor = in.readUnsignedShort();
   int n = in.readUnsignedShort();
   attribute = new LinkedList();
   for (int i = 0; i < n; ++i) attribute.add(AttributeInfo.read(constPool, in));
 }
Esempio n. 9
0
 void write(DataOutputStream out) throws IOException {
   out.writeShort(name); // attribute_name_index
   out.writeInt(length() - 6); // attribute_length
   out.writeShort(maxStack); // max_stack
   out.writeShort(maxLocals); // max_locals
   out.writeInt(info.length); // code_length
   out.write(info); // code
   exceptions.write(out);
   out.writeShort(attributes.size()); // attributes_count
   AttributeInfo.writeAll(attributes, out); // attributes
 }
Esempio n. 10
0
  void write(DataOutputStream out) throws IOException {
    out.writeShort(accessFlags);
    out.writeShort(name);
    out.writeShort(descriptor);

    if (attribute == null) out.writeShort(0);
    else {
      out.writeShort(attribute.size());
      AttributeInfo.writeAll(attribute, out);
    }
  }
Esempio n. 11
0
  public void testRemoveAnnotatino() throws Exception {
    CtClass cc = sloader.get("test5.RemoveAnnotation");
    AnnotationsAttribute aa =
        (AnnotationsAttribute) cc.getClassFile().getAttribute(AnnotationsAttribute.invisibleTag);
    assertTrue(aa.removeAnnotation("test5.RemoveAnno1"));
    AttributeInfo ai = cc.getClassFile().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertEquals(ai.getName(), AnnotationsAttribute.invisibleTag);

    CtMethod foo = cc.getDeclaredMethod("foo");
    AnnotationsAttribute aa2 =
        (AnnotationsAttribute) foo.getMethodInfo().getAttribute(AnnotationsAttribute.invisibleTag);
    assertTrue(aa2.removeAnnotation("test5.RemoveAnno1"));

    CtMethod bar = cc.getDeclaredMethod("bar");
    AnnotationsAttribute aa3 =
        (AnnotationsAttribute) bar.getMethodInfo().getAttribute(AnnotationsAttribute.invisibleTag);
    assertFalse(aa3.removeAnnotation("test5.RemoveAnno1"));
    assertTrue(aa3.removeAnnotation("test5.RemoveAnno2"));
    AttributeInfo ai2 = bar.getMethodInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertEquals(ai2.getName(), AnnotationsAttribute.invisibleTag);

    CtMethod run = cc.getDeclaredMethod("run");
    AttributeInfo ai3 = run.getMethodInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertNull(ai3);

    CtField baz = cc.getDeclaredField("baz");
    AttributeInfo ai4 = baz.getFieldInfo().removeAttribute(AnnotationsAttribute.invisibleTag);
    assertEquals(ai4.getName(), AnnotationsAttribute.invisibleTag);

    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(3, invoke(obj, "run"));
  }
Esempio n. 12
0
  CodeAttribute(ConstPool cp, int name_id, DataInputStream in) throws IOException {
    super(cp, name_id, (byte[]) null);
    int attr_len = in.readInt();

    maxStack = in.readUnsignedShort();
    maxLocals = in.readUnsignedShort();

    int code_len = in.readInt();
    info = new byte[code_len];
    in.readFully(info);

    exceptions = new ExceptionTable(cp, in);

    attributes = new ArrayList();
    int num = in.readUnsignedShort();
    for (int i = 0; i < num; ++i) attributes.add(AttributeInfo.read(cp, in));
  }
Esempio n. 13
0
 void renameClass(String oldname, String newname) {
   AttributeInfo.renameClass(attributes, oldname, newname);
 }
Esempio n. 14
0
 /**
  * Copies all constant pool items to a given new constant pool and replaces the original items
  * with the new ones. This is used for garbage collecting the items of removed fields and methods.
  *
  * @param cp the destination
  */
 void compact(ConstPool cp) {
   name = cp.addUtf8Info(getName());
   descriptor = cp.addUtf8Info(getDescriptor());
   attribute = AttributeInfo.copyAll(attribute, cp);
   constPool = cp;
 }
Esempio n. 15
0
  /**
   * Appends an attribute. If there is already an attribute with the same name, the new one
   * substitutes for it.
   *
   * @see #getAttributes()
   */
  public void addAttribute(AttributeInfo info) {
    if (attribute == null) attribute = new LinkedList();

    AttributeInfo.remove(attribute, info.getName());
    attribute.add(info);
  }
Esempio n. 16
0
 /**
  * Returns an Exceptions attribute.
  *
  * @return an Exceptions attribute or null if it is not specified.
  */
 public ExceptionsAttribute getExceptionsAttribute() {
   AttributeInfo info = AttributeInfo.lookup(attribute, ExceptionsAttribute.tag);
   return (ExceptionsAttribute) info;
 }
Esempio n. 17
0
 /**
  * Returns a Code attribute.
  *
  * @return a Code attribute or null if it is not specified.
  */
 public CodeAttribute getCodeAttribute() {
   AttributeInfo info = AttributeInfo.lookup(attribute, CodeAttribute.tag);
   return (CodeAttribute) info;
 }
Esempio n. 18
0
 /** Removes an Exception attribute. */
 public void removeExceptionsAttribute() {
   AttributeInfo.remove(attribute, ExceptionsAttribute.tag);
 }
Esempio n. 19
0
 /**
  * Adds a stack map table. If another copy of stack map table is already contained, the old one is
  * removed.
  *
  * @param smt the stack map table added to this code attribute. If it is null, a new stack map is
  *     not added. Only the old stack map is removed.
  */
 public void setAttribute(StackMapTable smt) {
   AttributeInfo.remove(attributes, StackMapTable.tag);
   if (smt != null) attributes.add(smt);
 }
Esempio n. 20
0
 /**
  * Adds a stack map table for J2ME (CLDC). If another copy of stack map table is already
  * contained, the old one is removed.
  *
  * @param sm the stack map table added to this code attribute. If it is null, a new stack map is
  *     not added. Only the old stack map is removed.
  * @since 3.12
  */
 public void setAttribute(StackMap sm) {
   AttributeInfo.remove(attributes, StackMap.tag);
   if (sm != null) attributes.add(sm);
 }
Esempio n. 21
0
 /** Sets <code>code[]</code>. */
 void setCode(byte[] newinfo) {
   super.set(newinfo);
 }
Esempio n. 22
0
 /**
  * Returns the attribute with the specified name. If it is not found, this method returns null.
  *
  * @param name attribute name
  * @return an <code>AttributeInfo</code> object or null.
  */
 public AttributeInfo getAttribute(String name) {
   return AttributeInfo.lookup(attributes, name);
 }
Esempio n. 23
0
 void getRefClasses(Map classnames) {
   AttributeInfo.getRefClasses(attributes, classnames);
 }
Esempio n. 24
0
 void renameClass(Map classnames) {
   AttributeInfo.renameClass(attributes, classnames);
 }
 void write(DataOutputStream out) throws IOException {
   super.write(out);
 }
Esempio n. 26
0
 /** Removes a Code attribute. */
 public void removeCodeAttribute() {
   AttributeInfo.remove(attribute, CodeAttribute.tag);
 }
Esempio n. 27
0
 /**
  * Returns the length of this <code>attribute_info</code> structure. The returned value is <code>
  * attribute_length + 6</code>.
  */
 public int length() {
   return 18 + info.length + exceptions.size() * 8 + AttributeInfo.getLength(attributes);
 }