Java - Multiple Package In Eclipse Issue - java

I have the following set of packages within my source folder.
The packages are shapes,model,views.
Say I have a class file in my model folder that has the following piece of code:
shapes.interfaceforshapes[][] temp = model.get2dshapearray();
if(temp[x][y].getClass().isInstance(shapes.cTriangle)){
}
Please note in the above code temp[x][y] will return a class that interfaces my shapeInterface
and all classes within the shapes folder interface this.
Am I doing the correct thing to say "Is the class within my array of type cTriangle"?
I currently get the error:
shapes.cTriangle cannot be resolved to a variable
but I don't want to match a variable, I want to test it agaisnt the class cTriangle within my package shape.
Thanks

Use instanceOf operator if you want to check if the object is an instance of a certain class, while the isInstance method expects an instance of a class.
if( temp[x][y] instanceOf shapes.cTriangle) {//dosomething }

That is not how isInstance(Object) works. You have to call it on a class and pass in the object you want to match. You would do:
shapes.cTriangle.class.isInstance(temp[x][y]);
assuming cTriangle is a class and temp[x][y] returns an object and you want to check if that object is of type cTriangle.

Related

Store class type as a variable to identify next expected object type in list in java

I'm working on a project where I obtain a list of strings and need to parse them into objects of different types, for example I have Command, FloatArgument and IntegerArgument as 3 different classes. For a given input like "fetch 5 3.45" I need to see if it is correct and then get the arguments. For this I decided to ceate a sort of library where all commands can be stored in a hashlist
baseCommands = new HashMap<String, Command>();
In the command class I store basic information I need for each command and I'd also like to include the next expected token as for now I'm pretty sure for each command there is only going to be 1 type of token which can follow any command/argument. Therefor I created a Class field inside the command class as well as the argument classes which stores this information:
public class Command {
public int identifier;
public Class next;
public boolean isLast;
public Command(int identifier, Class next, boolean isLast) {
//add the stuff neccesary to make general commands. (except string name).
this.identifier = identifier;
this.next = next;
this.isLast = isLast;
}
}
Normally, as I understand it, if you would use an abstract class for storing different class types if they are all similar. For example an abstract class Animal is extended by Dog, Cat, etc. However in this case I have 2 separate things: Commands and Arguments. I feel like having an empty abstract class might not be the most elegant solution here, but I could be wrong.
As I understand it the Class field is used to store class types. This would be ideal so that later I can see for a given token in an input if the next token after it matches the expected one by simply doing the following:
token.type instanceof lastToken.next
However once I try to put information into the hashmap like this:
baseCommands.put("execute", new Command(1, Command, false));
I get the following simple error:
Command cannot be resolved to a variable
Now I know that what it's trying to do is to pass a variable that doesn't exist instead of the class type like I'm trying to do. How can I pass the class Command as a type instead? Will I be able to use the above mentioned method of checking if the next token in the input is correct? Is this a correct way of doing this or should I create another abstract class for both commands and arguments?
Thank you for your input.
You have to refer to Command.class:
baseCommands.put("execute", new Command(1, Command.class, false));

Possible to get classpath of Type object? (e.g com.test.xyz.CLASSNAME)

I've been searching around, but I have not found what I am looking for. Essentially, what I am trying to do is get the class path of a Java object in the form
com.path.xyz.CLASSNAME
. Is this possible to do.
I currently have a object as follows:
List<Objects> x;
for(Object t: x){
getPath(t)
}
You can use t.getClass().getCanonicalName() to get the full class name...

Lookup.getDefault().lookup() returns null

What should be done if a lookup returns null? I am using Lookup.getDefault().lookup() of org.openide.util.Lookup which is used for finding instances of objects. The general pattern is to pass a Class object and get back an instance of that class or null.
I am using it in the code below:
StreamingServer server = Lookup.getDefault().lookup(StreamingServer.class);
ServerControllerFactory controllerFactory = Lookup.getDefault().lookup(ServerControllerFactory.class);
StreamingController controller = Lookup.getDefault().lookup(StreamingController.class);
Since no istances are found, null is returned for each of these, 'server', 'controllerFactory', 'controller'. I need to handle this situation so I can use these objects to access methods, as in :
controllerFactory.createServerController(graph)
server.register(serverController, context)
How can I accomplish this?
You need to create a META-INF/services folder in your Eclipse source folder. Then, for each class, create a text file in the META-INF/services folder. The name of the text file is the full name (includes the package location) of the class type, and the contents of the text file is the full name (includes the package location) of the implementing class (which is not necessarily the same name of the class).
For example, for the ServerControllerFactory class, you will create a text file named org.gephi.streaming.server.ServerControllerFactory and the text this file will contain is org.gephi.streaming.server.impl.ServerControllerFactory
For me, the class that I was trying to call lookup on just needed the #ServiceProvider decoration like this:
#ServiceProvider(service = MyClassHere.class)
public final class MyClassHere ....

Use Constants interface in GWT Project

I have not yet used Constants interface in GWT and I am having problem to run the example CellTable. The deferred binding fails and the central error message is: "No resource found for contactDataBaseCategories". contactDataBaseCategories is a method defined in the interface DataBaseConstants and returns an array of Strings. I suspect I must create a properties (txt?) file and to define the categories, but I am not sure, since I come across this case for the first time. How can I do it properly to make the example of the CellTable run?
Update: I have created the ContactDatabase.DatabaseConstants.properties file in the same package in which the interface is declared, I have added the line in the file:
contactDataBaseCategories = friends, coWorkers, other
but it still does not work. The error is again : "No resource found for contactDataBaseCategories" and then
"Deferred Binding failed for com.al.celltablöeexample.ContactDatabase.DatabaseConstants".
What going wrong?
This is how i do it
Constant interface
public interface DataBaseConstants extends Constants
{
#Key("contact-database-categories")
String contactDataBaseCategories();
}
property file. DataBaseConstants.properties
contact-database-categories = "Your String"
You can use it
public DataBaseConstants dbConstant= GWT.create( DataBaseConstants .class );
dbConstant.contactDataBaseCategories();
Edited
If you want to pass string array then you can do it like this
#DefaultStringArrayValue({"cat1", "cat2", "cat3", "cat4", "cat5"})
String[] contactDataBaseCategories();
More about Constants
I have finally managed it. The problem was that it could not find the resource/file: ContactDatabase.DatabaseConstants.properties. I have changed it to DatabaseConstants.properties and I removed the inner interface to its own file. The same I did in the class CwCellTable on the interface CwConstants. In the example page moreover, the instantiation of the CwConstants interface is missed, and one must do also this (in the constructor), like in the ContactDatabase class.
Just to add to Dilantha's answer you can then set
contact-database-categories = Family, Friends, Coworkers, Businesses, Contacts
in order to comply with the example.
Tip : In order to make the example work create a costructor in CwCellList and add the following :
initWidget(onInitialize());

How to invoke methods of a class (in java) when class name and method name are stored in two different strings

Well, not sure if the question sounds a little weird but let me try to put forth the clarification :
I have a JSP page. On this JSP page, I am calling a java class defined in one of my packages under my projects. This class connects to database and access a table which has got fields namely - functionname, function class. Now I am able to retrieve in my JSP the two strings, lets say -
String funName = "ComFunctions";
String className = "funLog");
Now, I want to invoke this function using this class name i.e. basically something like - className.funName
Is it possible in Java? Actually, these functions and class names will be retrieved in a for loop, so I can't directly call using real classname but have to use strings.
Kindly suggest if there is a way or worl around or if the question is still unclear.
I tried the following approach so far but no luck -
Class c = Class.forName(className);
Object o = c.newInstance();
Method m = c.getMethod(funName, String.class); // Not sure what is supposed to be second parameter here i.e. after funName
Error - the above code gives " No class found error". And i made sure that class is there under the package. Even adding package name i.e. packge.classname didnt help and it says "Symbol not found" for package name.
Any pointers please?
Example class that I am trying to invoke -
package mypackage;
public class ComFunctions extends WDriverInitialize{
public static void main(String[] args){
}
public static void funLog(String username){
System.out.println(userName);
}
}
You need to make sure the compiled class is in the webapp's classpath (ie, WEB-INF/classes) and use the FQN (ie, add the package name). You could also make a JAR file of your classes and add that to the WEB-INF/lib folder.
Also, the extra parameter in getMethod is to fetch a method with the matching parameters (ie, in your example, one that takes a String
You're missing one piece of the puzzle, and that's the method arguments. Without it, you can't really be sure what method funName is referring to, and what arguments to pass to it.
And of course, the class needs to be in the classpath.

Categories

Resources