I m试图读一个java案卷,在册子、阶级和方法名称上显示。 例如:
文件:测试。
package tspec.test;
public class Test {
public void addTest () {}
public void deleteTest () {}
}
产出:
package name: tspec.test
class name: Test
method name:
addTest
deleteTest
提前感谢:
I m试图读一个java案卷,在册子、阶级和方法名称上显示。 例如:
文件:测试。
package tspec.test;
public class Test {
public void addTest () {}
public void deleteTest () {}
}
产出:
package name: tspec.test
class name: Test
method name:
addTest
deleteTest
提前感谢:
可通过Java Compiler AP(在 Java6中推出)。 不幸的是,这一解决办法仅限于Sun s JDK。 因此,你必须安装和<>>>>>> 。
public void displayInformation(File javaSourceFile) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
// The file manager locates your Java source file for the compiler. Null arguments indicate I am comfortable with its default behavior.
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
// These will be parsed by the compiler
Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjects(javaSourceFile);
// Creates a new compilation task. This doesn t actually start the compilation process.
// Null arguments indicate I am comfortable with its default behavior.
CompilationTask task = compiler.getTask(null, null, null, null, null, fileObjects);
// Cast to the Sun-specific CompilationTask.
com.sun.tools.javac.api.JavacTaskImpl javacTask = (com.sun.tools.javac.api.JavacTaskImpl) task;
// The Sun-specific JavacTaskImpl can parse the source file without compiling it, returning
// one CompilationUnitTree for each JavaFileObject given to the compiler.getTask call (only one in our case).
Iterable<? extends CompilationUnitTree> trees = javacTask.parse();
CompilationUnitTree tree = trees.iterator().next();
// Create a class that implements the com.sun.source.tree.TreeVisitor interface.
// The com.sun.source.util.TreeScanner is a good choice because it already implements most of the logic.
// We just override the methods we re interested in.
class MyTreeVisitor extends TreeScanner<Void, Void> {
@Override
public Void visitClass(ClassTree classTree, Void p) {
System.out.println("class name: " + classTree.getSimpleName());
System.out.println("method name:");
return super.visitClass(classTree, p);
}
@Override
public Void visitMethod(MethodTree methodTree, Void p) {
System.out.println(methodTree.getName());
return super.visitMethod(methodTree, p);
}
}
tree.accept(new MyTreeVisitor(), null);
}
当我通过该方法时,我收到这一产出:
class name: Test method name: addTest deleteTest
不幸的是,我还没有看到包裹名称存放地。
其目的是迅速颁布 Java法,并报告其内容。 反省可做如下事情:
Class.forName(className).getDeclaredMethods();
这两种解决办法都不需要第三方图书馆或工具。
唯一困难的是,《java法典》可能没有很好的格式。 如同职能声明一样,可以分多个渠道进行。
最终解决办法是建立一个自动渠道,首先验证源代码,然后采用一些汇编技术,以 gr清你想要的数据。
We use PMD Java code analyzer to solve similar problem. It is useful.
不要把 Java案合为一! Java已经包含获取关于自身类别、方法和包裹的信息的途径:它称作。
查阅java.lang.Class 班级。 这一类别的每一例都代表了特定的 Java类,并载有返还阶级名称的方法、它所生活的包裹、它所包含的方法,以及更多的信息。
http://java.sun.com/javase/6/docs/api/java/lang/reflect/ Package-summary.html 自<代码>某些方法以来的整套办法 这批人的返回类型:。 一揽子计划包括代表方法、类型、领域等内容的课程。
查阅<条码> 《
Class<?> testclass = Class.forName("tspec.test.Test");
这种做法使一类身份不明的人群返回,即,如果你不熟悉通用物,则在角方括号内出现问题。 为什么不了解班级的类型,是因为你将班级名称与停业时间的拼凑在一起。 在编纂时, Java不能肯定地说,为Name而通过的法令甚至代表了有效的类别。
但是,以上定义的<条形状>测试条码>将因获得阶级名称、方法及含有包装材料而被罚款。
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...