jdk/make/modules/tools/src/com/sun/classanalyzer/CheckDeps.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
package com.sun.classanalyzer;
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
import java.io.*;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    26
import java.util.*;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    27
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    28
/**
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    29
 * A simple tool to check module dependencies against a known list of
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    30
 * dependencies. The tool fails (by throwing a RuntimeException) is an
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    31
 * unexpected dependency is detected.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    32
 */
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    33
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    34
public class CheckDeps {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    35
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
     * Represents a dependency from one module to another module. The dependency
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    38
     * may be optional.
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
    static class Dependency {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    41
        private final String module;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    42
        private final String other;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    43
        private final boolean optional;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    44
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    45
        private Dependency(String module, String other, boolean optional) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    46
            this.module = module;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    47
            this.other = other;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    48
            this.optional = optional;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    49
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    50
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    51
        String module()      { return module; }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    52
        String other()       { return other; }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    53
        boolean isOptional() { return optional; }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    54
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    55
        /**
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    56
         * Parses a dependency in one of the following forms:
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    57
         *   a -> b
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    58
         *   [optional] a -> b
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    59
         */
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    60
        static Dependency fromString(String s) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    61
            String[] components = s.split(" ");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    62
            int count = components.length;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    63
            if (count != 3 && count != 4)
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    64
                throw new IllegalArgumentException(s);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    65
            boolean optional = (count == 4);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    66
            if (optional && !components[0].equals("[optional]"))
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    67
                throw new IllegalArgumentException(s);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    68
            String arrow = optional ? components[2] : components[1];
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    69
            if (!arrow.equals("->"))
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    70
                throw new IllegalArgumentException(s);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    71
            String module = optional ? components[1] : components[0];
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    72
            String other = optional ? components[3] : components[2];
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    73
            return new Dependency(module, other, optional);
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
        @Override public String toString() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    77
            StringBuilder sb = new StringBuilder();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    78
            if (optional)
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    79
                sb.append("[optional] ");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    80
            sb.append(module);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    81
            sb.append(" -> ");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    82
            sb.append(other);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    83
            return sb.toString();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    84
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    85
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    86
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    87
    /**
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    88
     * Represents the "tail"
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
    static class DependencyTail {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    91
        private final String module;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    92
        private final boolean optional;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    93
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    94
        DependencyTail(String module, boolean optional) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    95
            this.module = module;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    96
            this.optional = optional;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    97
        }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    98
        String module()      { return module; }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
    99
        boolean isOptional() { return optional; }
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
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   102
    static void usage() {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   103
        System.out.println("java CheckDeps file1 file2");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   104
        System.out.println("  where file1 is the expected dependencies and file2 is");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   105
        System.out.println("  the actual dependencies. Both files are assumed to be");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   106
        System.out.println("  in modules.summary format (see ClassAnalyzer tool).");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   107
        System.out.println();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   108
        System.out.println("Example usages:");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   109
        System.out.println("  java CheckDeps make/modules/modules.summary " +
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   110
            "$(OUTPUTDIR)/modules.summary");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   111
        System.exit(-1);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   112
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   113
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   114
    public static void main(String[] args) throws IOException {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   115
        if (args.length != 2)
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   116
            usage();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   117
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   118
        // maps a module to the list of modules that it depends on
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   119
        Map<String,List<DependencyTail>> expected =
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   120
            new HashMap<String,List<DependencyTail>>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   121
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   122
        // parse the expected dependencies file
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   123
        Scanner s;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   124
        s = new Scanner(new FileInputStream(args[0]));
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   125
        try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   126
            while (s.hasNextLine()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   127
                Dependency ref = Dependency.fromString(s.nextLine());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   128
                if (ref != null) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   129
                    String module = ref.module();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   130
                    List<DependencyTail> list = expected.get(module);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   131
                    if (list == null) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   132
                        list = new ArrayList<DependencyTail>();
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   133
                        expected.put(module, list);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   134
                    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   135
                    list.add(new DependencyTail(ref.other(), ref.isOptional()));
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
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   138
        } finally {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   139
            s.close();
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
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   142
        // parse the actual dependencies file, checking each dependency
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   143
        // against the expected list.
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   144
        boolean fail = false;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   145
        s = new Scanner(new FileInputStream(args[1]));
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   146
        try {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   147
            while (s.hasNextLine()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   148
                Dependency dep = Dependency.fromString(s.nextLine());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   149
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   150
                // check if this dependency is expected
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   151
                List<DependencyTail> list = expected.get(dep.module());
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   152
                DependencyTail tail = null;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   153
                if (list != null) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   154
                    for (DependencyTail t: list) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   155
                        if (t.module().equals(dep.other())) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   156
                            tail = t;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   157
                            break;
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
                    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   160
                }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   161
                if (tail == null) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   162
                    System.err.println("Unexpected dependency: " + dep);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   163
                    fail = true;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   164
                } else {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   165
                    // hard dependency when optional dependency is expected
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   166
                    if (tail.isOptional() != dep.isOptional()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   167
                        if (tail.isOptional()) {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   168
                            System.err.println("Unexpected dependency: " + dep);
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   169
                            fail = true;
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   170
                        }
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
            }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   174
        } finally {
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   175
            s.close();
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
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   178
        if (fail)
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   179
            throw new RuntimeException("Unexpected dependencies found");
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   180
    }
697144bd8b04 6909572: Add a new target for building modules
mchung
parents:
diff changeset
   181
}