Is it possible to use clojure's gen-class macro to generate a class with static fields? - static

Can the following class be generated using Clojure's gen-class macro?
public class Test {
public static final String TEST_NAME = "This test's name.";
}
If not, why not?

No, gen-class can't generate static fields because gen-class is for introp. It attempts to give you the power to create the kind of classes demanded by other java libs. It is rare for a java lib to require that you provide a class with a particular static field.

Related

How Mybatis (iBatis) read my private variable?

I was wondering how Mybatis get the private variable in Java.
For example:
Let's say we have a Java class called Foo:
public class Foo{
private int foolID;
public Foo(int foolID){
this.foolID = foolID;
}
}
And let's create XML mapper for insert.
<insert id="insert" parameterType="Foo">
insert into foo_table (id)
values (#{foolID});
</insert>
Let's say there are FooDAO java class and FooMapper java interface for this insert.
My question is how come foolID is readable even without Getter (Even if there is a getter method for foolID, I never specify what the getter is...). It seems like magic to me, and I know there is no magic for programming... :)
The only way I can think of is reflection.
Thanks for your help in advance.
That's correct, reflection is used to access private fields, but only if accessing private fields is not restricted.
Seams like reflection is used heavily not just to access private fields but to invoke setters getters etc.

How to avoid that proguard obfuscates the classes annotated with #OnStart

In applications based on NetBeans Platform 7.2, it is possible to
replace the ModuleInstall classes with this code:
import org.openide.modules.OnStart;
import org.openide.modules.OnStop;
#OnStart
public final class Installer implements Runnable {
#Override
public void run() {
System.out.println("enable something...");
}
#OnStop
public static final class Down implements Runnable {
#Override
public void run() {
System.out.println("disable something...");
}
}
}
My problem is that, after obfuscation, the class loader does not find
the annotated classes.
In the Proguard configuration I added (as suggested here)
-keep #org.openide.modules.OnStart class *
But apparently this is not enough or it does not work.
Does anybody have a suggestion?
From I could figure out, you need to explicitly keep the annotations that you use to keep whatever specifications. So, in your case, adding
-keep enum org.openide.modules.OnStart
would allow this annotation to be used as a selector.
Proguard really ought to include a warning message if annotation selectors are not actually matching. It also doesn't really make sense to keep the annotation, especially if it's not of runtime retention.
Have you tried -keepattributes *Annotation*? It might do the trick, based on proguard usage.

EJB3.1 Providing property value

Maybe this question could be duplicated. I just can't find any good search words.
How can I provide some development property value to SLSBs?
#LocalBean
#Stateless
class ClouldBean {
public void doSomethingWithUsernameAndPassowrd() {
// ...
}
private String username;
private String password;
}
I just want to know how to inject username and password in a very standard and portable way.
Do I have to use some standard property configuration file?
Do I have to set it be contracted as being provided by container?
Yes, there is standard and portable way and it is called environment entry.
Add following to ejb-jar.xml:
<ejb-jar>
<enterprise-beans>
....
<session>
<ejb-name>ClouldBean</ejb-name>
....
<env-entry>
<env-entry-name>username</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>my name</env-entry-value>
</env-entry>
...
</session>
</enterprise-beans>
</ejb-jar>
And then you can inject value to the variable in your bean:
#Resource(name="username")
private String username;
For more detailed example you can take a look to this blog post. For all the details best source is EJB 3.1 specification section 16.4.

How can I write (Application.Current as App) in my Silverlight class library?

I have few public properties in App.xaml.cs which is in project A and I want to refer them in my project B. However my project A has a reference to project B, so I cannot add again the reference of project A in project B otherwise it will result in cyclic error. So how can I refer those properties in my class library? I don't want to use reflection :).
As a workaround I have stored those properties in one class in project B (so it can be referred in project A as well as project B) and made those properties to be static and all works fine. However I am still curious to know what if I had stored them in App.xaml.cs? Any options available?
Thanks in advance :)
The App class should expose things that are only relevant to the application project. As soon as you realised that you wanted these things accessable in B.dll they became relevant to more than just the application project and therefore no longer belong in the application project.
Adding a class to B.dll that carries these things as static properties could be a reasonable approach. Another common pattern is to have a single Current static property.
public MyClass
{
private static MyClass _current = new MyClass();
public static MyClass Current { get { return _current; } }
public string SomeInstanceValue { get; set; }
}
Both A and B would access things using the pattern var x = MyClass.Current.SomeInstanceValue. The advantage of this approach is that it allows the Current property getter to determine if a "current" instance is available or not.
You might also want to review the documentation on ApplicationLifeTimeObjects.
When A and B both need something, maybe you should put them in a C project (C as in Common) and then refer to C from both A and B.

In php we can access static member functions using class objects. Can someone please tell any practicle use of this feature

In php we can call static member functions using class objects. For example
class Human
{
public static function Speak()
{
echo "I am a human.";
}
}
$human = new Human();
$human->Speak();
What we would expect is that a static member function can only be called using the class name and not the class instance variable (object). But what i have seen while programming is that php allows calling a static member function using the class object also. Is there any practical use or some important reason that this feature has been provided in php ?
This feature exists in java and c++ also. Thanks Oli for pointing this out in your response.
This is the same as in other OO languages, such as C++ and Java. Why would you want the interpreter to prevent this?
UPDATE
My best guess for this (and this is only a guess) is "for convenience". In essence, why should the user of your class necessarily care whether a given member function is static or not? In some circumstances, this will certainly matter; in others, maybe not. I'm not saying this is a great justification, but it's all I can come up with!
it allows you to abstract from the particular definition of the method, so that for example if you had to turn it into a static one at some point, you don't have to rewrite all the method calls!
I can't answer for PHP, (or really for anything) but consider this hypothetical C++:
class base{
public:
static void speak(){cout<<"base\n";}
};
class sub :public base {
public:
static void speak(){cout<<"sub\n"; }
};
int _tmain(int argc, _TCHAR* argv[]){
base *base1 = new base();
base1->speak();
sub *sub1 = new sub();
sub1->speak();
base *sub2 = new sub();
sub2->speak();
((sub*)sub2)->speak();
}
The output would be:
base
sub
base
sub
I'm sure it could be useful... maybe helping you determine which class's static method you should call based on the object currently in hand.

Resources