001    package serp.bytecode;
002    
003    import java.io.DataInput;
004    import java.io.DataOutput;
005    import java.io.IOException;
006    import java.util.Arrays;
007    
008    import serp.bytecode.lowlevel.Entry;
009    import serp.bytecode.lowlevel.MethodHandleEntry;
010    
011    // {   u2 bootstrap_method_ref;
012    //     u2 num_bootstrap_arguments;
013    //     u2 bootstrap_arguments[num_bootstrap_arguments];
014    //  bootstrap_methods[num_bootstrap_methods];
015    public class BootstrapMethodElement {
016        private BootstrapMethods _bootstrapMethodAttribute;
017        
018        private int _bootstrap_method_ref = 0;
019        private int[] _bootstrap_arguments = new int[0];
020        
021        public BootstrapMethodElement() {
022            
023        }
024    
025        public BootstrapMethodElement(BootstrapMethods bootstrapmethodAttr, int bootstrap_method_ref, 
026                int num_bootstrap_arguments, int[] bootstrap_arguments) {
027            super();
028            _bootstrapMethodAttribute = bootstrapmethodAttr;
029            _bootstrap_method_ref = bootstrap_method_ref;
030           
031            _bootstrap_arguments = new int[num_bootstrap_arguments];
032            
033            for (int i = 0; i < num_bootstrap_arguments; i++) {
034                _bootstrap_arguments[i] = bootstrap_arguments[i];
035            }
036        }
037        
038        public BootstrapMethodElement(BootstrapMethods bootstrapmethodAttr, DataInput in) throws IOException {
039            _bootstrapMethodAttribute = bootstrapmethodAttr;
040            
041            _bootstrap_method_ref = in.readShort();
042            
043            int num_bootstrap_arguments = in.readShort();
044            _bootstrap_arguments = new int[num_bootstrap_arguments];
045            
046            for (int i = 0; i < num_bootstrap_arguments; i++) {
047                _bootstrap_arguments[i] = in.readShort();
048            }
049        }
050    
051        public BootstrapMethods getBootstrapMethodAttribute() {
052            return _bootstrapMethodAttribute;
053        }
054    
055        public void setBootstrapMethodAttribute(BootstrapMethods bootstrapMethodAttribute) {
056            _bootstrapMethodAttribute = bootstrapMethodAttribute;
057        }
058    
059        
060        public int getBootstrapMethodRef() {
061            return _bootstrap_method_ref;
062        }
063        
064        public MethodHandleEntry getBootstrapMethod() {
065            if (_bootstrap_method_ref == 0) {
066                return null;
067            }
068            
069            Entry e = _bootstrapMethodAttribute.getPool().getEntry(_bootstrap_method_ref);
070            return (MethodHandleEntry) e;
071        }
072    
073        public void setBootstrapMethodRef(int bootstrap_method_ref) {
074            _bootstrap_method_ref = bootstrap_method_ref;
075        }
076        
077        public void setBootstrapMethod(MethodHandleEntry mhe) {
078            if (mhe == null) {
079                _bootstrap_method_ref = 0;
080                return;
081            }
082            
083            _bootstrap_method_ref = mhe.getIndex();
084        }
085    
086        
087        public int getNumBootstrapArguments() {
088            return _bootstrap_arguments.length;
089        }
090        
091        public int[] getBootstrapArgumentIndices() {
092            return Arrays.copyOf(_bootstrap_arguments, _bootstrap_arguments.length);
093        }
094        
095        public Entry[] getBootstrapArguments() {
096            Entry[] ceArr = new Entry[getNumBootstrapArguments()];
097            for (int i = 0; i < ceArr.length; i++) {
098                ceArr[i] = _bootstrapMethodAttribute.getPool().getEntry(_bootstrap_arguments[i]);
099            }
100            return ceArr;
101        }
102    
103        public void setBootstrapArgumentIndices(int[] bootstrap_arguments) {
104            if (bootstrap_arguments == null || bootstrap_arguments.length == 0) {
105                _bootstrap_arguments = new int[0];
106                return;
107            }
108            
109            _bootstrap_arguments = Arrays.copyOf(bootstrap_arguments, bootstrap_arguments.length); 
110        }
111        
112        public void setBootstrapArguments(Entry[] bsArgs) {
113            if (bsArgs == null || bsArgs.length == 0) {
114                _bootstrap_arguments = new int[0];
115                return;
116            }
117            
118            _bootstrap_arguments = new int[bsArgs.length];
119            
120            for (int i = 0; i < bsArgs.length; i++) {
121                _bootstrap_arguments[i] = bsArgs[i].getIndex();
122            }
123        }
124        
125        public int getLength() {
126            return 4 + (2 * _bootstrap_arguments.length);
127        }
128        
129        public void write(DataOutput out) throws IOException {
130            out.writeShort(_bootstrap_method_ref);
131            out.writeShort(_bootstrap_arguments.length);
132            
133            for (int i = 0; i < _bootstrap_arguments.length; i++) {
134                out.writeShort(_bootstrap_arguments[i]);
135            }
136        }
137    }