ASTParser.setSource has polymorphic methods with different input types.
However, when I use ICompilationUnit as an input to setSource method,
I got an error saying I didn't use char[] as an input parameter.
Why this error?
ADDED
I use eclipse jdt/ast as a part of stand alone java program. In the course of doing that, I don't use eclipse project/resources, but load the java source into char[] as a parameter for setSource(). I'm not sure, but the eclipse may recognize that it's not run as a plugin to block the usage of other polymorphic methods.
check whether you have specified the kind as compilation unit.
// Parse the class as a compilation unit.
parser.setKind(ASTParser.K_COMPILATION_UNIT);
I was trying to execute the jdt/ast as a library for stand-alone java program. When I rerun them in plugin context, everything works fine.
Related
I have a Java class which uses a .jar file.
Every time that I want to compile this class, I have to do something like javac -cp ".:myJar.jar" myClass.java and every time that I want to execute it, I have to do the similar thing but with java instead of javac.
Is there a way to avoid doing this?
I know that I could put this jar file into my class path but I don't want to do that. I don't neither want to do a maven projet.
There is nothing preventing you from using an IDE, taking advantage of all its super useful features when developing the program, and then submit just the source code and associated jars to the professor.
Advantages of using IDE that your professor don't need:
Syntax color-coded editors with auto-complete.
Built-in display of javadoc, so you know what all the built-in Java methods do, and how they work.
Instant high-lighting of syntax errors.
Never having to compile the code, since IDE always keeps the code compiled.
Easy single-button execution of the program when you want to run it.
Debugger. Very important feature when your code is not working as you intended.
... more ...
I'm digging into the source code of the deeplearning for java recently. There is such a class NeuralNetConfiguration in which there are tons of fields that all requires getters and setters. The NeuralNetConfiguration.java source code does not provide any, however.
When I open this project in IntelliJ, ctrl click on the usage of this class, which are methods mostly like, NeuralNetConfiguration.getNInput() or NeuralNetConfiguration.getKernelSize(), the IDE direct me to the compiled class file in which all the getters are defined for each of the field in this class.
Just wonder how this is done since I'm a new bee to java. Posts I found about java reflect suggest that reflect can not add method to a method to a class unless you wrote your own classloader. I check the deep learning for java project and I don't think they have done that.
What bothers me too from time to time is, IntelliJ starts to report errors that those getFields methods could not be resolved since they are not in the source file at all, especially after my building the project using IntelliJ instead of using mvn command line.
The magic happens with the #Data annotation on the class. This annotation is from Project Lombok. There is probably an annotation processor somewhere that hooks into the compiling process and generates these methods.
Eclipse has an option in Java Compiler tab:
Store information about method parameters (usable via reflection)
If I checked on the option. I am able to get method parameter via Parameter API. It is good.
If I turn on the option, Compiler will stored method parameter information in All my compiling classes. This may make the class/jar file bigger. Eclipse turn it off by default.
My question is: Is there any way to turn on the option for some class I want? Is there any compiler directive that I can add it into my java class for this purpose?
Thank you!
Yes in a way. This is an option to javac (see -parameters) and javac can be run on whatever set of files you would like. However, there is not any option to selectively apply -parameters to certain classes when running javac on multiple files, so you would have to run multiple javac's most likely. This could be done through a build file most likely with a build language (for instance Ant or Gradle).
I am currently working with the AST of the JDT in order to be able to parse Java source code. I need to identify the type bindings of parameters. For example, for a String parameter, I would need to have java.lang.String and not simply String.
From some research I learnt that this may be done using resolveBinding but to do so one would need to set the environment using the setEnvironment method defined in the ASTParser. For some reason though, Eclipse is indicating a compile-time error saying that setEnvironment is undefined for ASTParser. I need the setEnvironment method because I do not have an IJavaProject available.
Can anyone please indicate what might be wrong?
Are you creating an Eclipse plug-in? If yes then you should be able to resolve bindings. Or maybe you are creating a standalone application using AST from Eclipse JDT? It is not clear. Also it would be useful if you could provide some code snippets where it fails.
I have developed some stand alone application and setEnvironment works fine using with ASTParser, here's a code snippet:
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
// you must set compilation unit name, so I just name it as a path to source file
parser.setUnitName(inputFilePath.toString());
//params classpathEntries, sourcepathEntries, encodings, IncludeRunningVMBootclasspath
parser.setEnvironment(null, null, null, true);
//fileContent is a char array of some source code
parser.setSource(fileContent);
parser.setResolveBindings(true);
CompilationUnit unit = (CompilationUnit) parser.createAST(null);
Afterwards having CompilationUnit you can traverse down the tree and find required nodes and use resolveTypeBinding() to get ITypeBinding object and then use getQualifiedName() to get a full name of a type, i.e. as you mentioned java.lang.String.
I was able to use the .setEnvironment method using the following jar file: org.eclipse.jdt.core-3.10.0.v20140604-1726.jar. In addition, I added this set of jar files in this link to my classPath.
This problem I faced while using maven repository of eclipse.jdt.core with version 3.7. Indeed this method is not supported in the previous versions. So using a newer version 3.10(+).x will be beneficial in this scope.
I have a text (.txt) file that contains Java code! I want to create a method that includes this Java code and then call that method through the program.
Can anybody suggest a way to do this?
let consider this example what it does actually load the source code, compile and execute the java code by simpler program by using JavaCompiler API.
Use the JavaCompiler. It can compile code from a String, so I'm sure it could handle code from a text file.
Do you think instead of putting it in the main method I can put it in for example test method and call method like this?
Put it wherever you like. E.G. see the STBC & especially the source code. It provides a GUI and can compile the code in the text area on button click.
this program need tools.jar but jre 7 doesnt have this!!
Did you try reading the documentation that is provided for the STBC? Notably:
System Requirements
STBC will run on any computer with a version 1.6+ Java Plug-In* JDK (AKA SDK).
(*) The API that STBC uses is merely a public interface to the compiler in the tools.jar that is distributed only with JDKs (though the 'public JRE' of the JDK also seems to acquire a tools.jar). This leads to some unusual requirements in running either the native jar, or the web start app.
Or shorter, no JRE will have a JavaCompiler, only JDKs have them.
Change the .txt file to a .java file,
add it to your java project
Compile the code
Execute the methods
Load the file in through standard java IO and then have Groovy evaluate it for you:
http://groovy.codehaus.org/Embedding+Groovy
it's something like quine:
http://www.nyx.org/%7Egthompso/quine.htm