jdk/make/modules/tools/src/com/sun/classanalyzer/Klass.java
author jcoomes
Thu, 20 May 2010 08:32:11 -0700
changeset 5545 c531522ec99a
parent 4524 697144bd8b04
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4524
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     1
/*
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     4
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     8
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    13
 * accompanied this code).
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    14
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    18
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    21
 * have any questions.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    22
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    23
 */
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    24
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    25
package com.sun.classanalyzer;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    26
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    27
import java.io.File;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    28
import java.util.ArrayList;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    29
import java.util.HashMap;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    30
import java.util.List;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    31
import java.util.Map;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    32
import java.util.Set;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    33
import java.util.SortedMap;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    34
import java.util.TreeMap;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    35
import java.util.TreeSet;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    36
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    37
import com.sun.tools.classfile.AccessFlags;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    38
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    39
/**
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    40
 *
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    41
 * @author Mandy Chung
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    42
 */
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    43
public class Klass implements Comparable<Klass> {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    44
    private final String classname;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    45
    private final String packagename;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    46
    private Module module;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    47
    private boolean isJavaLangObject;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    48
    private String[] paths;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    49
    private Map<String, Set<Method>> methods;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    50
    private AccessFlags accessFlags;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    51
    private long filesize;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    52
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    53
    private SortedMap<Klass, Set<ResolutionInfo>> deps;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    54
    private SortedMap<Klass, Set<ResolutionInfo>> referrers;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    55
    private List<AnnotatedDependency> annotatedDeps;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    56
    private Set<String> classForNameRefs;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    57
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    58
    private Klass(String classname) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    59
        this.classname = classname;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    60
        this.paths = classname.replace('.', '/').split("/");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    61
        this.isJavaLangObject = classname.equals("java.lang.Object");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    62
        this.deps = new TreeMap<Klass, Set<ResolutionInfo>>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    63
        this.referrers = new TreeMap<Klass, Set<ResolutionInfo>>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    64
        this.methods = new HashMap<String, Set<Method>>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    65
        this.annotatedDeps = new ArrayList<AnnotatedDependency>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    66
        this.classForNameRefs = new TreeSet<String>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    67
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    68
        int pos = classname.lastIndexOf('.');
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    69
        this.packagename = (pos > 0) ? classname.substring(0, pos) : "<unnamed>";
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    70
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    71
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    72
    String getBasename() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    73
        return paths[paths.length - 1];
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    74
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    75
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    76
    String getClassName() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    77
        return classname;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    78
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    79
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    80
    String getPackageName() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    81
        return packagename;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    82
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    83
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    84
    String getClassFilePathname() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    85
        StringBuilder sb = new StringBuilder(paths[0]);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    86
        for (int i = 1; i < paths.length; i++) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    87
            String p = paths[i];
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    88
            sb.append(File.separator).append(p);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    89
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    90
        return sb.append(".class").toString();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    91
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    92
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    93
    boolean isPublic() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    94
        return accessFlags == null || accessFlags.is(AccessFlags.ACC_PUBLIC);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    95
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    96
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    97
    Module getModule() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    98
        return module;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    99
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   100
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   101
    void setModule(Module m) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   102
        if (module != null) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   103
            throw new RuntimeException("Module for " + this + " already set");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   104
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   105
        this.module = m;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   106
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   107
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   108
    Set<Klass> getReferencedClasses() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   109
        return deps.keySet();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   110
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   111
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   112
    Set<Klass> getReferencingClasses() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   113
        return referrers.keySet();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   114
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   115
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   116
    void setAccessFlags(int flags) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   117
        this.accessFlags = new AccessFlags(flags);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   118
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   119
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   120
    void setFileSize(long size) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   121
        this.filesize = size;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   122
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   123
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   124
    long getFileSize() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   125
        return this.filesize;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   126
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   127
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   128
    boolean exists() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   129
        return filesize > 0;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   130
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   131
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   132
    boolean skip(Klass k) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   133
        // skip if either class is a root or same class
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   134
        return k.isJavaLangObject || this == k || k.classname.equals(classname);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   135
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   136
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   137
    void addDep(Method callee, ResolutionInfo resInfo) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   138
        addDep(callee.getKlass(), resInfo);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   139
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   140
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   141
    void addDep(Klass ref, ResolutionInfo ri) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   142
        if (skip(ref)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   143
            return;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   144
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   145
        Set<ResolutionInfo> resInfos;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   146
        if (!deps.containsKey(ref)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   147
            resInfos = new TreeSet<ResolutionInfo>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   148
            deps.put(ref, resInfos);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   149
        } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   150
            resInfos = deps.get(ref);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   151
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   152
        resInfos.add(ri);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   153
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   154
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   155
    void addReferrer(Method caller, ResolutionInfo resInfo) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   156
        addReferrer(caller.getKlass(), resInfo);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   157
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   158
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   159
    void addReferrer(Klass k, ResolutionInfo ri) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   160
        if (skip(k)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   161
            return;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   162
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   163
        Set<ResolutionInfo> resInfos;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   164
        if (!referrers.containsKey(k)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   165
            resInfos = new TreeSet<ResolutionInfo>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   166
            referrers.put(k, resInfos);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   167
        } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   168
            resInfos = referrers.get(k);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   169
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   170
        resInfos.add(ri);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   171
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   172
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   173
    Method getMethod(String name) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   174
        return getMethod(name, "");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   175
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   176
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   177
    Method getMethod(String name, String signature) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   178
        Set<Method> set;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   179
        if (methods.containsKey(name)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   180
            set = methods.get(name);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   181
        } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   182
            set = new TreeSet<Method>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   183
            methods.put(name, set);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   184
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   185
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   186
        for (Method m : set) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   187
            if (m.getName().equals(name) && m.getSignature().equals(signature)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   188
                return m;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   189
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   190
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   191
        Method m = new Method(this, name, signature);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   192
        set.add(m);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   193
        return m;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   194
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   195
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   196
    @Override
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   197
    public String toString() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   198
        return classname;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   199
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   200
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   201
    @Override
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   202
    public int compareTo(Klass o) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   203
        return classname.compareTo(o.classname);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   204
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   205
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   206
    void addAnnotatedDep(AnnotatedDependency dep) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   207
        annotatedDeps.add(dep);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   208
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   209
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   210
    void addClassForNameReference(String method) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   211
        classForNameRefs.add(method);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   212
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   213
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   214
    List<AnnotatedDependency> getAnnotatedDeps() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   215
        return annotatedDeps;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   216
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   217
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   218
    private static Map<String, Klass> classes = new TreeMap<String, Klass>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   219
    static Set<Klass> getAllClasses() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   220
        return new TreeSet<Klass>(classes.values());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   221
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   222
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   223
    static Klass findKlassFromPathname(String filename) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   224
        String name = filename;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   225
        if (filename.endsWith(".class")) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   226
            name = filename.substring(0, filename.length() - 6);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   227
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   228
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   229
        // trim ".class"
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   230
        name = name.replace('/', '.');
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   231
        for (Klass k : classes.values()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   232
            if (name.endsWith(k.getClassName())) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   233
                return k;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   234
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   235
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   236
        return null;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   237
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   238
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   239
    static Klass findKlass(String classname) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   240
        return classes.get(classname);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   241
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   242
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   243
    static Klass getKlass(String name) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   244
        Klass k;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   245
        String classname = name.replace('/', '.');
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   246
        if (classname.charAt(classname.length() - 1) == ';') {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   247
            classname = classname.substring(0, classname.length() - 1);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   248
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   249
        if (classes.containsKey(classname)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   250
            k = classes.get(classname);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   251
        } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   252
            k = new Klass(classname);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   253
            classes.put(classname, k);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   254
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   255
        return k;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   256
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   257
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   258
    public class Method implements Comparable<Method> {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   259
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   260
        private final Klass k;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   261
        private final String method;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   262
        private final String signature;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   263
        private long codeLength;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   264
        // non-primitive types only
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   265
        private final List<Klass> argTypes;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   266
        private final Klass returnType;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   267
        boolean isAbstract = false;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   268
        boolean marked = false;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   269
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   270
        public Method(Klass k, String method, String signature) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   271
            this(k, method, signature, null, null);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   272
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   273
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   274
        public Method(Klass k, String method, String signature, Klass returnType, List<Klass> argTypes) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   275
            this.k = k;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   276
            this.method = method;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   277
            this.signature = signature;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   278
            this.argTypes = argTypes;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   279
            this.returnType = returnType;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   280
            this.codeLength = 0;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   281
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   282
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   283
        public Klass getKlass() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   284
            return k;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   285
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   286
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   287
        public String getName() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   288
            return method;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   289
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   290
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   291
        public String getSignature() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   292
            return signature;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   293
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   294
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   295
        public Klass getReturnType() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   296
            return returnType;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   297
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   298
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   299
        public List<Klass> argTypes() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   300
            return argTypes;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   301
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   302
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   303
        public void setCodeLength(long len) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   304
            this.codeLength = len;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   305
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   306
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   307
        public long getCodeLength() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   308
            return codeLength;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   309
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   310
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   311
        @Override
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   312
        public boolean equals(Object o) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   313
            if (o instanceof Method) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   314
                return compareTo((Method) o) == 0;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   315
            } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   316
                return false;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   317
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   318
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   319
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   320
        @Override
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   321
        public int hashCode() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   322
            int hash = 3;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   323
            hash = 71 * hash + (this.k != null ? this.k.hashCode() : 0);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   324
            hash = 71 * hash + (this.method != null ? this.method.hashCode() : 0);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   325
            return hash;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   326
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   327
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   328
        @Override
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   329
        public String toString() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   330
            if (signature.isEmpty()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   331
                return k.classname + "." + method;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   332
            } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   333
                return signature;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   334
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   335
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   336
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   337
        public String toHtmlString() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   338
            return toString().replace("<", "&lt;").replace(">", "&gt;");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   339
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   340
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   341
        boolean isClinit() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   342
            return method.equals("<clinit>");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   343
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   344
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   345
        public int compareTo(Method m) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   346
            if (k == m.getKlass()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   347
                if (method.equals(m.method)) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   348
                    return signature.compareTo(m.signature);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   349
                } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   350
                    return method.compareTo(m.method);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   351
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   352
            } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   353
                return k.compareTo(m.getKlass());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   354
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   355
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   356
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   357
}