I'm currently working with the jodatime Java library and running into issues when trying to use it within coldfusion.
I've downloaded the latest jodatime 2.1 release, put the jar file into a folder on my local drive and pointed my coldfusion administrator to look at that folder in the ColdFusion Class Path under the Java and JVM settings page.
For the most part it works. but there are times when i get things like this:
local.oTestZone = createObject('java','org.joda.time.DateTimeZone').init('Europe/London');
Which should match with this: Constructor however I get an error in coldfusion saying:
Unable to find a constructor for class org.joda.time.DateTimeZone that accepts parameters of type ( java.lang.String ).
It works perfectly fine when I do something like this though:
local.oToZone = createObject('java','org.joda.time.DateTimeZone').forID('Europe/London');
Which matches on: forID
Am I missing something with my java implementation?
The DateTimeZone(String id) constructor is marked protected (it took me 3 reads of the JavaDoc to spot that), so CF won't be able to invoke it.
It looks to me like JodaTime expects you to use one of the static methods to construct your instances, so your second example is probably the right way of doing it.
You are dealing with an Abstract Class and a Protected Constructor.
A Protected Constructor means that only a subclass or a class in the same Package can call that constructor. So even though you are supplying the correct parameter, the constructor isn't available to your code.
The ColdFusion documentation has these tidbits:
"Although the cfobject tag loads the class, it does not create an instance object. Only static methods and fields are accessible immediately after the call to cfobject."
This is why forID works; it's a static method.
"To have persistent access to an object, you must use the init function, because it returns a reference to an instance of the object, and cfobject does not."
This and the previous statement are why methods like getOffset wont work in this situation.
I'm not familiar enough with this to know if there's a class that you can instantiate that will give you access to the constructor, but hopefully someone else can chime in.
Related
I am beginner in programming and i just started working with greenfoot (software for creating games using Java). When i was writing code i had to use construction builded in greenfoot which was using parameter described as: java.lang.class where i had to type ClassName.class . I was trying to go through documentation and a lot of other sources to figure out how it works and what is this, but to be honest i couldnt understand anything. Is there is someone who can tell me in simply words what does it mean and how construction like ClassName.class works? That is the first time i see dot notation used like this, before I have seen only dot notation when i tried to refer to for example other class method like: OtherClass.method() . So is it just builded in instance variable of every class or something like this? Thanks in advance
It's called a class literal. ClassName.class provides access to the instance of Class that represents the given class. So for instance, String.class refers to the Class instance for the String class. Using that object, you can do do various things (like find out what its members are at runtime via reflection). (FWIW, objects provide also access to the Class instance for their class via their getClass method.)
I'm writing some kind of library. I have an abstract class there. Client-code needs to extend it to use some methods. May happens that user quits application and after he restarts it I need to restore reference to his concrette class. My idea was to save canonical name of user's class and then just make newInstance() for it. However for some reason it can't create the instance. I've made a test:
void foo(AbstractClass a) {
String classname = a.getClass().getCanonicalName();
System.out.println(classname); //Output: "com.test.clientcode.Main.ConcretteClass"
a = null; // here I lost my reference to ConcretteClass for example, so all I have is a classname
Class.forName(classname).newInstance(); //Throws exception: "java.lang.ClassNotFoundException: `com.test.clientcode.Main.ConcretteClass"
}
It's a method within library code. For argument a I give it an instance of concrette user class.
UPDATE: to make things easier: in my library I have a method like above, argument a is a reference to client's ConcretteClass as we see in the output of 2nd line. Then I lose my reference. How can I make a new instance of ConcretteClass if the only thing I know is ConcretteClass' canonical name?
Your approach won't work.
If you want to "restore" the instance you should do in other way instead of simply newInstance. this is one thing. I don't know your concrete requirement, so I cannot answer further on the "restore" part.
I said your approach won't work, because you said your are writing a "library", so I guess client code will import your class, that is, your abstract class is in client codes's classpath. however, the client class won't be in your classpath. that's why you got the classnotfound Ex.
same as if I extend a class from guava for example, how come in guava codes, it knows my class and create an instance of my class?
I'm new to reflection, as a matter of fact, I never had to use it until I ran into the following problem -
I tried to use a method called getCompatibilityInfo defined in a class called Resources, but I saw that the only way of doing it is to call it like this:
Resources.class.getMethod("getCompatibilityInfo");
I can get an instance of Resources using a method called getResources(), so why isn't it accessible using getResources().getCompatibilityInfo()?
The class source code is here
Thanks!
Resources has not an empty public constructor. So new Resources() will give you a compile time error, also getCompatibilityInfo() is not part of the public API
That's something that only happens in Android, because android.jar has all the methods marked with #hide removed. This only matters while compiling, because android.jar is only used for development.
Long explanation on this answer.
I'm using Jboss 7.1.1.final and i have an application with heavy use of JMS. After doing some performance tests i see that whenever i reach a certain amount of messages the application starts to fail since there are not enough queue sessions.
After some research i discovered that this JMS attribute is only exposed in jboss version 7.1.2 (see here) .
Since, for legal reasons, i can't currently use this version (or the specific build where it was fixed) , the only other way to change it is by using reflection. So i downloaded Jboss source code and started playing with it, now i have a few questions:
First of all i discovered the class i need is org.jboss.as.messaging.CommonAttributes which is actually an interface and it has this line :
SimpleAttributeDefinition CONNECTION_THREAD_POOL_MAX_SIZE = new SimpleAttributeDefinition("thread-pool-max-size",
new ModelNode().set(HornetQClient.DEFAULT_THREAD_POOL_MAX_SIZE), ModelType.INT, true, MeasurementUnit.NONE);
Will ClassLoader.getSystemClassLoader() give me the classLoader i need in order to load the class?
How do i set an interface field value using reflection? (the Field.set() method requires an object instance and since it is an interface i cannot instantiate it)
You are talking about this piece of code. I must disappoint you: interfaces don't have fields, at all. They only have method declarations, without implementation.
What you see there are actually public static final (all these keywords are implicit for "fields" inside interfaces) constants defined within interface body. But the interface is only used as a namespace for them, they aren't real fields. Classes implementing such interface aren't inheriting these fields and having their own variable copy. They are only constants.
That being said - you cannot do anything, at least with this interface.
Let's say I have the following classes:
class A {
static public String a;
}
class B {
public function referToFieldInClassA() {
System.out.println(A.a);
}
}
Is there anything in the Java reflection APIs to allow me to find all places where a particular field is referenced? What I'm looking for is a way to find out that (given the example) class B has a reference to A.a.
I know I can get all the Fields in a Class via the reflection API. But now I want to find all references to that Field.
Thanks.
From the standard APIs, I think the answer is no. Eclipse can do it, but that's not at runtime.
For an incredibly hack-ish way of doing it, get the ClassLoader object, get the resourceAsStream to get something, use something like JODE to decompile it, and parse the source from inside of the program.
But that's a crazy amount of work. What the heck are you using this for?
If your need is compile-time rather than run-time, and you don't have an IDE, the very old trick of changing the names of the fields and recompiling, and searching for what fails to compile. ;]
Then, of course, get yourself an IDE.