Dynamically Accessing Class Members using String Class Name in JAVA

Following is the code for dynamically accessing member of a class when class name is not availaible while compiling.
You can use this method to invoke the method of class whose class name is available in string. and whose member name is also available in string.

/**
	 * Method used for dynamically accessing values from a class object in java. on passing method name or variable name along with object.
	 * @param obj : The object whose member variable or method has to be called.
	 * @param methodName : the method name of obj to be called. (null if not required)
	 * @param varName :  the variable name of obj to be called. (null if not required)
	 * @return object or null
	 */
	public static Object getValues(Object obj, String methodName, String varName) {
		try {
			// Creating the class object
			Class myclass = Class.forName(obj.getClass().getSimpleName());
			if (null != methodName) {
				//Loading all the methods of class.
				Method[] methods = myclass.getMethods();
				for (int i = 0; i < methods.length; i++) {
					//searching for the method we required
					if (methods[i].getName().equals(methodName)) {
						//Invoking and returning the method we require over object we pass.
						return methods[i].invoke(obj);
					}
				}
			}
			if (null != varName) {
				//Loading all the fields of class
				Field[] fields = myclass.getFields();
				for (int i = 0; i < fields.length; i++) {
					//Searching for the field name we require
					if (fields[i].getName().equals(varName)) {
						//returning the value of the required field.
						return fields[i].get(obj);
					}

				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

Released a new beta version of my WordPress plugin – ULTIMATE CMS

Hi There,

Today i have released a new version of WordPress plugin ultimate cms. The plugin details can be found at plugin web site at http://www.xydac.com

More details are also available on plugin site in WordPress repository @http://wordpress.org/extend/plugins/ultimate-cms/

-Xydac

Accessing Array Values with a variable key

From last few days I have been trying to figure out how to access an array value in PHP using a key value that is stored in a string. So finally i was able to frame a function that could just do that.
Following is the code to get a value from an array with known key
Here
`$arr = Array with values
$key = string with keys seperated with –

function get_array_val($arr,$key)
{
	$e= explode('-',$key);
	$ar = &amp;$arr;
	foreach($e as $v)
		$ar = $ar[$v];
	unset($ar);
	return $arr;
	
}

Continue reading

Finding root of any equation using calculator

Hello
This is a trick is used in my exams to calculate one root of any equation using a non-programmable calculator that are usually allowed during examination.I’ll show how i did it.

Basically its based on Newton Raphson’s formula that i learned in one of my subject.
The formula is as

and in a more generalised form :

now the steps are as under to get the solution :

  1. Set Ans=1,That can be done by punching in 1 and pressing =.
  2. Next make an equation ((ANS)-(f(ANS)÷f'(ANS)))
  3. Punch in =
  4. Keep punching = until you start getting same result consecutively

Example

Say equation is

f(x) = y = sin x + cos x

f'(x) = y' = cos x + sin x

So,

f(ANS) = sin ANS + cos ANS
f'(ANS) = cos ANS + Sin ANS

Now making the equation we get

((ANS)-((sin ANS + cos ANS)÷(cos ANS + sin ANS)))

Now we just need to punch in = and we will get the answer.
You may give it a try I used CASIO FX-82 MS calculator by the way..

WordPress Ultimate Post Type Manager

As the users of Ultimate Taxonomy Manager opted for me to work with a post type manager. So these days I’ve been busy  working on new plugin for WordPress named Ultimate Post Type Manager.Finally I am done with the new Plugin you can have a peek at

Plugin website

Please have a look, and do let me know what you think about it

Adding Custom Link to Plugins page in WordPress

I had to search a lot for adding a custom link to WordPress Plugin page that linked to Settings page of my plugin. So here’s a Tutorial on how to add it.

There are two parts of adding Custom Links

Add the following lines to add a filter

add_filter('plugin_action_links',' xydac_settings', 10, 2);

Here xydac_setting refers to the function which is used to add links.

Add the function for handling the Filter

function xydac_settings($links, $file) {
static $this_plugin;
if (!$this_plugin) $this_plugin = plugin_basename(__FILE__);
if ($file == $this_plugin){
$my_link = "<a href="admin.php?page=ultimate-taxonomy-manager">" . __("Settings") . "</a>";
array_unshift($links, $my_link);
}
return $links;
}

Add the links to Settings file of your plugin in this function and assign it to variable $my_link.

Thats all you have to do to add the settings link to your Plugin.

.

Ultimate Taxonomy Manager for WordPress

Finally i completed my first WordPress plugin named ” Ultimate Taxonomy Customizer”, which can be useful to create as many Taxonomy as you want as well as Custom Fields for those taxonomies.

Creating custom fields basically means adding more details to taxonomy. Continue reading

Follow

Get every new post delivered to your Inbox.