Home | History | Annotate | Download | only in file
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.dx.dex.file;
     18 
     19 import com.android.dex.DexFormat;
     20 import com.android.dex.DexIndexOverflowException;
     21 import java.util.Formatter;
     22 import java.util.Map;
     23 import java.util.TreeMap;
     24 import java.util.concurrent.atomic.AtomicInteger;
     25 
     26 /**
     27  * Member (field or method) refs list section of a {@code .dex} file.
     28  */
     29 public abstract class MemberIdsSection extends UniformItemSection {
     30 
     31     /**
     32      * Constructs an instance. The file offset is initially unknown.
     33      *
     34      * @param name {@code null-ok;} the name of this instance, for annotation
     35      * purposes
     36      * @param file {@code non-null;} file that this instance is part of
     37      */
     38     public MemberIdsSection(String name, DexFile file) {
     39         super(name, file, 4);
     40     }
     41 
     42     /** {@inheritDoc} */
     43     @Override
     44     protected void orderItems() {
     45         int idx = 0;
     46 
     47         if (items().size() > DexFormat.MAX_MEMBER_IDX + 1) {
     48             throw new DexIndexOverflowException(getTooManyMembersMessage());
     49         }
     50 
     51         for (Object i : items()) {
     52             ((MemberIdItem) i).setIndex(idx);
     53             idx++;
     54         }
     55     }
     56 
     57     private String getTooManyMembersMessage() {
     58         Map<String, AtomicInteger> membersByPackage = new TreeMap<String, AtomicInteger>();
     59         for (Object member : items()) {
     60             String packageName = ((MemberIdItem) member).getDefiningClass().getPackageName();
     61             AtomicInteger count = membersByPackage.get(packageName);
     62             if (count == null) {
     63                 count = new AtomicInteger();
     64                 membersByPackage.put(packageName, count);
     65             }
     66             count.incrementAndGet();
     67         }
     68 
     69         Formatter formatter = new Formatter();
     70         try {
     71             String memberType = this instanceof MethodIdsSection ? "method" : "field";
     72             formatter.format("Too many %1$s references to fit in one dex file: %2$d; max is %3$d.%n" +
     73                             "You may try using multi-dex. If multi-dex is enabled then the list of " +
     74                             "classes for the main dex list is too large.%n" +
     75                     "References by package:",
     76                     memberType, items().size(), DexFormat.MAX_MEMBER_IDX + 1);
     77             for (Map.Entry<String, AtomicInteger> entry : membersByPackage.entrySet()) {
     78                 formatter.format("%n%6d %s", entry.getValue().get(), entry.getKey());
     79             }
     80             return formatter.toString();
     81         } finally {
     82             formatter.close();
     83         }
     84     }
     85 
     86 }
     87