So I have an existing piece of code that looks like this:
Metrics metrics = null;
try {
metrics = metricsService.getCurrentMetrics();
} catch (SQLException e) {
e.printStackTrace();
}
//Do some stuff with the metrics
I have recently implemented a seperate mechanism as a way to track API metrics under a separate class, cleverly called APIMetrics.
So, with this change, the code would look something like this:
Metrics metrics = null;
APIMetrics apiMetrics = null;
try {
if(user.isAPI())
apiMetrics = metricsService.getCurrentAPIMetrics();
else
metrics = metricsService.getCurrentMetrics();
} catch (SQLException e) {
e.printStackTrace();
}
//Do some stuff with the metrics
The issue is that the code below, the "do some stuff with the metrics" is all written in terms of using the metrics object. Is there a way I can possibly set this up so that the "metrics" object refers not to just the object of type Metrics, but whatever object is the one we want to use?
So, for example, if we have a non-API user, we would want metrics to be of type Metrics and the result of metricsService.getCurrentMetrics(). However, if we have an API user, we would want metrics to be of type APIMetrics and the result of metricsService.getCurrentAPIMetrics().
Is there any good way to go about this? The Metrics and APIMetrics class share all methods. I know there may be a way to do this using inheritance or polymorphism, but I couldn't figure out exactly what to do. Thanks!
Make both classes Metrics and APIMetrics implement the same interface. Then, declare a single variable whose type will be this interface, initialize it accordingly and use it through the code.
Yes, as per the above answer you can make both classes implement an interface which declares all the methods.
public interface MetricsInt(){
// declare common methods
}
And in your code you can introduce a factory method that will take the user as an input and return the expected type of Metrics instance. Since the variable is of the type MetricInt, both type of objects can be set to it.
MetricsInt metrics = null;
try {
metrics = metricsService.getCurrentMetricsForUser(user);
} catch (SQLException e) {
e.printStackTrace();
}
Related
how are you? I'm trying to do some dynamic method calls to get sql strings on various objects in Java (Android), but i'm stuck with some questions about performance and stability.
Context Example: Repository class onCreate method get all entity objects (tables) and call a method (getCreateTable for example) to get a sql string to execute.
Sure i can explicit call class by class caling each method, but i have other calls like "dropTables", "truncateTables" and etc, and i do not want to be repeating the same structure all the time.
public void CreateTables() {
execute(Entity1.getCreateTable());
execute(Entity2.getCreateTable());
execute(Entity3.getCreateTable());
[..]
execute(Entity50.getCreateTable());
}
public void DropTables() {
execute(Entity1.getDropTable());
execute(Entity2.getDropTable());
execute(Entity3.getDropTable());
[..]
execute(Entity50.getDropTable());
}
Until now i know i can do that in 3 diferent ways.
1) Using reflection (currently in use): Basicaly, i store all the objects class in a list, and then use the reflection to call the desired static method.But i know that reflection not always should be the first choice.
private final List<Class> entityList = new ArrayList<Class>() {
{
add(Entity1.class);
add(Entity2.class);
add(Entity3.class);
}
};
public void createTables() {
/* get all query strings */
List<String> queryList = getQueryList("createTable");
try {
for (String query : queryList) {
execute(query);
}
} catch (SQLException e) {
[...]
}
}
private List<String> getQueryList(String methodName) {
List<String> queryList = new ArrayList<>();
for (Class<?> objectClass : entityList) {
try {
Method[] ms = objectClass.getMethods();
for (Method me : ms) {
if (me.getName().equals(methodName)) {
String query = (String) me.invoke(null);
if (query != null && query.length() > 0) {
queryList.add((String) me.invoke(null));
}
break;
}
}
} catch (Exception e) {
[...]
}
}
return queryList;
}
2) Storing object instance in list: I can have a list with the objects instanced and then cast then into abstract parent class (or interface) and call the methods to get the sql string. In this case, i don't know if is a good practice to keep an list of instanced objects in memory, maybe this could be worst than use reflection depending on list size.
private final List<BaseEntity> entityList = new ArrayList<BaseEntity>() {
{
add(new Entity1(context));
add(new Entity2(context));
add(new Entity3(context));
}
};
public void createTables() {
for (BaseEntity entity : entityList) {
try {
execute(entity.getCreateTable());
} catch (Exception e) {
[...]
}
}
}
3) Storing all the strings into JSON object: I don't tested that one yet, but i'm sure with should work. I can call an "init" method to iterate over all objects and create that JSON object/array with all the sql strings (drop, create, truncate and etc).
I really appreciate if you share with me what you think about these approaches (pros and cons) or another better solution.
As pointed out in the comments, it was a clarly a bad design (it's an old project that i'm refactoring). So i decided to get away from reflection and spend some time to redesign the code itself.
I created an base super class to handle all similiar methods and let the entities/models implement only the required individual rules, so DB access is stored in only one class as a Singleton. It's far better to use interface polymorphism.
In this way, the db class handle the dinamic SQL generation to avoid repeating the same code everywhere and re-use/recycle the list of instances to improve performance.
Obs. 1: Reflection throw down performance and usually let debbuging harder. Sure it can save some time as it is fast to implement, but will disable most of the IDE features, which makes it worthless in most cases.
Obs. 2: Keeping a list of DB instances active should never be done either. It's never a good idea to have many instances access database simultaneously, it can cause DB to lock and reproduce unexpectad issues.
Obs. 3: That JSON thing... forget about it. I'm sorry to suggest something so ugly.
I'm very new to Java so it makes it hard for me to explain what I'm trying to do.
I have an abstract class that invokes several object constants like this:
public abstract class Enchantment implements Keyed {
/**
* Provides protection against environmental damage
*/
public static final Enchantment PROTECTION_ENVIRONMENTAL = new EnchantmentWrapper("protection");
In a different file I can access this perfectly fine with Enchantment value = Enchantment.PROTECTION_ENVIRONMENTAL;
However, I'm trying to use a string variable for this instead. Something like this:
String str = "PROTECTION_ENVIRONMENTAL";
Enchantment value = Enchantment.str;
Obviously that won't work. So I did a bunch of research and learned I need to use reflection for this. Using this source code's docs I figured I was looking for field data. So I tried both:
Field fld = Enchantment.class.getField("PROTECTION_ENVIRONMENTAL");
Field fld = Enchantment.class.getDeclaredField("PROTECTION_ENVIRONMENTAL");
But these returned me a NoSuchFieldException. As I was on it, I've tried both getMethod() and getDeclaredMethod() just as well equally with no luck.
I'm now at the point that these are probably "object constants"? I'm not sure how to call them. But I'm definitely at a loss on how to get this to work now and after everything I've tried myself, I figured it was time to ask for some help here.
That one comment is spot on: you absolutely do not use reflection here.
There are only two valid reasons to use reflection:
you are creating a framework that has to deal with classes it doesn't know about
you have for some other reason to deal with classes you don't know about at compile time
But your code perfectly knows about that Enchantment class, its capabilities, and so on. Therefore reflection is the wrong approach. You figured it yourself: it is damn hard to get right, and damn right to get it wrong in some subtle ways. And when you get it wrong, it always blows up at runtime. Reflection code compiling means nothing. It always waits for you to run it to throw up in your face.
So to answer your question by not answering it: use a Map. Like:
Map<String, Enchantment> enchantmentsByConstantName = new HashMap<>();
enchantmentsByConstantName.put("PROTECTION_ENVIRONMENTAL", PROTECTION_ENVIRONMENTAL);
Alternatively, these constants could go into an enum, as outlined in the other answer, but in a sightly different way:
enum EnchantmentHolder {
PROTECTION_ENVIRONMENTAL(new EnchantmentWrapper("protection")),
ANOTHER_ENCHANTMENT(...)
A_THIRD_ENCHANTMENT(...)
...;
private Enchantment enchantment;
private EnchantmentHolder(Enchantment enchantment) {
this.entchantment = entchantment;
}
public Enchantment getEntchantment() { return entchantment; }
You may want to look into enumerations if you know they're going to be constant values;
public enum Enchantment {
PROTECTION_ENVIRONMENTAL {
public void cast() {
// do enum-specific stuff here
}
},
ANOTHER_ENCHANTMENT {
public void cast() {
// do enum-specific stuff here
}
},
A_THIRD_ENCHANTMENT{
public void cast() {
// do enum-specific stuff here
}
};
public abstract void cast();
}
enums can be treated like classes and have methods and properties. You can also convert to and from strings Enchantment.valueOf("PROTECTION_ENVIRONMENTAL") but that's generally if you are reading from a configuration file - in code you'd reference the value directly.
Once you have the Field, you need to call Field.get(Object) with an instance (in this case the class). Something like,
Class<?> cls = Enchantment.class;
try {
Field f = cls.getField("PROTECTION_ENVIRONMENTAL");
System.out.println(f.get(cls));
} catch (Exception e) {
e.printStackTrace();
}
Since you want the Enchantment, you could then test that the instance you get is assignable to Enchantment. Something like,
Class<? extends Enchantment> cls = Enchantment.class;
try {
Field f = cls.getField("PROTECTION_ENVIRONMENTAL");
Object obj = f.get(cls);
if (cls.isAssignableFrom(obj.getClass())) {
Enchantment e = cls.cast(obj);
System.out.println(e);
}
} catch (Exception e) {
e.printStackTrace();
}
But the enum approach is better.
In my EObject I have the field eStorage, which contains data, I want to use.
Is there a possibility to read out the eStorage?
I tried the code below but it doesn't work:
doIt(EObject object) {
object.getEStorage;
// use the eStorage...
}
Chances are that eStorage is a private field.
So either,
Re-read the javadoc of the EObject interface and/or the javadoc of the particular implementation of EObject you're using. You may find a method offering the data you're looking for.
Access the private field via Reflection
try {
Field f = object.getClass().getDeclaredField("eStorage");
f.setAccessible(true);
Object theDataYouWant = f.get(object);
} catch(Exception e) {
// Handle exception here...
}
References: How do I read a private field in Java?
I am trying to create a library which manages data. Part of it is storing stuff in databases. Since I don't know what the user of library wants to store, I am using generics. Here is my method to read a database table.
public <TEntity extends SyncableBase> List<TEntity> loadItems(Class clazz) {
List<TEntity> listToReturn = new ArrayList<TEntity>();
DatabaseHelperBase dbHelper = getDbHelper();
Dao<TEntity, Integer> dao;
try {
dao = dbHelper.getDao(clazz); //Is this approach correct?
listToReturn = dao.queryForAll(); //This doesn't work
} catch (SQLException e){
e.printStackTrace();
}
return listToReturn;
}
And somewhere in my app (which consumes this library) I call this method like so:
List<ToDoCategory> catList = mirrorservice.<ToDoCategory>loadItems(ToDoCategory.class);
No matter what I do, the empty ArrayList is returned to catList. I tried similar approach with queryForId too. That didn't work either. Meanwhile, my add is working just fine!
I suspect that there is something wrong with the way I'm creating Dao. What is the problem?
I am using some third party library to connect to a server via async protocol and get response back. For example method to get userid by username looks like this:
public int getUserid(String username) {
int userid = 0;
connection.call("getUserid", new Responder() {
public void onResult(final int result) {
System.out.println("userid: " + result);
//how to assign received value to userid and return it?
}
}, username);
//wait for response
while (userid == 0) {
try{
Thread.sleep(100);
} catch (Exception e) {}
}
return userid;
}
The problem is I can't assign returned "result" from server response to "userid" variable from the method (in order to return it after). How to solve this? I probably can assign it to some class variable rather than method variable but I want to keep it within method scope so I don't have to deal with concurrency issues.
Thanks.
If I understand your question correctly, you're asking how you can write a variable from inside an anonymous class.
Anonymous classes can only access final variables, and can't directly "write" them.
A straightforward solution that is "good enough" is to create sort of a ValueBox class with a single value field and a getter and setter. You can then instantiate a new one in the function as a final variable, and have your anonymous class access it. The anonymous class will use its getter and setter to write/read.
The fact that the variable is final just means that you can't aim the reference anywhere else, but you can still change the contents of the referred object from either function.
The bigger problem you are going to have is in waiting until the callback has been called. This sort of wait-sleep might be good enough, but you may want to consider timeouts, threads, etc, depending on what you're trying to achieve.
In addition, this all assumes that you are never going to call this twice on the connection. Otherwise, you need to provide more info to us on your synchronization model.
Here's some sample code:
public int getUserid(String username) {
final ValueBox<Integer> userid = new ValueBox<Integer>();
connection.call("getUserid", new Responder() {
public void onResult(final int result) {
System.out.println("userid: " + result);
userId.setValue(result);
//how to assign received value to userid and return it?
}
}, username);
//wait for response
while (userid.isEmpty()) {
try{
Thread.sleep(100);
} catch (Exception e) {}
}
return userid.getValue();
}
The simplest change is to use something like java.util.concurrent.SynchronousQueue. But possibly you want to provide an event driven interface yourself.