Create object in function based on Class argument - java

I want to define a function that creates different type objects that share the same base class. I'd like to pass in the object type and have the function creating the object and then modifying its attributes. The problem is that the main class from which all these objects are created, does not have the object's attributes so the code fails to compile.
Example:
public void new_generic_report(Class report_class, String report_name) {
Report new_report = this.reportManager.createReport(report_class);
new_report.set_name(report_name);
}
Calling new_generic_report(GreenReport.class, "green_report"); fails because new_report is of the class Report instead of GreenReport so it does not have the .set_name method.
I know I could implement the .set_name method (and other common methods) in the main Report class but I am writing code to interface with an API that I cannot modify.

If you are sure that createReport returns an instance of the correct class you can just do a cast:
((SpecialClass)new_report).set_name(report_name);
An alternative is to use reflection:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
static class Base {};
static class Child extends Base {
public void setName(final String name) {
System.out.println("setName("+name+")");
}
}
public static void main(String[] args) {
new Test().new_generic_report(Child.class, "Testname");
}
public void new_generic_report(final Class clazz, final String name) {
Base base = createBase(clazz);
try {
Method m = clazz.getMethod("setName", String.class);
m.invoke(base, name);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
private Base createBase(Class report_class) {
return new Child();
}
}
Of course this only works, if the returned instance implements the method.

Create a parent class for your report for instance :
public abstract class NamedReport extends Report
{
public abstract void setName(String name);
}
class GreenReport extends NamedReport {
#Override
public void setName(String name) {
}
}
Then simply cast your class in your method :
public void new_generic_report(Class report_class, String report_name) {
Report new_report = this.reportManager.createReport(report_class);
if (new_report instanceof NamedReport)
{
((NamedReport)new_report).set_name(report_name);
}
}

Related

How to access a parent class variable via a child class

I am trying to re-build an OOP approach to mobile verification at the developers discretion. The concept I come up with is to allow for interfaces to manipulate the class. If the class implements the interface, then the verify method will be executed.
The problem I am facing, because I am only used to programming in less strongly-typed languages (PHP) is how to get a protected variable from a class extending the current class.
_areaCodes.stream().forEach(o -> {
try {
int prefix = Integer.parseInt(this._mobileNumber.charAt(0), this._mobileNumber.charAt(1));
} catch (Exception e) {}
});
This line of code is now giving me an error
_mobileNumber cannot be resolved or is not a field
Here is my full code and here is an example I wrote of the same concept in PHP which I am trying to implement in Java.
import java.util.ArrayList;
interface Verification
{
public void initVerification();
}
class AreaCode
{
private int _code;
private String _country;
public AreaCode(int code, String country)
{
this._code = code;
this._country = country;
}
public int getAreaCode() { return this._code; }
public String getAreaCountry() { return this._country; }
}
class VerificationHandler
{
private ArrayList<AreaCode> _areaCodes = new ArrayList<AreaCode>() {{
this.add(new AreaCode(44, "UNITED KINGDOM"));
this.add(new AreaCode(91, "INDIA"));
}};
public void initVerification()
{
if(this instanceof Verification) {
this.verify();
}
}
protected void verify()
{
_areaCodes.stream().forEach(o -> {
try {
int prefix = Integer.parseInt(this._mobileNumber.charAt(0), this._mobileNumber.charAt(1));
} catch (Exception e) {}
});
}
}
class Main extends VerificationHandler implements Verification {
protected String _mobileNumber = "+447435217761";
}
public class Hack1337 { public static void main(String[] args) { new Main(); } }
How can I retrieve a variable in a class extending another, ie:
class A { public String getB() { return this.b; } }
class B extends A { protected String b = 'A should get this'; }
B b = new B().getB();
Only instances of class B, or sub-classes of B can access the b instance variable directly (unless you cast A to B within the body of the A class, which is bad practice).
You can give class A read-only access to that value by overriding getB():
class B extends A
{
protected String b = 'A should get this';
#Override
public String getB() {
return this.b;
}
}
and you may also want to make the getB() method abstract in class A (which means making class A abstract):
abstract class A
{
public abstract String getB();
}
This would only make sense if different sub-classes of A are expected to return different things in getB(). Otherwise, you may as well move the b variable to the base class A.

How to Pass Interface Object to a Method

I have a main class as shown below. I want to Pass interface object to another method from Main method. I am Passing it as shown in my Main class below. But I am getting error "; expected". Can some one please help me?
This is my Main Class :
package com.armus.web.server;
import com.armus.common.dtflow.DfService;
public class TriggerAgain
{
public static void main(String[] args){
Long i= 97944605L;
DMSerImpl rdf = new DMSerImpl();
try {
/*********** Section start....Getting Problem In this section...How Can I Pass interface object to a method setDfService*********/
#Override
rdf.setDfService(new DfService()
{
Dfsessn dfsessn=getDfsessnById(i)
{
System.out.println("In main class...In interface method="+i);
}
})
/*************************************************Section End*******************************************************************/
com.armus.dto.Jinfo a=rdf.Trigger(i);
}
catch( Throwable e){
System.out.println("In exception = "+e+" "+i);
e.printStackTrace();
return; // Always must return something
}
}
}
This is my another class to which I want to Pass interface Object to "setDfService" Method.
package com.armus.web.server;
import com.armus.common.dtflow.DfService;
import com.armus.common.dto.Dfsessn;
import com.armus.foundation.client.exception.ServiceException;
import com.armus.dto.Jinfo;
public class DMSerImpl
extends Remoteservletsup
implements DMserv, DMAjxSer
{
private DfService dtflowser;
public void setDfService(DfService dtflowser)
{
this.dtflowser = dtflowser;
}
private Dfsessn getDfsessn(long sessionId)
throws ServiceException
{
try
{
dfSession = this.dtflowser.getDfsessnById(Long.valueOf(sessionId));
}
catch (ServerException e)
{
Dfsessn dfSession;
LOG.error(e.getMessage(), e);
throw new ServiceException(e.getMessage());
}
return dfSession;
}
public com.armus.dto.Jinfo Trigger(long sessionId)
throws ServiceException
{
Dfsessn dfSession = getDfsessn(sessionId);
//some code
}
}
Below is My Interface :
package com.armus.common.dtflow;
import com.armus.common.dto.Dfsessn;
import com.armus.common.exception.ServerException;
public abstract interface DfService
{
public abstract Dfsessn getDfsessnById(Long paramLong)
throws ServerException;
}
Thank you in Advance !!! :)
This is an example of creating an instance of an anonymous class
rdf.setDfService(new DfService() {
#Override
public Dfsessn getDfsessnById(Long paramLong) {
....
}});
You are:
Putting the #Override in the wrong place;
not declaring the method override properly; and
missing a closing semicolon:
rdf.setDfService(new DfService()
{
#Override
public Dfsessn getDfsessnById(Long i)
{
System.out.println("In main class...In interface method="+i);
}
});
#Override
rdf.setDfService(new DfService()
{
Dfsessn dfsessn=getDfsessnById(i)
{
System.out.println("In main class...In interface method="+i);
}
})
Above what you are trying to do is creating an anonymous class instance implementing the DfService interface but you are not doing it correctly(syntax wise).
Read about anonymous inner classes here https://www.geeksforgeeks.org/anonymous-inner-class-java/
Also, DfService is a functional interface(single method interface) so you can also use a lamda here. Something like this.
rdf.setDfService(paramLong -> System.out.println("In main class...In interface method="+i));
Take a look at this for more info https://javarevisited.blogspot.com/2015/01/how-to-use-lambda-expression-in-place-anonymous-class-java8.html

