Classes:
- interface IEmployee
- Employee
- EmployeeInvocationHandler
- ReadOnlyInvocationHandler
- Test
import java.lang.reflect.*;
public class Test {
public static void main(String[] args) {
Test test = new Test();
IEmployee employee = test.getEmployee("George", 34);
System.out.println("\tTest employeeProxy");
IEmployee employeeProxy = test.getEmployeeProxy(employee);
test.testProxy(employeeProxy);
System.out.println("\tTest readOnlyProxy");
IEmployee readOnlyProxy = test.getReadOnlyProxy(employee);
test.testProxy(readOnlyProxy);
}
IEmployee getEmployee(String name, int age) {
Employee employee = new Employee();
employee.setName(name);
employee.setAge(age);
return employee;
}
public void testProxy(IEmployee proxy) {
System.out.println("Name is " + proxy.getName());
System.out.println("Current age is " + proxy.getAge());
try {
System.out.println("Set new age to 35");
proxy.setAge(35);
}
catch (Exception ex) {
System.out.println("Cannot set the age!");
}
try {
System.out.println("Set salary to 100");
proxy.setSalary(100);
} catch (Exception ex) {
System.out.println("Cannot set the salary!");
}
}
IEmployee getEmployeeProxy(IEmployee employee) {
return (IEmployee) Proxy.newProxyInstance(
employee.getClass().getClassLoader(),
employee.getClass().getInterfaces(),
new EmployeeInvocationHandler(employee));
}
IEmployee getReadOnlyProxy(IEmployee employee) {
return (IEmployee) Proxy.newProxyInstance(
employee.getClass().getClassLoader(),
employee.getClass().getInterfaces(),
new ReadOnlyInvocationHandler(employee));
}
}
public interface IEmployee {
String getName();
void setName(String name);
int getAge();
void setAge(int age);
int getSalary();
void setSalary(int salary);
}
public class Employee implements IEmployee {
String name;
int age = 34, salary = 100;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
import java.lang.reflect.*;
public class EmployeeInvocationHandler implements InvocationHandler {
IEmployee employee;
public EmployeeInvocationHandler(IEmployee employee) {
this.employee = employee;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws IllegalAccessException {
try {
//allow employee to get info about him /her
if (method.getName().startsWith("get")) {
return method.invoke(employee, args);
//employee is not able to set the salary
} else if (method.getName().equals("setSalary")) {
throw new IllegalAccessException();
//employee is able to set other information
} else if (method.getName().startsWith("set")) {
return method.invoke(employee, args);
}
} catch(InvocationTargetException ex) {
ex.printStackTrace();
}
return null;
}
}
import java.lang.reflect.*;
public class ReadOnlyInvocationHandler implements InvocationHandler {
IEmployee employee;
public ReadOnlyInvocationHandler(IEmployee employee) {
this.employee = employee;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws IllegalAccessException {
try {
//read only user can only get info about an employee
if (method.getName().startsWith("get")) {
return method.invoke(employee, args);
} else if (method.getName().startsWith("set")) {
throw new IllegalAccessException();
}
} catch(InvocationTargetException ex) {
ex.printStackTrace();
}
return null;
}
}
Running the code:
\dynamicproxy>javac -d class *.java
\dynamicproxy>cd class
\dynamicproxy\class>java Test
Test employeeProxy
Name is George
Current age is 34
Set new age to 35
Set salary to 100
Cannot set the salary!
Test readOnlyProxy
Name is George
Current age is 35
Set new age to 35
Cannot set the age!
Set salary to 100
Cannot set the salary!
Niciun comentariu:
Trimiteți un comentariu