setEnvironment method is undefined for the ASTParser class - java

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.

Related

Could java reflection add method into class file that are not in java source file?

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.

Unable to understand the following error: "(Ljava/lang/Integer;)V" is not applicable on this object

When I am getting the result from the DB2 and trying to set to this attribute noOfLocations I am getting the following error.
Method "setNoOfLocations" with signature "(Ljava/lang/Integer;)V" is not applicable on this object
Following code shows the problem.
I am using rs to set the value.
packDO.setNoOfLocations(rs.getInt("NO_LOC_PKG"));
and
rs.getInt("NO_LOC_PKG") is returning 0
and
NO_LOC_PKG is of datatype Integer in the DB
and noOfLocations type with setter method is,
private Integer noOfLocations;
public void setNoOfLocations(Integer noOfLocations) {
this.noOfLocations = noOfLocations;
}
The error message you posted suggests that it's a build-related issue, as compiler and runtime errors don't generally describe methods by their signatures.
Make sure that your build is up-to-date and that the source available to your debugger matches the source of the binary you are running.
Depending on your build system, issues could result from previous partial builds, unexpected timestamps on files, copying/moving files, a crashed IDE, a quirky build system, etc.
Autoboxing and unboxing is introduced in Java 1.5.
int getInt(int columnIndex)
throws SQLException
returns primitive.
So if you are using <1.5 version java you will run into this issue.
Otherwise mention you verision.
I've just encountered this and as other posters have described it refers to a class not being found at runtime. For me the reason is :
Within Eclipse I have a web app running on Tomcat. The web app refers to a seperate jar file that is also maintained within Eclipse. Within Eclipse everything is fine as the jar file is on the class path. However, when I attempt to use the web app which has a dependency on the jar file I receive this error. Tomcat is unable to find the jar file library. Fix : add relevant jar to build path of Tomcat.
Adding to what #sᴜʀᴇsʜ ᴀᴛᴛᴀ said, I think what you need is
packDO.setNoOfLocations( new Integer( rs.getInt("NO_LOC_PKG") ));
getInt() returns int, and setNoOfLocations() wants java.lang.Integer.
By the way, it has nothing to do with DB2.

Could not locate Clojure resource on classpath

I am trying to access some of my clojure functions in my Java code (I am using Eclipse and CounterClockWise). Firstly, I was not sure how to do it? But then I found some answer here.
I am using clojure.lang.RT package for calling .clj files in my Java code. While naming namespaces, .clj files proper conventions has been followed like ns test.clojure.core is in test.clojure package where-in core is .clj file. Also upon following some information, I have added java-source-path (i.e. java-source-path src/test/java) and source-path (i.e. source-path src/test/clojure) in my project.clj.
But, when I run my Java file following error is given - a) Could not locate Clojure resource on classpath b) Attempting to call unbound fn.
Please if anyone in community could help me (assuming I am absolute beginner) through detailed procedure or tutorial I would be grateful, as only information I could get (for using clojure.lang.RT) is very little.
Looking forward to get a complete answer to end my frustration.
I guess you are using RT.loadResourceScript which is causing the proble.
You can try out something like this:
//Load the namespace
RT.var("clojure.core","eval").invoke(RT.var("clojure.core","read-string").invoke("(use 'test.clojure.core)"));
//Find a function in namespace
IFn fn = (IFn)RT.var("test.clojure.core","hello");
//Call that function
fn.invoke();

Using ICompilationUnit for ASTParser.setSource() method in eclipse jdt/ast

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.

Setting up test inputs in eclipse

I have some methods that would require to execute over a java class.
For example my method receives as argument a class file, something like:
Information info = grabInformation("class_to_execute");
This method would run the "class_to_execute"and capture its output. And I would like to later assert its output with a given expected value.
My question is: how could I set up eclipse so that my test cases would find the classes that it will execute? Is adding the classes to the build path enough? Are there some variables I could set?
I don't think the CLASSPATH has anything to do with it.
If "class_to_execute" is in another project or JAR, then add it to your Build Path under Libraries. Do you have any reason to believe that's not enough? Build path == CLASSPATH for most purposes.
If you're having Build Path or CLASSPATH problems, it might be easier to debug if you do this:
Information info = grabInformation(class_to_execute.class);
If it can't find the class, then put your cursor on the error and type Control+1. Eclipse might be able to help you fix the Build Path automatically.

Categories