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