Cannot find symbol '/' in class name - java

When I compile my java class. There are some errors of cannot find symble '/' in class name. Below is a sample code from my class:
public TransactionSearchResponse submit(TxnSearchRequest req)
{
url = (new StringBuilder(String.valueOf(req.getBaseUrl()))).append("/txns/search").toString();
method = "POST";
return (TransactionSearchResponse)sendRequest(req, com/COMPPONENT/api/TransactionSearchResponse);
}
Be cause of copyright from author of this code block. sendRequest Method is deleted.
Netbeans cannot recognize the dash '/' in the class name "com/COMPPONENT/api/TxnResp". And the class name contains some parts:
Package name: com.COMPONENT.api
Class name: TxnResp
Java file name: TxnResp.java
The dash '/' show in red color as Netbeans mask it an error line. The only hint I got from Netbeans are "Add import for com.COMPONENT.api.TxnResp" or "Flip Operands of '/' (may alter semantics), and I did that but got no luck. And when I try to run the code, it generate an error of "Cannot find symbol". Can you help me to solve this issue?
Regards,
Dung Tri

If a method sendRequest is declared like
Object sendRequest( Request x, Class<?> y )
you'll have to call it with an instance of a java.lang.Class object:
... = sendRequest( request, com.COMPPONENT.api.TxnResp.class );
Appending .class is the way of obtaining an instance of a certain Class object (not to be confused with an instance of TxnResp which is created using new TxnResp).
Also, given
com.COMPPONENT.api.TxnResp txnResp = new com.COMPPONENT.api.TxnResp();
the expression
txnResp.getClass()
results in an instance of the Class<com.COMPPONENT.api.TxnResp> but of course the .class notation is more convenient for your purpose.

Related

Error with Java String in declaration in intellij

I've updated my intellij to 2017.2.5 and imported all the projects. For some reason, declaration and constructors show some error with Strings. e.g. private String type = "abc"; and name = " xyz"; Error asks me to change it to java.lang.string as a solution.
Jdk is up to date. libraries are up to date, so what is the problem? Project structure looks ok. How do I fix this?
Just delete the line:
import org.apache.xpath.operations.String
This is causing a type conflict between java.lang.String (what you want) and an Apache class (which you probably don't want).

Get IMethod from the method name in java

I'm looking for a way to find the IMethod, given the method name as input for developing my eclipse plugin further.
Couldn't figure out a way to do so.
can someone please direct me in the right path.
There can be two approaches:
You can use the ASTVisitor pattern to visit the MethodDeclaration nodes, do a check for name and arguments, and get IMethod from them by resolving the binding. Refer the below posts:
Eclipse create CompilationUnit from .java file
How to convert AST to JDT Java model
Get the ITypes from the compilation unit and loop through the IMethods, do check for name and arguments to find the required one.
IType [] typeDeclarationList = unit.getTypes();
for (IType typeDeclaration : typeDeclarationList) {
// Get methods under each type declaration.
IMethod [] methodList = typeDeclaration.getMethods();
for (IMethod method : methodList) {
// Logic here.
}
}

Why is this an "illegal class name" in Groovy / Java? [duplicate]

I'm using the following code in post-build step of my Jenkins job:
evaluate(new File("Set-BuildBadge.groovy"));
So it runs a script successfully if it does not contain functions.
If inside the script I define a function for example:
def addSummaryWithText(Icon, Text) {
manager.createSummary(Icon).appendText(Text, false)
}
...
addSummaryWithText("installer.gif", "Project: " + ProjectName)
then I get the following error:
FATAL: Illegal class name "Set-BuildBadge$addSummaryWithText" in class
file Set-BuildBadge$addSummaryWithText java.lang.ClassFormatError:
Illegal class name "Set-BuildBadge$addSummaryWithText" in class file
Set-BuildBadge$addSummaryWithText at
java.lang.ClassLoader.defineClass1(Native Method) ...
I'm not getting how GroovyShell.evaluate works.
Can anyone help me?
Looks like the JVM doesn't like class names with a hyphen in them.
By calling your script Set-BuildBadge.groovy internally it is compiled into a class that isn't allowed when you add a function to the script.
Changing the name of the script to SetBuildBadge.groovy will fix it :-)

