×

Loading...
Ad by
Ad by

我的两点建议

我不是个好的设计者,因为我总喜欢把简单问题复杂化,不过你的project实在不怎么简单,所以希望我的建议有点用。

我建议你使用两项很有用的技术:
基于JSR-000094的Java Rule Engine API
和基于JSR-000175的JAVA Metadata Facility

基于Rule Engine你可以使系统得到灵活的扩展,
所有的错误可能都以Rule的形式出现,比如:
if line start with int, char, Integer.... then mark here can cause a defination mistake
if line contain { then mark here can cause a bracket not match mistake
诸如此类,只要定义好Rule Engin,以后想添加一种错误,就添加一条Rule就可以了。

通过Metadata,你可以使用Tag,来定义要执行的操作,实现你所提到的Template里面的标签。
例如:@bugtype1here{... code to remove a semicolon ...}
这样用Rule来产生Meta-Tag,再通过Meta-Tag来执行对代码的修改,基本就满足你的需要了。

另外,有一个工具叫做ANTLR,可以进行代码分析,我只是听说,没有用过,你可以试试。
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 求教java问题:今天老师给出了个题目,想请教大家,提示点思路,挺急的,先多谢了。题目有点长,大意如下:
    本文发表在 rolia.net 枫下论坛-开发一个用于教学的training program,主要目的是练习java code debugging。input是一个.java文件,也就是一段java code。output是faulty code。最终目的是将产生的faulty code打印给学生,看他们是不是可以找出所有的bug。此程序的未来用户是prof或ta。
    -首先需要设计工具A将input java code改写为一个bug template,也就是加入各种bug标志的not pure java code,template里面并没有bug,而是会有些类似◎bugtype1here,◎bugtype2here这样的标志,标志在哪里可以加入什么样的bug。然后将template存放到template library中,供用户以后选用。
    -需要设计工具B,当用户选择一个template,然后选择一些bug类型,就可以自动产生faulty code。这个faulty code是一个有bug的java code。

    问题:
    -好像首先需要知道java code都有哪些bug类型,prof只给举了个例子,比如:incorrect loop,undefined variable,或者take one statement out。哪里可以找到关于各种可能的java code bug类型的信息?权威些,全些最好啦。
    -设计工具A的时候,要能读java code然后自动产生template,有什么思路或者现成的工具可以帮助实现‘读code、识别可以加入bug的地方、写template’这些任务?关于这一点我现在一点思路也没有,请多帮忙出出主意。更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • I think the hardest part is how to do this programmatically, I only know how to do it manually, I do this everyday
      • agree. Prof also said that the hardest part is how to generate template automatically. But no idea about this till now.
    • Creat your own exception extends the exception. throw it when you think there is an error. catch it at the end.
      • ?
    • why not use the JNI in your java code or Apache ant
      1) maybe you can use the JNI in your java code to invoke the java compiler then check the compiler out put for those error...
      2) Better to use Apache Ant for those...
      • 多谢回复,可能是我的问题没说清楚。我是要把一个没有bug的.java source code变成有bug的faulty code,关键是如何制造faulty code automatically。而不是要debug。
    • My 2 cents
      本文发表在 rolia.net 枫下论坛Sorry , I didn't really get a chance to think it over. The followings are just some ideas off the top of my head.

      1.The ideal is somehow to leverage the Java compiler.

      2. If 1. is not doable, you may have to implement the compiling rules yourself. This is huge work, but may not that difficult as you thought at first glance because you are not going to analyze the code according to the compiling rules and search for any probable syntax errors. You are creating some specific errors.

      3. You program may behave like this:
      A user wants to plant a "CanNotResolveSymbol" error into your code. This error is usually about an undefined variable. so the user needs to highlight a variable and then click on item in an error list.Your program searchs where this variable is defined and removes it. This way you get a Java code with the error "Cannot resolve symbol".

      This may be the simplest example, not all errors are that straightforward.
      You might be having hard time with them.

      In the end, I would suggest you doing more research on Internet and see what you can find before you really start work if you have enough time. Because in my solution, you never know how many cases you may have missed.

      Good luck!更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • Great thanks! I just began the research on 'java static code checking/testing' topic. I found an open source tool named PMD on internet. It works like a compiler with its own bug classification & definition.
        Like what you said, what I'm trying to do should be easier than that, coz I only need to ADD bug, rather than find it.
        I'll take some time to study PMD's bug classification & definition. And read his source code to get more idea to create a simplified 'compiler'.
        Also I found there are several similar tools on internet. But don't know which one has better reputation. So have to spend time on each to see what I could get from them.
        Thanks again, fatbean! You know, sometimes a developer just want to discuss with others to get sparkle idea, coding alone is suffering. Thanks for taking my question seriously. any idea or suggestion are welcomed anytime!
        • CodeDom on Java?
          我只懂 C#,不过对这个题目挺好奇的,所以 google 了一下。在 .NET 平台上,为了做到语言无关性,有一个 class 叫 CodeDom,飞快地看了一下,大致上是把 Code Document Object Model 转换成 C# 或 VB 或 Java 的。如何先把 C# / VB.NET code parse 成 Code DOM 不是件容易的事,但网上也有人给出了 solution。蛮麻烦的。
          只是个思路 -- 不知道 java 平台上有没有类似的东东。
    • 我的两点建议
      我不是个好的设计者,因为我总喜欢把简单问题复杂化,不过你的project实在不怎么简单,所以希望我的建议有点用。

      我建议你使用两项很有用的技术:
      基于JSR-000094的Java Rule Engine API
      和基于JSR-000175的JAVA Metadata Facility

      基于Rule Engine你可以使系统得到灵活的扩展,
      所有的错误可能都以Rule的形式出现,比如:
      if line start with int, char, Integer.... then mark here can cause a defination mistake
      if line contain { then mark here can cause a bracket not match mistake
      诸如此类,只要定义好Rule Engin,以后想添加一种错误,就添加一条Rule就可以了。

      通过Metadata,你可以使用Tag,来定义要执行的操作,实现你所提到的Template里面的标签。
      例如:@bugtype1here{... code to remove a semicolon ...}
      这样用Rule来产生Meta-Tag,再通过Meta-Tag来执行对代码的修改,基本就满足你的需要了。

      另外,有一个工具叫做ANTLR,可以进行代码分析,我只是听说,没有用过,你可以试试。
      • Cool! They're exactly what I want! Will search more info on internet soon. Great thanks!