MemberIdsSection.java revision 4dfe64e94f153ccd8db56bea7b8cdfb02e5bba08
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
17package com.android.dx.dex.file;
18
19import com.android.dx.util.DexException;
20import java.util.Formatter;
21import java.util.Map;
22import java.util.TreeMap;
23import java.util.concurrent.atomic.AtomicInteger;
24
25/**
26 * Member (field or method) refs list section of a {@code .dex} file.
27 */
28public abstract class MemberIdsSection extends UniformItemSection {
29    /** The largest addressable member is 0xffff, in the dex spec as field@CCCC or meth@CCCC. */
30    private static final int MAX_MEMBERS = 0x10000;
31
32    /**
33     * Constructs an instance. The file offset is initially unknown.
34     *
35     * @param name {@code null-ok;} the name of this instance, for annotation
36     * purposes
37     * @param file {@code non-null;} file that this instance is part of
38     */
39    public MemberIdsSection(String name, DexFile file) {
40        super(name, file, 4);
41    }
42
43    /** {@inheritDoc} */
44    @Override
45    protected void orderItems() {
46        int idx = 0;
47
48        if (items().size() > MAX_MEMBERS) {
49            throw new DexException(tooManyMembersMessage());
50        }
51
52        for (Object i : items()) {
53            ((MemberIdItem) i).setIndex(idx);
54            idx++;
55        }
56    }
57
58    private String tooManyMembersMessage() {
59        Map<String, AtomicInteger> membersByPackage = new TreeMap<String, AtomicInteger>();
60        for (Object member : items()) {
61            String packageName = ((MemberIdItem) member).getDefiningClass().getPackageName();
62            AtomicInteger count = membersByPackage.get(packageName);
63            if (count == null) {
64                count = new AtomicInteger();
65                membersByPackage.put(packageName, count);
66            }
67            count.incrementAndGet();
68        }
69
70        Formatter formatter = new Formatter();
71        String memberType = this instanceof MethodIdsSection ? "methods" : "fields";
72        formatter.format("Too many %s: %d; max is %d. By package:",
73                memberType, items().size(), MAX_MEMBERS);
74        for (Map.Entry<String, AtomicInteger> entry : membersByPackage.entrySet()) {
75            formatter.format("%n%6d %s", entry.getValue().get(), entry.getKey());
76        }
77        return formatter.toString();
78    }
79}
80