CPEntries.java revision 3615:871b60b0c091
1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.jdeprscan.scan;
27
28import java.util.ArrayList;
29import java.util.Formatter;
30import java.util.List;
31import java.util.Locale;
32
33import com.sun.tools.classfile.ClassFile;
34import com.sun.tools.classfile.ConstantPool;
35
36import static com.sun.tools.classfile.ConstantPool.CPInfo;
37
38/**
39 * A container for selected constant pool entries. There are currently
40 * lists that contain the following types of CP entries:
41 *
42 *  - CONSTANT_Class_info
43 *  - CONSTANT_Fieldref_info
44 *  - CONSTANT_Methodref_info
45 *  - CONSTANT_InterfaceMethodref_info
46 */
47class CPEntries {
48    final List<ConstantPool.CONSTANT_Class_info> classes = new ArrayList<>();
49    final List<ConstantPool.CONSTANT_Fieldref_info> fieldRefs = new ArrayList<>();
50    final List<ConstantPool.CONSTANT_Methodref_info> methodRefs = new ArrayList<>();
51    final List<ConstantPool.CONSTANT_InterfaceMethodref_info> intfMethodRefs = new ArrayList<>();
52
53    public static CPEntries loadFrom(ClassFile cf) {
54        CPEntries entries = new CPEntries();
55        for (CPInfo cpi : cf.constant_pool.entries()) {
56            cpi.accept(new CPSelector(), entries);
57        }
58        return entries;
59    }
60
61    @Override
62    public String toString() {
63        StringBuilder sb = new StringBuilder();
64        Formatter f = new Formatter(sb, Locale.getDefault());
65        f.format("Classes:%n");
66        f.format("%s%n", classes);
67        f.format("FieldRefs:%n");
68        f.format("%s%n", fieldRefs);
69        f.format("MethodRefs:%n");
70        f.format("%s%n", methodRefs);
71        f.format("InterfaceMethodRefs:%n");
72        f.format("%s%n", intfMethodRefs);
73        f.flush();
74        return sb.toString();
75    }
76}
77