Yaml fixtures of java playframework doesn't work as expected

I tried to make the zenTasks tutorial for the play-java framework (I use the current playframework, which is 2.3.2). As it comes to testing and adding fixtures I'm kind of lost!
The docu states that
Edit the conf/test-data.yml file and start to describe a User:
- !!models.User
email: bob#gmail.com
name: Bob
password: secret
...
And I should download a sample (which is in fact a dead link!)
So I tried myself adding more Users like this:
- !!models.User
email: somemail1#example.com
loginName: test1
- !!models.User
email: somemail2#example.com
loginName: test2
If I then try to load it via
Object load = Yaml.load("test-data.yml");
if (load instanceof List){
List list = (List)load;
Ebean.save(list);
} else {
Ebean.save(load);
}
I get the following Exception:
[error] Test ModelsTest.createAndRetrieveUser failed:
java.lang.IllegalArgumentException: This bean is of type [class
java.util.ArrayList] is not enhanced?, took 6.505 sec [error] at
com.avaje.ebeaninternal.server.persist.DefaultPersister.saveRecurse(DefaultPersister.java:270)
[error] at
com.avaje.ebeaninternal.server.persist.DefaultPersister.save(DefaultPersister.java:244)
[error] at
com.avaje.ebeaninternal.server.core.DefaultServer.save(DefaultServer.java:1610)
[error] at
com.avaje.ebeaninternal.server.core.DefaultServer.save(DefaultServer.java:1600)
[error] at com.avaje.ebean.Ebean.save(Ebean.java:453) [error]
at ModelsTest.createAndRetrieveUser(ModelsTest.java:18) [error]
...
How Am I supposed to load more than one User (or whatever object I wish) and parse them without exception?
In Ebean class save method is overloaded.
save(Object) - expects parameter which is entity (extends Model, has #Entity annotation)
save(Collection) - expects collection of entities.
Yaml.load function returns objecs which can be:
Entity
List of entities
But if we simply do:
Object load = Yaml.load("test-data.yml");
Ebean.save(load);
then save(Object) method will be called. This is because at compile time compiler doesn't know what exactly will Yaml.load return. So above code will throw exception posted is question when there is more then one user in "test-data.yml" file.
But when we cast the result to List as in code provided by OP then everything works good. save(Collection) method is called and all entities are saved correctly. So the code from question is correct.
I have same problem with loading data from "test-data.yml". But I have found solution for this problem. Here is http://kewool.com/2013/07/bugs-in-play-framework-version-2-1-1-tutorial-fixtures/ solution code. But all Ebean.save methods must be replaced with Ebean.saveAll methods.

Get unresolved imports

I'm writing eclipse plugin that looks for unresolved imports in all source files.
I found that it can be helpful to use IProblem or IMarker objects. Here's code example
public IMarker[] findJavaProblemMarkers(ICompilationUnit cu)
throws CoreException {
IResource javaSourceFile = cu.getUnderlyingResource();
IMarker[] markers =
javaSourceFile.findMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER,
true, IResource.DEPTH_INFINITE);
}
frome here
I don't know how I can get info from IProblem or IMarker about which import cause the compilation problem (unresolved import).
Any help?
http://help.eclipse.org/indigo/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_classpath.htm
There are a list of different int values in the IProblem interface representing different errors; if you could get the errorcodes of a file somehow, you could use them. (Example, ImportNotVisible, ImportNotFound, etc.). Just check if the error ID matches one of the error ID's for import failures there.
An IMarker knows the line number and start and stop chars for the java source marked by the IMarker. You can take the substring of the java source string and, if the marker type indicates that it's a problem with the class or import, you can search the project's classpath for a class or package matching (or similar to) that substring.

Categories

Resources