At the startup, I am creating a string according to the database type in class A. Then I want to inject that string value to class B after the setBitAndSqlExpression() function is called. After the injection I want to use that variable in Class B. Class B is executed if the user makes a request after run time init.
Note that they are in different packages. I cannot pass them via the constructor.
class A { //code first enters here
public String BITAND_SQL_EXPRESSION;
public void setBitAndSqlExpression() {
try {
String driverName = dataSource.getConnection().getMetaData().getDriverName();
if (driverName.contains("PostgreSQL")) {
BITAND_SQL_EXPRESSION = "%s" + "::INTEGER" + " & %s = %s";
} else {
BITAND_SQL_EXPRESSION = "BITAND (%s,%s) = %s";
}
} catch (Exception e) {
LOGGER.warn("Driver cannot be found. Setting operations will not work correctly.");
}
}
}
class B { //enters here if only user makes a request after class A creation.
public String BITAND_SQL_EXPRESSION; // want to use the value at class A
public B(
...//codes and constructors.
}
Is a factory method possible for you ?
It would be in the package of the B class :
public interface IB {
methodA();
methodB();
}
class B implements IB{ //enters here if only user makes a request after class A creation.
public String BITAND_SQL_EXPRESSION; // want to use the value at class A
public B(
...//codes and constructors.
}
public final class BFactory {
public static IB create(String sqlExpression) {
return new B(sqlExpression);
}
}
Then in the package of class A you could call this factory
BFactory.create(BITAND_SQL_EXPRESSION);
A would depend of an interface and not the B implementation.
Related
If I have something like
public class OwnerClass1{
public class OwnedClass{
// definition 1
}
}
public class OwnerClass2{
public class OwnedClass{
// definition 2
}
}
From a function that is implemented as below:
public <OwnedClass> boolean doStuff(OwnedClass example) {
System.out.println(example.<???>);
// example.getClass() returns "OwnerClass1$OwnedClass" etc here, so I guess getting this to string and trimming after $ would be one solution
// example.getSuperClass() returns "java.lang.Object" here, so not what I need
}
How can I get the behavior as below:
doStuff(new OwnerClass1.OwnedClass());
// OwnerClass1
doStuff(new OwnerClass2.OwnedClass());
// OwnerClass2
Note: Code above is meant to give a rough idea of the structure, not to be compiled out of box.
I understand that you want your unique doStuff method to act differently depending of the class on the actual class of the parameter you pass to it.
For this to be possible, OwnedClass1 and OwnedClass2 have to extend a common class or interface (that I guess you call OwnedClass). Otherwise your doStuffwill have to take an Object as param.
Then you can use instanceofto differenciate the classes.
Example with Object :
public boolean doStuff(Object example) {
if (example instanceof OwnedClass1) {
System.out.println("this is a class 1!");
} else if (example instanceof OwnedClass2) {
System.out.println("this is a class 2!");
} else {
throw new RuntimeException("Not supported : " + example.getClass());
}
}
And if you are only interested in the short name of the class, then you could go like that :
public boolean doStuff(Object example) {
System.out.println("this is a " + example.getClass().getName());
}
or even
public boolean doStuff(Object example) {
System.out.println("this is a " + example.getClass().getName().replaceAll(".*\\.", ""));
}
HTH!
The OwnedClass doesn't extend the OwnerClass1, it only extends the Object class. Most likely you are looking for getEnclosingClass() method instead of getSuperClass().
public boolean doStuff(Object example) {
System.out.println(example.getClass().getEnclosingClass());
}
Both OwnedClass sub classes could inherit from another class, that you pass to doStuff.
public class OwnerClass1 {
public class OwnedClass extends SuperOwnedClass {
// definition 1
}
}
public class OwnerClass2 {
public class OwnedClass extends SuperOwnedClass {
// definition 2
}
}
public class SuperOwnedClass {
}
public boolean doStuff(SuperOwnedClass example) {
System.out.println(example.<???>);
}
Or even better, let em implement interfaces.
I am writing a testing framework using Gauge.
I want some initilization logic performed in one class, and the steps logic to reuse it, like this:
public class A {
protected String property = "";
#BeforeSpec
public void init(){
property = "hello";
}
}
public class B extends A {
#Step("...")
public void verifyProperty() {
assertEquals(property, "hello");
}
}
I can't seem to be able to achieve this. When performing the steps, the "property" is always null.
Placing the #BeforeSpec in class B and calling super.init() works, but I would like to avoid having this call in every test class that extends A.
Has anyone encountered and solved such an issue?
Try to use a static variable:
public class A {
public static String property = "";
#BeforeSpec
public void init(){
property = "hello";
}
}
public class B {
#Step("...")
public void verifyProperty() {
assertEquals(A.property, "hello");
}
}
I am new to java, I want to know if it's possible. But I am calling this method from another class. But the parameters are giving me a hard time. I want to call this method without having the parameters with it or have it go to the other class without any errors.
public List<String> getApporDb(
String servers,
String username,
String password,
Float critThreshold) throws Exception {
try {
//something
try {
// another thing
if (serverArray != null && serverArray.length > 0 && folders != null && folders.length > 0) {
logger.info("inside loop of CpuUtilization and MemoryUtilization");
Float cpuUtilization = null;
List<String> memoList = new ArrayList<String>();
for (String server : serverArray) {
memoList.add(server);
}
}
}
}
Essentially I want to get the arraylist in the middle of this method to the other class.
The other class
public void arrayList() {
databaseClass dbc = new databaseClass();
dbc.getApporDb(String servers,
String username,
String password);
// errors everywhere due to parameters form the line above.
}
Error message : Multiple markers at this line
- appServerCpuDiskspaceutilizationMap cannot be resolved to a
variable
- username cannot be resolved to a variable
- password cannot be resolved to a variable
- servers cannot be resolved to a variable
In a word - no. You need to supply all the parameters when calling a method.
You can extend the class with one of your classes and then use #Override to create a new version of the method. You would then use your class as if it were the class you extended: It has the same methods and functionality, except for the methods you have overridden.
An example could look like this:
Class a:
Public class A{
public void printName(){
System.out.println("A");
}
public void test(){
System.out.println("test");
}
}
Class B:
public class B extends A{
#Override
public void printName(){
System.out.println("B");
}
}
Main:
public static void main(String[] args){
B b = new B();
A a = new A();
a.printName(); -------- A
b.printName(); -------- B
b.test(); -------- test
a.test(); -------- test
}
I have the following class structure. I would like to know how can I jump in eclipse to the actual implementation of th gethod printVar().
If I hover (hold down STRG) over the 2 method calls in main and click open implementation I get both Classes displayed. Is there a way to directly jump to the implementation in class B for this method call w.getWrapperVar().printVar()
public class A {
private String var;
public A(String a){
this.var=a;
}
public void printVar(){
System.out.println("class A print: " + this.var);
}
}
public class B extends A{
private String var;
public B(String a){
super(a);
this.var=a;
}
#Override
public void printVar(){
System.out.println("class B print: " + this.var);
}
}
public class Wrapper {
private B wrapperVar;
private A wrapperVar2;
public Wrapper(String var){
this.wrapperVar = new B(var);
this.wrapperVar2 = new A(var);
}
// explicit Return Class A
public A getWrapperVar(){
return this.wrapperVar;
}
public A getWrapperVar2(){
return this.wrapperVar2;
}
}
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Wrapper w = new Wrapper("value");
w.getWrapperVar().printVar();
w.getWrapperVar2().printVar();
}
}
Eclipse doesn't know that the implementation class is B, so it can't take you directly to B.
However, you can click on printVar and press Ctrl-G to search for all methods that might be the actual implementation.
After selecting a method call you can hit the F3 function key. This action will take you to the definition of the method. This definition may be in an interface.
When you are in that file you may want to hit Ctrl+T. This will bring up a type hierarchy showing the classes that implement the method you were looking for.
Hope this helps.
Is there a way of retrieving an array of static classes within the Network class (defined below), and pass each class's attribute class into a parameter of a method call kryo.register?
public class Network {
// Classes to be transferred between the client and the server
public static class A {
public int id;
public String name;
}
public static class B {
public int id;
public int x;
public int y;
}
// Rest of the classes are defined over here
static public void register(EndPoint endPoint) {
Kryo kryo = endPoint.getKryo();
// typical way of registering classes so that kryonet can use it
// kryo.register(A.class);
// kryo.register(B.class);
// the rest of the classes are registered with kryonet over here
// my attempt at solving the question,
// but for some reason this doesn't work?
for(Object o : Network.class.getDeclaredClasses()) {
kryo.register(o.getClass());
}
}
}
The problem is that you are using the class of the class, which isn't what you want. if you used the correct type for the result of the getDeclaredClasses() call, it would have been more obvious:
for(Class<?> c : Network.class.getDeclaredClasses()) {
kryo.register(c);
}
(btw, you are already using reflection -> getDeclaredClasses()).