3

我正在编写一些代码来解析 java 源代码。我正在试验 Eclipse JDT AST 解析器。我的代码如下。(解析代码)。我正在针对我用 Java 编写的 Mailer 应用程序测试解析器(第二个代码片段)。我的解析器正在访问除 generateEmail() 和 debug() 方法之外的所有方法。我到处看了看,但我一生都无法理解为什么会发生这种情况。谁能告诉我我做错了什么?是内存问题吗?我没有收到任何 OutOfMemoryException

我想使用 MethodVisitor 方法访问特定方法,以访问特定方法中的语句和变量。

我的解析代码

public class RuleEngine {

public static void parse(String file) {
    File java = new File(file);
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    String code = readFile(java);
    parser.setSource(code.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);


    cu.accept(new ASTVisitor() {

        public boolean visit(ImportDeclaration id) {
            Name imp = id.getName();
            debug("import", id.getName().getFullyQualifiedName());
            return false;
        }

        public boolean visit(VariableDeclarationFragment node) {
            SimpleName name = node.getName();
            debug("var.declaration", (name.getFullyQualifiedName() + ":" + cu.getLineNumber(name.getStartPosition())));
            return false; // do not continue 
        }

        public boolean visit(MethodDeclaration method) {
            debug("method", method.getName().getFullyQualifiedName());
            debug("method.return", method.getReturnType2().toString());
            List<SingleVariableDeclaration> params = method.parameters();

            for(SingleVariableDeclaration param: params) {
                debug("param", param.getName().getFullyQualifiedName());
            }

            Block methodBlock = method.getBody();
            String myblock = methodBlock.toString();
            methodVisitor(myblock);
            return false;
        }


    });

}

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(VariableDeclarationFragment var) {
            debug("met.var", var.getName().getFullyQualifiedName());
            return false;
        }

        public boolean visit(SimpleName node) {
            debug("SimpleName node", node.getFullyQualifiedName());
            return false;
        }
        public boolean visit(IfStatement myif) {
            debug("if.statement", myif.toString());
            return false;
        }

    });
}

public static void debug(String ref, String message) {
    System.out.println(ref +": " + message);
}

public static void main(String[]args) {
    parse("MailerDaemon.java");
}

这是我的 MailerDaemon 代码

public boolean isBccMode() {
    return bccMode;
}

public void setBccMode(boolean bccMode) {
    this.bccMode = bccMode;
}

public void setServerPort(String serverPortAddr) {
    String[] elems = serverPortAddr.split("\\:");
    this.setServerAddr(elems[0]);
    this.setSmtpPort(elems[1]);
}

public String getServerAddr() {
    int i = 0;
    return serverAddr;
}
public void setServerAddr(String serverAddr) {
    this.serverAddr = serverAddr;
}
public boolean isSslOn() {
    return isSslOn;
}
public void setSslOn(boolean isSslOn) {
    this.isSslOn = isSslOn;
}
public String getSmtpPort() {
    return smtpPort;
}
public void setSmtpPort(String smtpPort) {
    this.smtpPort = smtpPort;
}
public String getFromEmail() {
    return fromEmail;
}
public void setFromEmail(String fromEmail) {
    this.fromEmail = fromEmail;
}
public String getToEmails() {
    return toEmails;
}
public void setToEmails(String toEmails) {
    this.toEmails = toEmails;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getSubject() {
    return subject;
}
public void setSubject(String subject) {
    this.subject = subject;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}
public String getCcList() {
    return ccList;
}
public void setCcList(String ccList) {
    this.ccList = ccList;
}
public String getBccList() {
    return bccList;
}
public void setBccList(String bccList) {
    this.bccList = bccList;
}



public String getFile() {
    return file;
}
public void setFile(String file) {
    debug("filename: " + file);
    this.file = file;
}
public void generateEmail() {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", this.getSmtpPort());
    if(isSslOn()) {
        props.put("mail.smtp.socketFactory.port", this.getSmtpPort());
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    }
    props.put("mail.smtp.host", getServerAddr());

    Session session = Session.getDefaultInstance(props, new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(getUsername(), getPassword());
        }
    });


    Message msg = new MimeMessage(session);
    try {
        msg.setFrom(new InternetAddress(this.getFromEmail()));
        if (getToEmails() != null) {
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(getToEmails()));
        } else if (isBccMode()) {
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(getFromEmail()));
        }

        //msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(getCcList()));
        msg.setSubject(getSubject());
        //msg.setText(getMessage());
        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText(getMessage());

        /*
        MimeBodyPart attachments = new MimeBodyPart();
        FileDataSource fd = new FileDataSource(getFile());
        attachments.setDataHandler(new DataHandler(fd));
        attachments.setFileName(fd.getName());
        */

        Multipart mp = new MimeMultipart();
        mp.addBodyPart(messagePart);
        //mp.addBodyPart(attachments);

        msg.setContent(mp);
        Transport.send(msg);
        debug("Done. Closing Session...");

    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private static void debug(String message) {
    System.out.println("[DEBUG]: " + message);
}
4

2 回答 2

1

我认为您的解析代码没有明显问题。我希望它在尝试解析该generateEmail()方法时在某个地方失败。由于解析器遵循顺序方法,因此该debug()方法也不会被解析。尝试将语句包含public boolean visit(MethodDeclaration method)在一个可能带有 Throwable 子句的 try-catch 块中。

还要检查您的readFile()方法。读取文件时最常见的一个问题是缺少将换行符附加到每一行。不添加新行会导致代码的错误构造,尤其是当代码中有注释时。您可以检查compilationUnit.getProblems()方法来检查任何此类问题。

于 2013-01-14T06:10:55.037 回答
0

@UnniKris - 感谢您的回复。我更改了readFile()方法并\n在换行符写入 StringBuilder 之后包含了一个。这行得通。我所有的方法都被成功解析了。

我的 readFile() 方法的代码片段发布在这里:

public static String readFile(File file) {
    StringBuilder sb = new StringBuilder();
    try {
        Scanner scan = new Scanner(file);

        while(scan.hasNext()) {
            sb.append(scan.nextLine()+"\n"); //added the new line feed here
        }
        scan.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String fullcode = sb.toString();
    //debug("full.code", fullcode);
    return fullcode;

}
于 2013-01-14T15:43:57.557 回答