|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 2021, 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 | +package org.graalvm.visualvm.lib.profiler.heapwalk.details.jdk; |
| 26 | + |
| 27 | +import java.nio.charset.Charset; |
| 28 | +import java.util.List; |
| 29 | +import org.graalvm.visualvm.lib.jfluid.heap.Heap; |
| 30 | +import org.graalvm.visualvm.lib.jfluid.heap.Instance; |
| 31 | +import org.graalvm.visualvm.lib.profiler.heapwalk.details.spi.DetailsProvider; |
| 32 | +import org.graalvm.visualvm.lib.profiler.heapwalk.details.spi.DetailsUtils; |
| 33 | +import org.openide.util.lookup.ServiceProvider; |
| 34 | + |
| 35 | +/** |
| 36 | + * |
| 37 | + * @author Tomas Hurka |
| 38 | + */ |
| 39 | +@ServiceProvider(service = DetailsProvider.class) |
| 40 | +public final class NioDetailsProvider extends DetailsProvider.Basic { |
| 41 | + |
| 42 | + private static final String UNIXPATH_MASK = "sun.nio.fs.UnixPath"; // NOI18N |
| 43 | + private static final String WINDOWSPATH_MASK = "sun.nio.fs.WindowsPath"; // NOI18N |
| 44 | + |
| 45 | + private long lastHeapId; |
| 46 | + private Charset lastJnuEncoding; |
| 47 | + |
| 48 | + public NioDetailsProvider() { |
| 49 | + super(UNIXPATH_MASK, WINDOWSPATH_MASK); |
| 50 | + } |
| 51 | + |
| 52 | + public String getDetailsString(String className, Instance instance) { |
| 53 | + if (UNIXPATH_MASK.equals(className)) { |
| 54 | + String path = DetailsUtils.getInstanceFieldString(instance, "stringValue"); // NOI18N |
| 55 | + if (path != null) { |
| 56 | + return path; |
| 57 | + } |
| 58 | + Charset encoding = getJnuEncoding(instance.getJavaClass().getHeap()); |
| 59 | + List<String> pathItems = DetailsUtils.getPrimitiveArrayFieldValues(instance, "path"); // NOI18N |
| 60 | + byte[] pathArr = DetailsUtils.getByteArray(pathItems); |
| 61 | + if (pathArr != null) { |
| 62 | + return new String(pathArr, encoding); |
| 63 | + } |
| 64 | + } else if (WINDOWSPATH_MASK.equals(className)) { |
| 65 | + return DetailsUtils.getInstanceFieldString(instance, "path"); // NOI18N |
| 66 | + } |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + private Charset getJnuEncoding(Heap heap) { |
| 71 | + if (lastHeapId != System.identityHashCode(heap)) { |
| 72 | + String encoding = heap.getSystemProperties().getProperty("sun.jnu.encoding", "UTF-8"); // NOI18N |
| 73 | + lastJnuEncoding = Charset.forName(encoding); |
| 74 | + lastHeapId = System.identityHashCode(heap); |
| 75 | + } |
| 76 | + return lastJnuEncoding; |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments