| 1 21 package proguard.classfile.editor; 22 23 import proguard.classfile.*; 24 import proguard.classfile.attribute.Attribute; 25 import proguard.classfile.util.SimplifiedVisitor; 26 import proguard.classfile.visitor.MemberVisitor; 27 28 34 public class MemberAdder 35 extends SimplifiedVisitor 36 implements MemberVisitor 37 { 38 private ProgramClass targetClass; 39 private boolean copyAttributes; 40 41 private ConstantAdder constantAdder = new ConstantAdder(); 42 private MembersEditor membersEditor = new MembersEditor(); 43 44 45 49 public MemberAdder(ProgramClass targetClass, boolean copyAttributes) 50 { 51 this.targetClass = targetClass; 52 this.copyAttributes = copyAttributes; 53 54 constantAdder.setTargetClass(targetClass); 55 } 56 57 58 60 public void visitProgramField(ProgramClass programClass, ProgramField programField) 61 { 62 String name = programField.getName(programClass); 63 String descriptor = programField.getDescriptor(programClass); 64 65 if (targetClass.findMethod(name, descriptor) == null) 66 { 67 ProgramField newProgramField = new ProgramField(); 68 69 newProgramField.u2accessFlags = programField.u2accessFlags; 71 72 programClass.constantPoolEntryAccept(programField.u2nameIndex, 74 constantAdder); 75 76 newProgramField.u2nameIndex = constantAdder.getConstantIndex(); 77 78 programClass.constantPoolEntryAccept(programField.u2descriptorIndex, 80 constantAdder); 81 82 newProgramField.u2descriptorIndex = constantAdder.getConstantIndex(); 83 84 if (copyAttributes) 86 { 87 programField.attributesAccept(programClass, 88 new AttributeAdder(targetClass, 89 newProgramField)); 90 } 91 92 membersEditor.addField(targetClass, newProgramField); 94 } 95 } 96 97 98 public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) 99 { 100 String name = programMethod.getName(programClass); 101 String descriptor = programMethod.getDescriptor(programClass); 102 103 if (targetClass.findMethod(name, descriptor) == null) 104 { 105 ProgramMethod newProgramMethod = new ProgramMethod(); 106 107 newProgramMethod.u2accessFlags = programMethod.u2accessFlags; 109 110 programClass.constantPoolEntryAccept(programMethod.u2nameIndex, 112 constantAdder); 113 114 newProgramMethod.u2nameIndex = constantAdder.getConstantIndex(); 115 116 programClass.constantPoolEntryAccept(programMethod.u2descriptorIndex, 118 constantAdder); 119 120 newProgramMethod.u2descriptorIndex = constantAdder.getConstantIndex(); 121 122 newProgramMethod.u2attributesCount = 0; 124 newProgramMethod.attributes = new Attribute[0]; 125 126 if (copyAttributes) 128 { 129 programMethod.attributesAccept(programClass, 130 new AttributeAdder(targetClass, 131 newProgramMethod)); 132 } 133 134 membersEditor.addMethod(targetClass, newProgramMethod); 136 } 137 } 138 } 139 | Popular Tags |