src/jdk.internal.vm.ci/share/classes/jdk.vm.ci.meta/src/jdk/vm/ci/meta/MetaUtil.java
equal
deleted
inserted
replaced
1 /* |
1 /* |
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
36 * of the enclosing class/classes of {@code clazz} (if any). This option is ignored |
36 * of the enclosing class/classes of {@code clazz} (if any). This option is ignored |
37 * if {@code clazz} denotes an anonymous or local class. |
37 * if {@code clazz} denotes an anonymous or local class. |
38 * @return the simple name |
38 * @return the simple name |
39 */ |
39 */ |
40 public static String getSimpleName(Class<?> clazz, boolean withEnclosingClass) { |
40 public static String getSimpleName(Class<?> clazz, boolean withEnclosingClass) { |
41 final String simpleName = clazz.getSimpleName(); |
41 final String simpleName = safeSimpleName(clazz); |
42 if (simpleName.length() != 0) { |
42 if (simpleName.length() != 0) { |
43 if (withEnclosingClass) { |
43 if (withEnclosingClass) { |
44 String prefix = ""; |
44 String prefix = ""; |
45 Class<?> enclosingClass = clazz; |
45 Class<?> enclosingClass = clazz; |
46 while ((enclosingClass = enclosingClass.getEnclosingClass()) != null) { |
46 while ((enclosingClass = enclosingClass.getEnclosingClass()) != null) { |
47 prefix = enclosingClass.getSimpleName() + "." + prefix; |
47 prefix = safeSimpleName(enclosingClass) + "." + prefix; |
48 } |
48 } |
49 return prefix + simpleName; |
49 return prefix + simpleName; |
50 } |
50 } |
51 return simpleName; |
51 return simpleName; |
52 } |
52 } |
59 index = name.lastIndexOf('.', index); |
59 index = name.lastIndexOf('.', index); |
60 if (index == -1) { |
60 if (index == -1) { |
61 return name; |
61 return name; |
62 } |
62 } |
63 return name.substring(index + 1); |
63 return name.substring(index + 1); |
|
64 } |
|
65 |
|
66 private static String safeSimpleName(Class<?> clazz) { |
|
67 try { |
|
68 return clazz.getSimpleName(); |
|
69 } catch (InternalError e) { |
|
70 // Scala inner class names do not always start with '$', |
|
71 // causing Class.getSimpleName to throw an InternalError |
|
72 Class<?> enclosingClass = clazz.getEnclosingClass(); |
|
73 String fqn = clazz.getName(); |
|
74 if (enclosingClass == null) { |
|
75 // Should never happen given logic in |
|
76 // Class.getSimpleName but best be safe |
|
77 return fqn; |
|
78 } |
|
79 String enclosingFQN = enclosingClass.getName(); |
|
80 int length = fqn.length(); |
|
81 if (enclosingFQN.length() >= length) { |
|
82 // Should also never happen |
|
83 return fqn; |
|
84 } |
|
85 return fqn.substring(enclosingFQN.length()); |
|
86 } |
64 } |
87 } |
65 |
88 |
66 /** |
89 /** |
67 * Converts a type name in internal form to an external form. |
90 * Converts a type name in internal form to an external form. |
68 * |
91 * |