aspose file tools*
The moose likes Java in General and the fly likes Generating class methods at runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generating class methods at runtime" Watch "Generating class methods at runtime" New topic
Author

Generating class methods at runtime

Dalia Sultana
Ranch Hand

Joined: Jul 16, 2006
Posts: 42
Hi All,
I don't know if it is the right forum for my question, but..

I have a class like the following. I want to be able to give back the user the strings that was passed in in the string array when the object was constructed. For example say somewhere in the code I have


after constructing the object, I want the user class to have access to a methods like getModify_a(), getModify_b(), getModify_c() etc, as the user class has no idea about the stings that was passed in inside the array. These objects are static and constructed in a different class at start up.
Is this doable? or does anyone have a suggestion as to what can I do this to implement something like this?

Following is the class that I have currently.





[ May 13, 2008: Message edited by: Dalia Sultana ]

[ May 13, 2008: Message edited by: Dalia Sultana ]
[ May 13, 2008: Message edited by: Dalia Sultana ]
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
As far as I know this is not possible in java. You can not provide dynamic methods at runtime to an object. whats the whole point in having the class then?


Amit Tank
Linked In
Dalia Sultana
Ranch Hand

Joined: Jul 16, 2006
Posts: 42
Well, I need to instantiate many objects like this, like

ComplexPermission cp1 = new ComplexPermissionImpl("view_users", new String[]{ "modify_user_address","modify_user_name", "modify_user_account"});


ComplexPermission cp2 = new ComplexPermissionImpl("view_product", new String[]{ "modify_product_description","modify_product_price", "modify_product_id"}); etc.

The String[] could contain many strings , not just 2 or 3.

These objects are used in many places in the application. So they are instantiated at start up and used every where. I was trying to avoid instantiating them in every place that they are used. The user class needs to validate if a given action is allowed through a call like cp1.isModifiable("modify_user_address"). There have been occasions where using strings like that was introducing bugs (i.e. for spelling error etc) which wouldn't get caught until much later. So this is an attempt to close that hole. The user class shouldn't need to know the name of the permissions, they can just say cp1.getModifyProductDescriptionPermission() and then just use that to validate. Please suggest, if there is another way to achieve this.
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Why don't you create a Class like PermissionEnum, have all your permission as final constants there

public class PermissionEnum {
int i = 0;

private PermissionEnum (int i) {
this.i= i;
}

public static final PermissionEnum modify_product_description = new PermissionEnum (1);

public static final PermissionEnum modify_product_price = new PermissionEnum (2);

public static final PermissionEnum modify_product_id = new PermissionEnum (3);


}

Now modify your ComplexPermissionImpl and ComplexPermission to accept (string,PermissionEnum []) as parameters so that when the user class calls your constructor it has to send only valid data.

Hope this helps
Dalia Sultana
Ranch Hand

Joined: Jul 16, 2006
Posts: 42
Thanks, This could almost work, but I haven't given you the entire picture, apologies for that. We have something like this currently.



an instance of this class is created and used like this in different places:



Here is the code for a PermissionedButton



This works nicely for almost all cases. But the problem arises when there are more than one modify permission controlling different parts of a dialog.

I need to be able to say
PerrmissionedButton testButton = new PermissionedButton(complexPermission);
then the button will just get enabled as long as the user has any of the permissions. This is fine as a ComplexPermission class can have a function that returns an ArrayList of all the activities that was used in the object creation.

But the problem is when I want to say
PerrmissionedButton testButton = new PermissionedButton("PRODUCTS_MODIFY_ID_ONLY");

How does the user class know that it has to pass PRODUCTS_MODIFY_ID_ONLY? I guess I can just specify all these permissions as emums and they will just have to look it up.

Thanks again!

Please suggest anything that you think might make things simpler for other developers. I am just aiming for a clear design! How does permissioning things work in a standard software? So simple yet so tedious!


[ May 14, 2008: Message edited by: Dalia Sultana ]
[ May 14, 2008: Message edited by: Dalia Sultana ]
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Permissions are basically for a resource (part of a UI) so when the caller is calling the PermissionManager it should pass on the resource (PermissionEnum) to check if it has the view/modify permission.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generating class methods at runtime