ABTeacher and ABStudent is child class of ABUser
And in applyStudentChange I have Collection<ABStudent> ABStudents
I want to call changeABUser(Collection<ABUser> ABUsers) in applyStudentChange but the collection I have got is a collection of child class, how should I call changeABUser(Collection<ABUser> ABUsers) ?
I tried call this waychangeABUser((Collection<ABUser>) ABStudents), and this cast does not seem to work...
You need to give the following member type for your collection:
.... changeABUser(Collection<? extends ABUser> users) ....
Related
I have an abstract Scala class as a Mongo collection.
#Entity("aclTemplate")
abstract class AclTemplate(#(Id#field) var id: String) extends Serializable
Another class extends the above
#Entity("aclTemplate")
class GroupACLTemplate(id: String, var groupRoleAccess: Set[GroupRoleAccess]) extends AclTemplate(id) with Serializable
There are some docs of GroupACLTemplate in the collection. I am trying a simple query
createQuery().disableValidation().field("groupRoleAccess.groupId").equal(groupId).asList();
This throws a ValidationException
org.mongodb.morphia.query.ValidationException: The field 'groupRoleAccess.groupId' could not be found in 'com.model.acl.AclTemplate'
I do not think it is because of the long standing polymorphism issue in morphia. Because when I try to access just groupRoleAccess, it is able to. However, it is not able to access inside that set. It is a normal Java set. This is the GroupRoleAccess class
class GroupRoleAccess(var groupId: String, var roleId: String) extends Serializable
Am I missing something here?
I managed to hack something up. Apparently, since the collection is an abstract class, Mongo/Morphia does not look for attributes that are present in its subclasses. So I used createQuery and passed the class of the subclass.
ds.createQuery(clazz).disableValidation().field("groupRoleAccess.groupId").equal(groupId).asList();
But I still wonder how it was able to extract groupRoleAccess before
You should try 1.3.0-SNAPSHOT. I just fixed a bug similar to this and it probably fixes your issue, too.
I have class canonical name like this dev.ashish.mvc.beans.Employee.
Using this how can i create Class Employee at runtime in order access data members and member functions of Employee.
At runtime i want create class using its canonical name. At times it can be any entity Employee,Customer,User etc.
I tried this :
Class entityClass = Class.forName("dev.ashish.mvc.beans.Employee");
the above code does return class if i do entityClass.getName() it does return me dev.ashish.mvc.beans.Employee but how can i access methods of class Employee .
If i use java reflection like below :
Field field [] = entityClass.getClass().getDeclaredFields();
it returns me declared fields of class java.lang.Class instead of dev.ashish.mvc.beans.Employee
How can i achieve this ???
You already have your class in entityClass, so calling entityClass.getClass() will give you java.lang.Class and entityClass.getClass().getDeclaredFields() will indeed give you methods of Class not of your particular class.
You need:
Field field [] = entityClass.getDeclaredFields();
When you did:
Class entityClass = Class.forName("dev.ashish.mvc.beans.Employee");
you just got class of Employee. Now you have to create instance of it:
Employee employee = (Employee) entityClass.newInstance();
Update:
My answer is wrong. I though you need to work with methods of instance. But you need to access class methods.
You can call a method by name thus (adding to what has already said by others)
Method methodToCall = cls.getMethod("method_name");//cls is of type Class (e.g Employee.class)
methodToCall.invoke(obj, args);//obj is of type Employee in your case
Note that cls is of type Class and you already have to have an instance of the class that you want to call a method on. Note that even if you're calling a method without arguments you still have to pass a class instance (obj in this example), which means that the first argument is always a class instance.
I have 2 class just call it "Stuff" and "Customer", the classes based on my database table (JDBC) and have abstract class because this 2 classes has same few property(Id,Name), my abstract class containing(Id,Name, along with setter and getter from Id and Name variable).
I was creating 2 more class ("ExecuteStuff" and "ExecuteCustomer") which has a goal to execute a query for manipulate a data in my database,because this situation "ExecuteStuff and ExecuteCustomer" class should have method insert, update,delete and show for manipulate and showing a data from my database, because "ExecuteStuff" and "ExecuteCustomer" need a same method for process a data from my database , I decided to creating my own interface called "myData" which is contain 4 mehod (insertData(), updateData(),deleteData() and showData()) for class "ExecuteStuff" and class "ExecuteCustomer".
My problem is, what type data should I use for parameter inside a method in my interface "myData", for example = public int insertData(Stuff stuff); this method will work for "ExecuteStuff" but not for "ExecuteCustomer" because "ExecuteStuff" and "ExecuteCustomer" has a different object type.
Or a graceful way to solve this problem.
If I understand you correctly, you can use a generic type in your interface. That way it won't matter what the data type of the parameter you pass in is.
Here is a link that explains generics:
https://docs.oracle.com/javase/tutorial/java/generics/types.html
Another solution is to use:
public int insertData(Object obj);
Since both Stuff and Customer are objects.
Hope I was able to help!
You could use a generic parameter:
interface MyData {
public <T> T insertData(T data);
}
Class example:
class MyCustomClassThat implements MyData{
#Override
public <T> T insertData(T data) {
return data;
}
}
This makes the insertData method accept any class. Then you can operate on it however you like. Finally, we return the object originally presented; just in case you operated on the data object itself.
I have a SuperClass Employee and a Subclass Manager extends Employee with tribute Name.
Now I want to use instanceof while I go over an array of Employee
Example:
while (employee[i]!=null){
if (employee[i] instanceof Manager)
here is my problem"!!
I want to sysout a Manager atribute "Name":
sysout("Name: "+employee[i].name)
but it says create name in Employee.. why if it extends employee and already used instanceof...I tried Casting like this (Manager)employee[i].name but it doesn't do anything.
The cast needs to be applied to the value on which you are accessing the field.
((Manager)employee[i]).name
You were using it like
(Manager)employee[i].name
which attempted to apply the cast to the value returned by accessing the field name.
I'm writing a servlet-filter as the solution of this question:
Is it a good idea to filter inside a JSF template?
now, the idea is to create a big filter to check all privilegies and give the access or not to a certain user.
I create a Map to contains all privilegies for all sub applications and it has the sub application's id (a Long value) as Key and for the value another Map that contains other important informations.
The controller classes are named class1Controller, class2Controller ecc and the subapplications are stored in many folder named class1, class2 ecc...
The last thing that I must say is that all classes have a parameter called applicationID that is the same key of the Map that I mentioned previously.
So, what I would do?
I can retrieve the subapplication visited by the user using getRequestURI() method from HttpServletRequest, the problem is that I should take the application id from the class linked to that application, so I wrote this code:
Long id= ((Class.forName(packageName+applicationName+"Controller"))session.getAttribute(applicationName+"Controller")).getApplicationId();
The problem is that the compiler returns that it can't find method getApplicationId()!
Can I do something to resolve this problem? Or I must find another way to do that?
The last thing that I must say is that all classes have a parameter called applicationID
It sounds like you want an interface with the getApplicationId method in; make all the controllers implement that interface, and then all you need to do is cast to that interface.
// TODO: Work out a better interface name than "Application" :)
Object attribute = session.getAttribute(applicationName+"Controller");
Long id = ((Application) attribute).getApplicationId();
(You might want to use an abstract base class as described by BalusC - they're variations on the same theme, really.)
You're calling getApplicationId() on a Class instance, but it does not have that method at all.
The normal approach is to let all those classes extend some common base abstract class or an interface which has the method definied and then cast to that base abstract class or interface instead.
E.g. with a base abstract class:
public class FooController extends BaseController {}
public class BarController extends BaseController {}
etc..
Where the base abstract class look like this:
public abstract class BaseController {
public Long getApplicationId() {
return applicationId;
}
}
Then you can get it as follows:
Long id = ((BaseController) session.getAttribute(applicationName + "Controller")).getApplicationId();