001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with this
004 * work for additional information regarding copyright ownership. The ASF
005 * licenses this file to You under the Apache License, Version 2.0 (the
006 * "License"); you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014 * License for the specific language governing permissions and limitations under
015 * the License.
016 */
017package org.apache.commons.compress.harmony.pack200;
018
019import java.io.IOException;
020import java.io.OutputStream;
021import java.util.ArrayList;
022import java.util.Iterator;
023import java.util.List;
024
025/**
026 * A group of metadata (annotation) bands, such as class_RVA_bands, method_AD_bands etc.
027 */
028public class MetadataBandGroup extends BandSet {
029
030    public static final int CONTEXT_CLASS = 0;
031    public static final int CONTEXT_FIELD = 1;
032    public static final int CONTEXT_METHOD = 2;
033
034    private final String type;
035    private int numBackwardsCalls = 0;
036
037    public IntList param_NB = new IntList(); // TODO: Lazy instantiation?
038    public IntList anno_N = new IntList();
039    public List<CPSignature> type_RS = new ArrayList<>();
040    public IntList pair_N = new IntList();
041    public List<CPUTF8> name_RU = new ArrayList<>();
042    public List<String> T = new ArrayList<>();
043    public List<CPConstant<?>> caseI_KI = new ArrayList<>();
044    public List<CPConstant<?>> caseD_KD = new ArrayList<>();
045    public List<CPConstant<?>> caseF_KF = new ArrayList<>();
046    public List<CPConstant<?>> caseJ_KJ = new ArrayList<>();
047    public List<CPSignature> casec_RS = new ArrayList<>();
048    public List<CPSignature> caseet_RS = new ArrayList<>();
049    public List<CPUTF8> caseec_RU = new ArrayList<>();
050    public List<CPUTF8> cases_RU = new ArrayList<>();
051    public IntList casearray_N = new IntList();
052    public List<CPSignature> nesttype_RS = new ArrayList<>();
053    public IntList nestpair_N = new IntList();
054    public List<CPUTF8> nestname_RU = new ArrayList<>();
055
056    private final CpBands cpBands;
057    private final int context;
058
059    /**
060     * Constructs a new MetadataBandGroup
061     *
062     * @param type must be either AD, RVA, RIA, RVPA or RIPA.
063     * @param context {@code CONTEXT_CLASS}, {@code CONTEXT_METHOD} or {@code CONTEXT_FIELD}
064     * @param cpBands constant pool bands
065     * @param segmentHeader segment header
066     * @param effort packing effort
067     */
068    public MetadataBandGroup(final String type, final int context, final CpBands cpBands,
069        final SegmentHeader segmentHeader, final int effort) {
070        super(effort, segmentHeader);
071        this.type = type;
072        this.cpBands = cpBands;
073        this.context = context;
074    }
075
076    /*
077     * (non-Javadoc)
078     *
079     * @see org.apache.commons.compress.harmony.pack200.BandSet#pack(java.io.OutputStream)
080     */
081    @Override
082    public void pack(final OutputStream out) throws IOException, Pack200Exception {
083        PackingUtils.log("Writing metadata band group...");
084        if (hasContent()) {
085            String contextStr;
086            if (context == CONTEXT_CLASS) {
087                contextStr = "Class";
088            } else if (context == CONTEXT_FIELD) {
089                contextStr = "Field";
090            } else {
091                contextStr = "Method";
092            }
093            byte[] encodedBand = null;
094            if (!type.equals("AD")) {
095                if (type.indexOf('P') != -1) {
096                    // Parameter annotation so we need to transmit param_NB
097                    encodedBand = encodeBandInt(contextStr + "_" + type + " param_NB", param_NB.toArray(), Codec.BYTE1);
098                    out.write(encodedBand);
099                    PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type
100                        + " anno_N[" + param_NB.size() + "]");
101                }
102                encodedBand = encodeBandInt(contextStr + "_" + type + " anno_N", anno_N.toArray(), Codec.UNSIGNED5);
103                out.write(encodedBand);
104                PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " anno_N["
105                    + anno_N.size() + "]");
106
107                encodedBand = encodeBandInt(contextStr + "_" + type + " type_RS", cpEntryListToArray(type_RS),
108                    Codec.UNSIGNED5);
109                out.write(encodedBand);
110                PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " type_RS["
111                    + type_RS.size() + "]");
112
113                encodedBand = encodeBandInt(contextStr + "_" + type + " pair_N", pair_N.toArray(), Codec.UNSIGNED5);
114                out.write(encodedBand);
115                PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " pair_N["
116                    + pair_N.size() + "]");
117
118                encodedBand = encodeBandInt(contextStr + "_" + type + " name_RU", cpEntryListToArray(name_RU),
119                    Codec.UNSIGNED5);
120                out.write(encodedBand);
121                PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " name_RU["
122                    + name_RU.size() + "]");
123            }
124            encodedBand = encodeBandInt(contextStr + "_" + type + " T", tagListToArray(T), Codec.BYTE1);
125            out.write(encodedBand);
126            PackingUtils
127                .log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " T[" + T.size() + "]");
128
129            encodedBand = encodeBandInt(contextStr + "_" + type + " caseI_KI", cpEntryListToArray(caseI_KI),
130                Codec.UNSIGNED5);
131            out.write(encodedBand);
132            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " caseI_KI["
133                + caseI_KI.size() + "]");
134
135            encodedBand = encodeBandInt(contextStr + "_" + type + " caseD_KD", cpEntryListToArray(caseD_KD),
136                Codec.UNSIGNED5);
137            out.write(encodedBand);
138            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " caseD_KD["
139                + caseD_KD.size() + "]");
140
141            encodedBand = encodeBandInt(contextStr + "_" + type + " caseF_KF", cpEntryListToArray(caseF_KF),
142                Codec.UNSIGNED5);
143            out.write(encodedBand);
144            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " caseF_KF["
145                + caseF_KF.size() + "]");
146
147            encodedBand = encodeBandInt(contextStr + "_" + type + " caseJ_KJ", cpEntryListToArray(caseJ_KJ),
148                Codec.UNSIGNED5);
149            out.write(encodedBand);
150            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " caseJ_KJ["
151                + caseJ_KJ.size() + "]");
152
153            encodedBand = encodeBandInt(contextStr + "_" + type + " casec_RS", cpEntryListToArray(casec_RS),
154                Codec.UNSIGNED5);
155            out.write(encodedBand);
156            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " casec_RS["
157                + casec_RS.size() + "]");
158
159            encodedBand = encodeBandInt(contextStr + "_" + type + " caseet_RS", cpEntryListToArray(caseet_RS),
160                Codec.UNSIGNED5);
161            out.write(encodedBand);
162            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " caseet_RS["
163                + caseet_RS.size() + "]");
164
165            encodedBand = encodeBandInt(contextStr + "_" + type + " caseec_RU", cpEntryListToArray(caseec_RU),
166                Codec.UNSIGNED5);
167            out.write(encodedBand);
168            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " caseec_RU["
169                + caseec_RU.size() + "]");
170
171            encodedBand = encodeBandInt(contextStr + "_" + type + " cases_RU", cpEntryListToArray(cases_RU),
172                Codec.UNSIGNED5);
173            out.write(encodedBand);
174            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " cases_RU["
175                + cases_RU.size() + "]");
176
177            encodedBand = encodeBandInt(contextStr + "_" + type + " casearray_N", casearray_N.toArray(),
178                Codec.UNSIGNED5);
179            out.write(encodedBand);
180            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " casearray_N["
181                + casearray_N.size() + "]");
182
183            encodedBand = encodeBandInt(contextStr + "_" + type + " nesttype_RS", cpEntryListToArray(nesttype_RS),
184                Codec.UNSIGNED5);
185            out.write(encodedBand);
186            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " nesttype_RS["
187                + nesttype_RS.size() + "]");
188
189            encodedBand = encodeBandInt(contextStr + "_" + type + " nestpair_N", nestpair_N.toArray(), Codec.UNSIGNED5);
190            out.write(encodedBand);
191            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " nestpair_N["
192                + nestpair_N.size() + "]");
193
194            encodedBand = encodeBandInt(contextStr + "_" + type + " nestname_RU", cpEntryListToArray(nestname_RU),
195                Codec.UNSIGNED5);
196            out.write(encodedBand);
197            PackingUtils.log("Wrote " + encodedBand.length + " bytes from " + contextStr + "_" + type + " nestname_RU["
198                + nestname_RU.size() + "]");
199        }
200    }
201
202    private int[] tagListToArray(final List<String> list) {
203        return list.stream().mapToInt(s -> s.charAt(0)).toArray();
204    }
205
206    /**
207     * Add an annotation to this set of bands.
208     *
209     * @param numParams TODO
210     * @param annoN TODO
211     * @param pairN TODO
212     * @param typeRS TODO
213     * @param nameRU TODO
214     * @param t TODO
215     * @param values TODO
216     * @param caseArrayN TODO
217     * @param nestTypeRS TODO
218     * @param nestNameRU TODO
219     * @param nestPairN TODO
220     */
221        public void addParameterAnnotation(final int numParams, final int[] annoN, final IntList pairN,
222                        final List<String> typeRS, final List<String> nameRU, final List<String> t, final List<Object> values,
223                        final List<Integer> caseArrayN, final List<String> nestTypeRS, final List<String> nestNameRU,
224                        final List<Integer> nestPairN) {
225                param_NB.add(numParams);
226                for (int element : annoN) {
227                        anno_N.add(element);
228                }
229                pair_N.addAll(pairN);
230                for (String desc : typeRS) {
231                        type_RS.add(cpBands.getCPSignature(desc));
232                }
233                for (String name : nameRU) {
234                        name_RU.add(cpBands.getCPUtf8(name));
235                }
236                final Iterator<Object> valuesIterator = values.iterator();
237                for (String tag : t) {
238                        T.add(tag);
239                        if (tag.equals("B") || tag.equals("C") || tag.equals("I") || tag.equals("S") || tag.equals("Z")) {
240                                final Integer value = (Integer) valuesIterator.next();
241                                caseI_KI.add(cpBands.getConstant(value));
242                        } else if (tag.equals("D")) {
243                                final Double value = (Double) valuesIterator.next();
244                                caseD_KD.add(cpBands.getConstant(value));
245                        } else if (tag.equals("F")) {
246                                final Float value = (Float) valuesIterator.next();
247                                caseF_KF.add(cpBands.getConstant(value));
248                        } else if (tag.equals("J")) {
249                                final Long value = (Long) valuesIterator.next();
250                                caseJ_KJ.add(cpBands.getConstant(value));
251                        } else if (tag.equals("c")) {
252                                final String value = (String) valuesIterator.next();
253                                casec_RS.add(cpBands.getCPSignature(value));
254                        } else if (tag.equals("e")) {
255                                final String value = (String) valuesIterator.next();
256                                final String value2 = (String) valuesIterator.next();
257                                caseet_RS.add(cpBands.getCPSignature(value));
258                                caseec_RU.add(cpBands.getCPUtf8(value2));
259                        } else if (tag.equals("s")) {
260                                final String value = (String) valuesIterator.next();
261                                cases_RU.add(cpBands.getCPUtf8(value));
262                        }
263                        // do nothing here for [ or @ (handled below)
264                }
265                for (Integer element : caseArrayN) {
266                        final int arraySize = element.intValue();
267                        casearray_N.add(arraySize);
268                        numBackwardsCalls += arraySize;
269                }
270                for (String type : nestTypeRS) {
271                        nesttype_RS.add(cpBands.getCPSignature(type));
272                }
273                for (String name : nestNameRU) {
274                        nestname_RU.add(cpBands.getCPUtf8(name));
275                }
276                for (Integer numPairs : nestPairN) {
277                        nestpair_N.add(numPairs.intValue());
278                        numBackwardsCalls += numPairs.intValue();
279                }
280        }
281
282    /**
283     * Add an annotation to this set of bands
284     *
285     * @param desc TODO
286     * @param nameRU TODO
287     * @param tags TODO
288     * @param values TODO
289     * @param caseArrayN TODO
290     * @param nestTypeRS TODO
291     * @param nestNameRU TODO
292     * @param nestPairN TODO
293     */
294        public void addAnnotation(final String desc, final List<String> nameRU, final List<String> tags,
295                        final List<Object> values, final List<Integer> caseArrayN, final List<String> nestTypeRS,
296                        final List<String> nestNameRU, final List<Integer> nestPairN) {
297                type_RS.add(cpBands.getCPSignature(desc));
298                pair_N.add(nameRU.size());
299
300                for (String name : nameRU) {
301                        name_RU.add(cpBands.getCPUtf8(name));
302                }
303
304                final Iterator<Object> valuesIterator = values.iterator();
305                for (String tag : tags) {
306                        T.add(tag);
307                        if (tag.equals("B") || tag.equals("C") || tag.equals("I") || tag.equals("S") || tag.equals("Z")) {
308                                final Integer value = (Integer) valuesIterator.next();
309                                caseI_KI.add(cpBands.getConstant(value));
310                        } else if (tag.equals("D")) {
311                                final Double value = (Double) valuesIterator.next();
312                                caseD_KD.add(cpBands.getConstant(value));
313                        } else if (tag.equals("F")) {
314                                final Float value = (Float) valuesIterator.next();
315                                caseF_KF.add(cpBands.getConstant(value));
316                        } else if (tag.equals("J")) {
317                                final Long value = (Long) valuesIterator.next();
318                                caseJ_KJ.add(cpBands.getConstant(value));
319                        } else if (tag.equals("c")) {
320                                final String value = (String) valuesIterator.next();
321                                casec_RS.add(cpBands.getCPSignature(value));
322                        } else if (tag.equals("e")) {
323                                final String value = (String) valuesIterator.next();
324                                final String value2 = (String) valuesIterator.next();
325                                caseet_RS.add(cpBands.getCPSignature(value));
326                                caseec_RU.add(cpBands.getCPUtf8(value2));
327                        } else if (tag.equals("s")) {
328                                final String value = (String) valuesIterator.next();
329                                cases_RU.add(cpBands.getCPUtf8(value));
330                        }
331                        // do nothing here for [ or @ (handled below)
332                }
333                for (Integer element : caseArrayN) {
334                        final int arraySize = element.intValue();
335                        casearray_N.add(arraySize);
336                        numBackwardsCalls += arraySize;
337                }
338                for (String element : nestTypeRS) {
339                        final String type = element;
340                        nesttype_RS.add(cpBands.getCPSignature(type));
341                }
342                for (String element : nestNameRU) {
343                        final String name = element;
344                        nestname_RU.add(cpBands.getCPUtf8(name));
345                }
346                for (Integer element : nestPairN) {
347                        final Integer numPairs = element;
348                        nestpair_N.add(numPairs.intValue());
349                        numBackwardsCalls += numPairs.intValue();
350                }
351        }
352
353    /**
354     * Returns true if any annotations have been added to this set of bands.
355     *
356     * @return true if any annotations have been added to this set of bands.
357     */
358    public boolean hasContent() {
359        return type_RS.size() > 0;
360    }
361
362    public int numBackwardsCalls() {
363        return numBackwardsCalls;
364    }
365
366    public void incrementAnnoN() {
367        anno_N.increment(anno_N.size() - 1);
368    }
369
370    public void newEntryInAnnoN() {
371        anno_N.add(1);
372    }
373
374    /**
375     * Remove the latest annotation that was added to this group
376     */
377    public void removeLatest() {
378        final int latest = anno_N.remove(anno_N.size() - 1);
379        for (int i = 0; i < latest; i++) {
380            type_RS.remove(type_RS.size() - 1);
381            final int pairs = pair_N.remove(pair_N.size() - 1);
382            for (int j = 0; j < pairs; j++) {
383                removeOnePair();
384            }
385        }
386    }
387
388    /*
389     * Convenience method for removeLatest
390     */
391    private void removeOnePair() {
392        final String tag = T.remove(T.size() - 1);
393        if (tag.equals("B") || tag.equals("C") || tag.equals("I") || tag.equals("S") || tag.equals("Z")) {
394            caseI_KI.remove(caseI_KI.size() - 1);
395        } else if (tag.equals("D")) {
396            caseD_KD.remove(caseD_KD.size() - 1);
397        } else if (tag.equals("F")) {
398            caseF_KF.remove(caseF_KF.size() - 1);
399        } else if (tag.equals("J")) {
400            caseJ_KJ.remove(caseJ_KJ.size() - 1);
401        } else if (tag.equals("C")) {
402            casec_RS.remove(casec_RS.size() - 1);
403        } else if (tag.equals("e")) {
404            caseet_RS.remove(caseet_RS.size() - 1);
405            caseec_RU.remove(caseet_RS.size() - 1);
406        } else if (tag.equals("s")) {
407            cases_RU.remove(cases_RU.size() - 1);
408        } else if (tag.equals("[")) {
409            final int arraySize = casearray_N.remove(casearray_N.size() - 1);
410            numBackwardsCalls -= arraySize;
411            for (int k = 0; k < arraySize; k++) {
412                removeOnePair();
413            }
414        } else if (tag.equals("@")) {
415            nesttype_RS.remove(nesttype_RS.size() - 1);
416            final int numPairs = nestpair_N.remove(nestpair_N.size() - 1);
417            numBackwardsCalls -= numPairs;
418            for (int i = 0; i < numPairs; i++) {
419                removeOnePair();
420            }
421        }
422    }
423
424}