Need to call multiple EJBs based on the input string

I have a pojo class where in I have to call multiple ejbs based on the input string. For example , if the input is a x, I have to call XServiceBean, and if it is Y, I have to call YServiceBean.
I am planning to parameterise the input string x and the respective service bean in the database or xml. I do not want to put multiple if conditions or switch cases to call the service bean based on the input string.
Is there any simple pattern that i can use to achieve this. would be helpful if you can give some example
thank you
Main class where you can run as java for testing purpose
package stack;
public class ServiceInit
{
public static void main(String[] args)
{
new ServiceInit();
}
public ServiceInit()
{
ServiceBeanInterface xbean = ServiceFactory.getInstance().getServiceBean("X");
xbean.callService();
ServiceBeanInterface ybean = ServiceFactory.getInstance().getServiceBean("Y");
ybean.callService();
}
}
Service Factory which returns the bean that you want to call
package stack;
public class ServiceFactory
{
/*
* you can do it with factory and class reflection if the input is always the prefix for the service bean.
*/
private static ServiceFactory instance;
// the package name where your service beans are
private final String serviceBeanPackage = "stack.";
private ServiceFactory()
{
}
public static ServiceFactory getInstance()
{
if (instance == null)
{
instance = new ServiceFactory();
}
return instance;
}
#SuppressWarnings("unchecked")
public ServiceBeanInterface getServiceBean(String prefix)
{
ServiceBeanInterface serviceBean = null;
try
{
Class<ServiceBeanInterface> bean = (Class<ServiceBeanInterface>) Class
.forName(serviceBeanPackage + prefix + "ServiceBean");
serviceBean = bean.newInstance();
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return serviceBean;
}
}
The interface which is implemented by your service classes
package stack;
public interface ServiceBeanInterface
{
void callService();
}
XServiceBean class
package stack;
public class XServiceBean implements ServiceBeanInterface
{
#Override
public void callService()
{
System.out.println("I am X");
}
}
YServiceBean class
package stack;
public class YServiceBean implements ServiceBeanInterface
{
#Override
public void callService()
{
System.out.println("I am Y");
}
}

Dynamically invoke a method from a varying class

I have a requirement where in i need to invoke method from class in a particular pattern which is obtained as input argument.
public RandomMethod(String ClassName){
//Eg For Class Name Abc , there is a method AbcProcessor which i need to invoke
ClassName.ClassNameProcessor
}
Since i am getting the argument as String , i am not able to figure out how to cast String into a form where i can call something like Abc.AbcProcessor()
I believe there is some way to do this using reflections. But i am not sure how to proceed.
By reflection you can do that, try following sample:
Class A:
public class A {
public void print(){
System.out.println("A");
}
}
Class B:
public class B {
public void print(){
System.out.println("B");
}
}
Invoking print() from A and B:
public class Test {
public static void callPrint(String className){
try {
Class clazz = Class.forName(className);
Object obj = clazz.newInstance();
clazz.getDeclaredMethod("print").invoke(obj);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
callPrint("test.A");
callPrint("test.B");
}
}
You need to use reflecton, indeed:
public void randomMethod(String fullyQualifiedClassName, String methodName) throws ReflectiveOperationException {
Class<?> clazz = Class.forName(fullyQualifiedClassName);
clazz.getMethod(methodName).invoke(null);
}
which would work assuming you are calling public static method with no arguments

How to call instance of variety of classes in java

I create a class to handle some specific job that use variety of classes on my project.
But after finish the job class must call-back specific method on the called classes.
I use interface to handle this call-back method.
How can I store the called class?
I can get the instance from constructor but I'm looking for generic way.
Your question is not clear but it may be possible that you have missed the fact that classes can implement more than one interface.
public interface DoesAJob {
public void doIt();
}
public interface Finishes {
public void finish();
}
class AThing implements DoesAJob, Finishes {
#Override
public void doIt() {
}
#Override
public void finish() {
}
}
private void doTheJob(DoesAJob thing) {
thing.doIt();
}
private void finishUp(Finishes thing) {
thing.finish();
}
public void test() {
AThing thing = new AThing();
doTheJob(thing);
finishUp(thing);
}
You can use just Java Interface, or use Java Reflection.
First the Interface
package test;
public interface MyClassInterface {
public String getName();
}
next, the Interface Implementation
package test;
public class MyClassImplementation implements MyClassInterface {
String name;
public MyClassImplementation() {
name= "Whatever";
}
public String getName() {
return name;
}
}
finally invoke the class. just Interface example:
package test;
public class MainTest {
public static void main(String[] args){
MyClassInterface myClassImplementation = new MyClassImplementation();
System.out.println(myClassImplementation.getName());
}
}
Using Reflection example:
package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MainTest {
public static void main(String[] args)
throws InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException {
//using reflection
Object otherClassImplementation=null;
try {
Class<?> cls = Class.forName("test.MyClassImplementation");
otherClassImplementation = cls.newInstance();
Method method = cls.getMethod("getName");
System.out.println(method.invoke(otherClassImplementation));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

Categories

Resources