Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/CPPExpressions.java
41161 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.utilities;2526import java.util.regex.*;2728/** Provides helper routines for parsing simple C++ expressions such29as "(JavaThread *) 0xf00" and "Universe::_collectedHeap". */3031public class CPPExpressions {32private static Pattern castPattern;3334/** Represents a cast expression such as "(JavaThread *)350xf00". Returns both the type to which we are casting as well as36the address. */37public static class CastExpr {38private String type;39private String address;4041private CastExpr(String type, String address) {42this.type = type;43this.address = address;44}4546public String getType() {47return type;48}4950public String getAddress() {51return address;52}53}5455/** Represents a static field expression such as "Universe::_collectedHeap". */56public static class StaticFieldExpr {57private String containingType;58private String fieldName;5960private StaticFieldExpr(String containingType, String fieldName) {61this.containingType = containingType;62this.fieldName = fieldName;63}6465public String getContainingType() {66return containingType;67}6869public String getFieldName() {70return fieldName;71}72}7374/** Attempts to parse the given string into a CastExpr. Returns null75if the string did not match the supported pattern. */76public static CastExpr parseCast(String expr) {77if (castPattern == null) {78castPattern = Pattern.compile("\\s*\\(\\s*([0-9A-Za-z:_]*)\\s*\\*\\s*\\)\\s*([0-9a-zA-Z]*)\\s*");79}80Matcher matcher = castPattern.matcher(expr);81if (matcher.matches()) {82String type = matcher.group(1);83String addr = matcher.group(2);84return new CastExpr(type, addr);85}86return null;87}8889/** Attempts to parse the given string into a90StaticFieldExpr. Returns null if the string did not match the91supported pattern. */92public static StaticFieldExpr parseStaticField(String expr) {93String sep = "::";94int idx = expr.lastIndexOf(sep);95if (idx < 0) {96return null;97}98String containingType = expr.substring(0, idx);99String fieldName = expr.substring(idx + sep.length(), expr.length());100return new StaticFieldExpr(containingType, fieldName);101}102}103104105