在 Java 源代码中使用 ASTParser 提取 Catch 子句 [英] Extracting Catch Clause Using ASTParser in Java Source code

查看:59
本文介绍了在 Java 源代码中使用 ASTParser 提取 Catch 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java ASTParser 来解析我的 Java 代码.我正在使用以下代码.使用此代码,我可以提取if"&尝试"语句.但我无法提取catch 子句".有没有人知道如何在此单独提取catch-clause.以下代码没有给出任何错误,但它没有在 catch 子句中打印任何内容.

I am using Java ASTParser to parse my java code. I am using following code. Using this code I am able to extract "if" & "try" statements. But I am not able to extract "catch clause". Does any one have any idea how to extract catch-clause separately in this. Following code is not giving any error, but its not printing anything in catch clause.

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;
      }

   });
}

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

Following is the example code that I am trying to query:

     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,因此解析器不会在您的节点内部进行探索.

it's because after visiting your IfStatement node your return false, so the parser does not explore inside your node.

返回true,它会起作用.

return true and it will work.

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

这篇关于在 Java 源代码中使用 ASTParser 提取 Catch 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