src/java.base/share/classes/jdk/internal/jimage/decompressor/SignatureParser.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
package jdk.internal.jimage.decompressor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 * A descriptor parser used to extract java type strings from
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 * UTF_8 descriptors.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
 * @implNote This class needs to maintain JDK 8 source compatibility.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
 * It is used internally in the JDK to implement jimage/jrtfs access,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 * but also compiled and delivered as part of the jrtfs.jar to support access
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * to the jimage file provided by the shipped JDK by tools running on JDK 8.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
public class SignatureParser {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
   public static class ParseResult {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
        public final List<String> types = new ArrayList<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
        public String formatted;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
        private ParseResult() {}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
    private SignatureParser() {}
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    public static String reconstruct(String formatted, List<String> arguments) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        int arg_index = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
        char[] chars = formatted.toCharArray();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        StringBuilder out = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        for (int i = 0; i < chars.length; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
            char c = chars[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
            out.append(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
            switch (c) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
                case 'L': {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
                    String pkg = arguments.get(arg_index);
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
    63
                    if(!pkg.isEmpty()) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
                        out.append(pkg).append("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
                    arg_index+=1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
                    out.append(arguments.get(arg_index));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
                    arg_index+=1;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
                default: {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        return out.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
    public static ParseResult parseSignatureDescriptor(String str) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        ParseResult res = new ParseResult();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        char[] chars = str.toCharArray();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        StringBuilder type = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        StringBuilder formatted = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        for (int i = 0; i < chars.length; i++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
            char c = chars[i];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            switch (c) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                case ';':
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                case ':':
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                case '<': {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                    if(type != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                        String fullName = type.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                        int endIndex = fullName.lastIndexOf("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                        String clazz = fullName;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                        String pkg = "";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                        if(endIndex != -1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                            pkg = fullName.substring(0, endIndex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                            clazz = fullName.substring(endIndex+1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                        res.types.add(pkg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                        res.types.add(clazz);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                    formatted.append(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
                    type = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
                case 'L': {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                    if(type == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                        type = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                        formatted.append(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                        type.append(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                default: {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                    if(type == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                        formatted.append(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                    } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                        type.append(c);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        res.formatted = formatted.toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        return res;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
}