001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. 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, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.compress.harmony.unpack200; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.util.ArrayList; 022import java.util.HashMap; 023import java.util.HashSet; 024import java.util.List; 025import java.util.Map; 026import java.util.Set; 027 028import org.apache.commons.compress.harmony.pack200.Codec; 029import org.apache.commons.compress.harmony.pack200.Pack200Exception; 030import org.apache.commons.compress.harmony.unpack200.bytecode.CPClass; 031import org.apache.commons.compress.harmony.unpack200.bytecode.ClassConstantPool; 032import org.apache.commons.compress.harmony.unpack200.bytecode.ClassFileEntry; 033import org.apache.commons.compress.harmony.unpack200.bytecode.ConstantPoolEntry; 034 035/** 036 * Inner Class Bands 037 */ 038public class IcBands extends BandSet { 039 040 private IcTuple[] icAll; 041 042 private final String[] cpUTF8; 043 044 private final String[] cpClass; 045 046 private Map<String, IcTuple> thisClassToTuple; 047 private Map<String, List<IcTuple>> outerClassToTuples; 048 049 /** 050 * @param segment TODO 051 */ 052 public IcBands(final Segment segment) { 053 super(segment); 054 this.cpClass = segment.getCpBands().getCpClass(); 055 this.cpUTF8 = segment.getCpBands().getCpUTF8(); 056 } 057 058 /* 059 * (non-Javadoc) 060 * 061 * @see org.apache.commons.compress.harmony.unpack200.BandSet#unpack(java.io.InputStream) 062 */ 063 @Override 064 public void read(final InputStream in) throws IOException, Pack200Exception { 065 // Read IC bands 066 final int innerClassCount = header.getInnerClassCount(); 067 final int[] icThisClassInts = decodeBandInt("ic_this_class", in, Codec.UDELTA5, innerClassCount); 068 final String[] icThisClass = getReferences(icThisClassInts, cpClass); 069 final int[] icFlags = decodeBandInt("ic_flags", in, Codec.UNSIGNED5, innerClassCount); 070 final int outerClasses = SegmentUtils.countBit16(icFlags); 071 final int[] icOuterClassInts = decodeBandInt("ic_outer_class", in, Codec.DELTA5, outerClasses); 072 final String[] icOuterClass = new String[outerClasses]; 073 for (int i = 0; i < icOuterClass.length; i++) { 074 if (icOuterClassInts[i] == 0) { 075 icOuterClass[i] = null; 076 } else { 077 icOuterClass[i] = cpClass[icOuterClassInts[i] - 1]; 078 } 079 } 080 final int[] icNameInts = decodeBandInt("ic_name", in, Codec.DELTA5, outerClasses); 081 final String[] icName = new String[outerClasses]; 082 for (int i = 0; i < icName.length; i++) { 083 if (icNameInts[i] == 0) { 084 icName[i] = null; 085 } else { 086 icName[i] = cpUTF8[icNameInts[i] - 1]; 087 } 088 } 089 090 // Construct IC tuples 091 icAll = new IcTuple[icThisClass.length]; 092 int index = 0; 093 for (int i = 0; i < icThisClass.length; i++) { 094 final String icTupleC = icThisClass[i]; 095 final int icTupleF = icFlags[i]; 096 String icTupleC2 = null; 097 String icTupleN = null; 098 final int cIndex = icThisClassInts[i]; 099 int c2Index = -1; 100 int nIndex = -1; 101 if ((icFlags[i] & 1 << 16) != 0) { 102 icTupleC2 = icOuterClass[index]; 103 icTupleN = icName[index]; 104 c2Index = icOuterClassInts[index] - 1; 105 nIndex = icNameInts[index] - 1; 106 index++; 107 } 108 icAll[i] = new IcTuple(icTupleC, icTupleF, icTupleC2, icTupleN, cIndex, c2Index, nIndex, i); 109 } 110 } 111 112 @Override 113 public void unpack() throws IOException, Pack200Exception { 114 final IcTuple[] allTuples = getIcTuples(); 115 thisClassToTuple = new HashMap<>(allTuples.length); 116 outerClassToTuples = new HashMap<>(allTuples.length); 117 for (final IcTuple tuple : allTuples) { 118 119 // generate mapping thisClassString -> IcTuple 120 // presumably this relation is 1:1 121 // 122 final Object result = thisClassToTuple.put(tuple.thisClassString(), tuple); 123 if (result != null) { 124 throw new Error("Collision detected in <thisClassString, IcTuple> mapping. " 125 + "There are at least two inner clases with the same name."); 126 } 127 128 // generate mapping outerClassString -> IcTuple 129 // this relation is 1:M 130 131 // If it's not anon and the outer is not anon, it could be relevant 132 if ((!tuple.isAnonymous() && !tuple.outerIsAnonymous()) || (tuple.nestedExplicitFlagSet())) { 133 134 // add tuple to corresponding bucket 135 final String key = tuple.outerClassString(); 136 List<IcTuple> bucket = outerClassToTuples.get(key); 137 if (bucket == null) { 138 bucket = new ArrayList<>(); 139 outerClassToTuples.put(key, bucket); 140 } 141 bucket.add(tuple); 142 } 143 } 144 } 145 146 public IcTuple[] getIcTuples() { 147 return icAll; 148 } 149 150 /** 151 * Answer the relevant IcTuples for the specified className and class constant pool. 152 * 153 * @param className String name of the class X for ic_relevant(X) 154 * @param cp ClassConstantPool used to generate ic_relevant(X) 155 * @return array of IcTuple 156 */ 157 public IcTuple[] getRelevantIcTuples(final String className, final ClassConstantPool cp) { 158 final Set<IcTuple> relevantTuplesContains = new HashSet<>(); 159 final List<IcTuple> relevantTuples = new ArrayList<>(); 160 161 final List<IcTuple> relevantCandidates = outerClassToTuples.get(className); 162 if (relevantCandidates != null) { 163 for (int index = 0; index < relevantCandidates.size(); index++) { 164 final IcTuple tuple = relevantCandidates.get(index); 165 relevantTuplesContains.add(tuple); 166 relevantTuples.add(tuple); 167 } 168 } 169 170 final List<ClassFileEntry> entries = cp.entries(); 171 172 // For every class constant in both ic_this_class and cp, 173 // add it to ic_relevant. Repeat until no more 174 // changes to ic_relevant. 175 176 for (int eIndex = 0; eIndex < entries.size(); eIndex++) { 177 final ConstantPoolEntry entry = (ConstantPoolEntry) entries.get(eIndex); 178 if (entry instanceof CPClass) { 179 final CPClass clazz = (CPClass) entry; 180 final IcTuple relevant = thisClassToTuple.get(clazz.name); 181 if (relevant != null && relevantTuplesContains.add(relevant)) { 182 relevantTuples.add(relevant); 183 } 184 } 185 } 186 187 // Not part of spec: fix up by adding to relevantTuples the parents 188 // of inner classes which are themselves inner classes. 189 // i.e., I think that if Foo$Bar$Baz gets added, Foo$Bar needs to be 190 // added 191 // as well. 192 193 final List<IcTuple> tuplesToScan = new ArrayList<>(relevantTuples); 194 final List<IcTuple> tuplesToAdd = new ArrayList<>(); 195 196 while (tuplesToScan.size() > 0) { 197 198 tuplesToAdd.clear(); 199 for (int index = 0; index < tuplesToScan.size(); index++) { 200 final IcTuple aRelevantTuple = tuplesToScan.get(index); 201 final IcTuple relevant = thisClassToTuple.get(aRelevantTuple.outerClassString()); 202 if (relevant != null && !aRelevantTuple.outerIsAnonymous()) { 203 tuplesToAdd.add(relevant); 204 } 205 } 206 207 tuplesToScan.clear(); 208 for (int index = 0; index < tuplesToAdd.size(); index++) { 209 final IcTuple tuple = tuplesToAdd.get(index); 210 if (relevantTuplesContains.add(tuple)) { 211 relevantTuples.add(tuple); 212 tuplesToScan.add(tuple); 213 } 214 } 215 216 } 217 218 // End not part of the spec. Ugh. 219 220 // Now order the result as a subsequence of ic_all 221 relevantTuples.sort((arg0, arg1) -> { 222 final Integer index1 = Integer.valueOf(arg0.getTupleIndex()); 223 final Integer index2 = Integer.valueOf(arg1.getTupleIndex()); 224 return index1.compareTo(index2); 225 }); 226 227 return relevantTuples.toArray(IcTuple.EMPTY_ARRAY); 228 } 229 230}