java - 在 Java 源代码中使用 ASTParser 提取 Catch 子句

标签 java parsing

我正在使用 Java ASTParser 来解析我的 java 代码。我正在使用以下代码。使用此代码我可以提取“if”和“try”语句。但我无法提取“catch 条款”。有谁知道如何在此单独提取 catch 子句吗?以下代码没有给出任何错误,但它没有在 catch 子句中打印任何内容。

public static void methodVisitor(String content) 
{
   //debug("entering met visitor", "1");
   ASTParser metparse = ASTParser.newParser(AST.JLS3);
   metparse.setSource(content.toCharArray());
   metparse.setKind(ASTParser.K_STATEMENTS);
   Block block = (Block) metparse.createAST(null);

   block.accept(new ASTVisitor() {

      public boolean visit(IfStatement myif) {          
         System.out.println("myif="+myif.toString());
         return false;
      }


      public boolean visit(TryStatement mytry) {           
         System.out.println("mytry="+mytry.toString());
         return false;
      }

      public boolean visit(CatchClause mycatch) {                     
         System.out.println("mycatch="+mycatch.toString());
         return false;
      }

   });
}

以下是我尝试查询的示例代码:

     public class Clock2 extends Applet implements Runnable {
     private static final long serialVersionUID = 1L;
      Thread timer;                // The thread that displays clock
   int lastxs, lastys, lastxm,
    lastym, lastxh, lastyh;  // Dimensions used to draw hands
SimpleDateFormat formatter;  // Formats the date displayed
String lastdate;             // String to hold date displayed
Font clockFaceFont;          // Font for number display on clock
Date currentDate;            // Used to get date to display
Color handColor;             // Color of main hands and dial
Color numberColor;           // Color of second hand and numbers

@Override
public void init() {
    lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
    formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
    currentDate = new Date();
    lastdate = formatter.format(currentDate);
    clockFaceFont = new Font("Serif", Font.PLAIN, 14);
    handColor = Color.blue;
    numberColor = Color.darkGray;

    try {
        setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),16)));
    } catch (Exception e) {
        // Ignore
    }
    try {
        handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),16));
    } catch (Exception e) {
        // Ignore
    }
    try {
        numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),16));
    } catch (Exception e) {
        // Ignore
    }
    resize(300,300);              // Set clock window size
}

}

最佳答案

这是因为在访问您的 IfStatement 节点后,您返回 false,因此解析器不会在您的节点内部进行探索。

返回 true 就可以了。

  public boolean visit(IfStatement myif) {          
     System.out.println("myif="+myif.toString());
     return true;
  }

关于java - 在 Java 源代码中使用 ASTParser 提取 Catch 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25340834/

相关文章:

java - 仅允许本地连接。 Chrome 无法启动 : crashed.(未知错误:DevToolsActivePort 文件不存在

java - Swing:确定拖动结束时包含的对象

java - Android – Facebook SDK AppEventsLogger logEvent 不工作

Java 中的 Java 字节数组等效项

html - Beautifulsoup 解析特定标签下的数据

java - Eclipse IDE制作的jar中的音频文件

java - 在 Java 中,有一些 URL 解析器?

javascript - 如何使用 javascript 或 jQuery 解析 html 内容

c# - 将字符串转换为 BigInteger

java - 您将如何提高此正则表达式的效率