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 
  26 package com.sun.tools.jdeprscan.scan;
  27 
  28 import java.util.ArrayList;
  29 import java.util.Formatter;
  30 import java.util.List;
  31 import java.util.Locale;
  32 
  33 import com.sun.tools.classfile.ClassFile;
  34 import com.sun.tools.classfile.ConstantPool;
  35 
  36 import 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  */
  47 class CPEntries {
  48     List<ConstantPool.CONSTANT_Class_info> classes;
  49     List<ConstantPool.CONSTANT_Fieldref_info> fieldRefs;
  50     List<ConstantPool.CONSTANT_Methodref_info> methodRefs;
  51     List<ConstantPool.CONSTANT_InterfaceMethodref_info> interfaceMethodRefs;
  52 
  53     CPEntries() {
  54         classes = new ArrayList<>();
  55         fieldRefs = new ArrayList<>();
  56         methodRefs = new ArrayList<>();
  57         interfaceMethodRefs = new ArrayList<>();
  58     }
  59 
  60     public static CPEntries loadFrom(ClassFile cf) {
  61         CPEntries entries = new CPEntries();
  62         for (CPInfo cpi : cf.constant_pool.entries()) {
  63             cpi.accept(new CPSelector(), entries);
  64         }
  65         return entries;
  66     }
  67 
  68     @Override
  69     public String toString() {
  70         StringBuilder sb = new StringBuilder();
  71         Formatter f = new Formatter(sb, Locale.getDefault());
  72         f.format("Classes:%n");
  73         f.format("%s%n", classes);
  74         f.format("FieldRefs:%n");
  75         f.format("%s%n", fieldRefs);
  76         f.format("MethodRefs:%n");
  77         f.format("%s%n", methodRefs);
  78         f.format("InterfaceMethodRefs:%n");
  79         f.format("%s%n", interfaceMethodRefs);
  80         f.flush();
  81         return sb.toString();
  82     }
  83 }