beans

Get/Set a bean property

This is an example of how to get and set a bean property. We are using the Statement class. A Statement object represents a primitive statement in which a single method is applied to a target and a set of arguments. In order to get and set a bean property you should:

  • Create a simple class, like Bean class in the example. It has two String properties and getters and setters for the properties.
  • Create a new object of Bean class.
  • Create a new Statement object for the specified object to invoke the setProperty1 method and by a String array of arguments.
  • Call the execute() API method of Statement. It finds a method whose name is the same as the methodName property, and invokes the method on the Bean object.
  • Create a new Expression. An Expression object represents a primitive expression in which a single method is applied to a target and a set of arguments to return a result. The new Expression is created with the specified value for the specified Bean object to invoke the getProperty1 method by the array of arguments.
  • Call execute() API method of Expression. It finds a method whose name is the same as the methodName property, and invokes the method on the Bean object.
  • Call getValue() API method of Expression to get the value of the property set in the Expression.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.beans.Expression;
import java.beans.Statement;

public class GetSetBeanProperty {

	public static void main(String[] args) throws Exception {

		Object o = new Bean();

		Statement stmt;
		Expression expr;

		// Set the value of property1
	    stmt = new Statement(o, "setProperty1", new Object[]{"My Prop Value"});
	    stmt.execute();

	    // Get the value of property1
	    expr = new Expression(o, "getProperty1", new Object[0]);
	    expr.execute();
	    System.out.println("Property1 value: " + (String)expr.getValue());

	    /////////////////////////////////////////////

	    // Set the value of property2
	    stmt = new Statement(o, "setProperty2", new Object[]{new Integer(345)});
	    stmt.execute();

	    // Get the value of property2
	    expr = new Expression(o, "getProperty2", new Object[0]);
	    expr.execute();
	    System.out.println("Property2 value: " + (Integer)expr.getValue());

	}

	public static class Bean {

		// Property property1
		private String property1;
	    // Property property2
		private int property2;

		public String getProperty1() {
			return property1;
		}
		public void setProperty1(String property1) {
			this.property1 = property1;
		}

		public int getProperty2() {
			return property2;
		}
		public void setProperty2(int property2) {
			this.property2 = property2;
		}

	}

}

Output:

Property1 value: My Prop Value Property2 value: 345

 
This was an example of how to get and set a bean property in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button