• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ddmuilib.logcat;
17 
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20 
21 /**
22  * Helper class that can determine if a string matches the exception
23  * stack trace pattern, and if so, can provide the java source file
24  * and line where the exception occured.
25  */
26 public final class LogCatStackTraceParser {
27     /** Regex to match a stack trace line. E.g.:
28      *          at com.foo.Class.method(FileName.extension:10)
29      *  extension is typically java, but can be anything (java/groovy/scala/..).
30      */
31     private static final String EXCEPTION_LINE_REGEX =
32             "\\s*at\\ (.*)\\((.*)\\..*\\:(\\d+)\\)"; //$NON-NLS-1$
33 
34     private static final Pattern EXCEPTION_LINE_PATTERN =
35             Pattern.compile(EXCEPTION_LINE_REGEX);
36 
37     /**
38      * Identify if a input line matches the expected pattern
39      * for a stack trace from an exception.
40      */
isValidExceptionTrace(String line)41     public boolean isValidExceptionTrace(String line) {
42         return EXCEPTION_LINE_PATTERN.matcher(line).find();
43     }
44 
45     /**
46      * Get fully qualified method name that threw the exception.
47      * @param line line from the stack trace, must have been validated with
48      * {@link LogCatStackTraceParser#isValidExceptionTrace(String)} before calling this method.
49      * @return fully qualified method name
50      */
getMethodName(String line)51     public String getMethodName(String line) {
52         Matcher m = EXCEPTION_LINE_PATTERN.matcher(line);
53         m.find();
54         return m.group(1);
55     }
56 
57     /**
58      * Get source file name where exception was generated. Input line must be first validated with
59      * {@link LogCatStackTraceParser#isValidExceptionTrace(String)}.
60      */
getFileName(String line)61     public String getFileName(String line) {
62         Matcher m = EXCEPTION_LINE_PATTERN.matcher(line);
63         m.find();
64         return m.group(2);
65     }
66 
67     /**
68      * Get line number where exception was generated. Input line must be first validated with
69      * {@link LogCatStackTraceParser#isValidExceptionTrace(String)}.
70      */
getLineNumber(String line)71     public int getLineNumber(String line) {
72         Matcher m = EXCEPTION_LINE_PATTERN.matcher(line);
73         m.find();
74         try {
75             return Integer.parseInt(m.group(3));
76         } catch (NumberFormatException e) {
77             return 0;
78         }
79     }
80 
81 }
82