Hi,
I'm using Java 1.6. Right now I have this in my code ...
Class<?> c = Class.forName
("myco.galc.capitol.tours.test.SelectTourValidationErrorsTest");
in which the string
"myco.galc.capitol.tours.test.SelectTourValidationErrorsTest" refers
to the current class (this) and its package. What is another way of
writing that expression that does not rely on hard-coding the package
and class name?
Thanks, - Dave
|
|
0
|
|
|
|
Reply
|
laredotornado (854)
|
12/1/2009 8:54:10 PM |
|
laredotornado wrote:
> Class<?> c = Class.forName
> ("myco.galc.capitol.tours.test.SelectTourValidationErrorsTest");
>
> in which the string
> "myco.galc.capitol.tours.test.SelectTourValidationErrorsTest" refers
> to the current class (this) and its package. What is another way of
> writing that expression that does not rely on hard-coding the package
> and class name?
this.getClass().getName()
Regards, Lothar
--
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!
|
|
0
|
|
|
|
Reply
|
Lothar
|
12/1/2009 9:14:56 PM
|
|
laredotornado wrote:
>> Class<?> c =3D Class.forName
>> ("myco.galc.capitol.tours.test.SelectTourValidationErrorsTest");
>
>> in which the string
>> "myco.galc.capitol.tours.test.SelectTourValidationErrorsTest" refers
>> to the current class (this) and its package. =A0What is another way of
>> writing that expression that does not rely on hard-coding the package
>> and class name?
>
Lothar Kimmeringer wrote:
> this.getClass().getName()
>
Don't you mean just 'getClass()', since the assignment is to a 'Class'
variable and not a 'String'?
This is useful for getting the current class, but useless if "that
expression" to which the OP refers is 'Class.forName()' and not
'Class<?> c'.
If the goal is to load and initialize a class as 'forName()' does,
then 'getClass()' will not be available, or else the class is already
loaded and initialized and there's no need. For this goal one pretty
much needs a class name, but it can be read from an external source
rather than hard coded.
If the goal is to get the current class's 'Class' instance, then
either 'getClass()', as you say, or
'SelectTourValidationErrorsTest.class' yields it. The latter is
necessary in a static context.
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
12/1/2009 9:49:52 PM
|
|
Lew wrote:
> Lothar Kimmeringer wrote:
>> this.getClass().getName()
>
> Don't you mean just 'getClass()', since the assignment is to a 'Class'
> variable and not a 'String'?
I was just answering what was asked (in the subject).
> This is useful for getting the current class, but useless if "that
> expression" to which the OP refers is 'Class.forName()' and not
> 'Class<?> c'.
>
> If the goal is to load and initialize a class as 'forName()' does,
> then 'getClass()' will not be available, or else the class is already
> loaded and initialized and there's no need.
Exactly what I was understanding when reading the OP:
>>> in which the string
>>> "myco.galc.capitol.tours.test.SelectTourValidationErrorsTest" refers
>>> to the current class (this) and its package.
> If the goal is to get the current class's 'Class' instance, then
> either 'getClass()', as you say, or
> 'SelectTourValidationErrorsTest.class' yields it. The latter is
> necessary in a static context.
Combining above "as a string" in the subject and "this" in the
OP-body I read the question "how can I get the classname from
a class as string".
If I'm wrong "laredotornado" should be question again and be
more verbose.
Regards, Lothar
--
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!
|
|
0
|
|
|
|
Reply
|
Lothar
|
12/1/2009 10:19:41 PM
|
|
laredotornado <laredotornado@zipmail.com> writes:
> Hi,
>
> I'm using Java 1.6. Right now I have this in my code ...
>
> Class<?> c = Class.forName
> ("myco.galc.capitol.tours.test.SelectTourValidationErrorsTest");
>
> in which the string
> "myco.galc.capitol.tours.test.SelectTourValidationErrorsTest" refers
> to the current class (this) and its package. What is another way of
> writing that expression that does not rely on hard-coding the package
> and class name?
>
> Thanks, - Dave
If what you want is a reference to the class object, you can use a
class literal:
import myco.galc.capitol.tours.test.SelectTourValidationErrorsTest;
Class<SelectTourValidationErrorsTest> c = SelectTourValidationErrorsTest.class;
Although I believe this compiles to a call to Class.forName, so
there's no runtime benefit.
--
Jim Janney
|
|
0
|
|
|
|
Reply
|
Jim
|
12/1/2009 10:28:18 PM
|
|
Jim Janney wrote:
> If what you want is a reference to the class object, you can use a
> class literal:
>
> import myco.galc.capitol.tours.test.SelectTourValidationErrorsTest;
> Class<SelectTourValidationErrorsTest> c = SelectTourValidationErrorsTest.class;
>
> Although I believe this compiles to a call to Class.forName, so
> there's no runtime benefit.
That cannot be true. 'Class.forName(String)' initializes the class,
reference to the 'class' literal must not.
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
12/1/2009 11:13:02 PM
|
|
Lew wrote:
> Jim Janney wrote:
>> If what you want is a reference to the class object, you can use a
>> class literal:
>>
>> import myco.galc.capitol.tours.test.SelectTourValidationErrorsTest;
>> Class<SelectTourValidationErrorsTest> c = SelectTourValidationErrorsTest.class;
>>
>> Although I believe this compiles to a call to Class.forName, so
>> there's no runtime benefit.
>
> That cannot be true. 'Class.forName(String)' initializes the class,
> reference to the 'class' literal must not.
It might compile to a call to Class.forName(String name, boolean
initialize, ClassLoader loader), with the initialize boolean zero.
Patricia
|
|
0
|
|
|
|
Reply
|
Patricia
|
12/1/2009 11:41:58 PM
|
|
Lew <lew@lewscanon.com> writes:
> Jim Janney wrote:
>> If what you want is a reference to the class object, you can use a
>> class literal:
>>
>> import myco.galc.capitol.tours.test.SelectTourValidationErrorsTest;
>> Class<SelectTourValidationErrorsTest> c = SelectTourValidationErrorsTest.class;
>>
>> Although I believe this compiles to a call to Class.forName, so
>> there's no runtime benefit.
>
> That cannot be true. 'Class.forName(String)' initializes the class,
> reference to the 'class' literal must not.
You're right. Compiling with Java 6 I see a single ldc instruction.
--
Jim Janney
|
|
0
|
|
|
|
Reply
|
Jim
|
12/2/2009 12:13:42 AM
|
|
According to Jim Janney <jjanney@shell.xmission.com>:
> You're right. Compiling with Java 6 I see a single ldc instruction.
Actually Sun's compiler compiled 'Foo.class' to a single ldc instruction
much before the JVM specification was amended to make that usage legal
(this was an obvious specification oversight).
At the same time, the Jikes compiler (from IBM) was trying to closely
follow the JVM specification as published, and as such resorted to a
rather indirect method for obtaining the Class instance without
initializing the class itself. Basically, for the following class:
public class Bar {
public static void main(String[] args)
{
Class c = Foo.class;
System.out.println(c.getName());
}
}
jikes will add a hidden static method to class Bar, the method being
named "class" (like the keyword -- not a problem for the JVM), and
the bytecode will look like this:
**** main (java.lang.String[]) -> void
0: getstatic Bar.class$Foo {java.lang.Class}
3: dup
4: ifnonnull #18
7: pop
8: ldc<String> "[LFoo;"
10: iconst 0
11: invokestatic Bar.class {(java.lang.String, boolean) -> java.lang.Class}
14: dup
15: putstatic Bar.class$Foo {java.lang.Class}
18: astore $1
19: getstatic java/lang/System.out {java.io.PrintStream}
22: aload $1
23: invokevirtual java/lang/Class.getName {() -> java.lang.String}
26: invokevirtual java/io/PrintStream.println {(java.lang.String) -> void}
29: return
**** class (java.lang.String, boolean) -> java.lang.Class
exception table:
0 12 12 java/lang/ClassNotFoundException
0: aload $0
1: invokestatic java/lang/Class.forName {(java.lang.String) -> java.lang.Class}
4: iload $1
5: ifne #11
8: invokevirtual java/lang/Class.getComponentType {() -> java.lang.Class}
11: areturn
12: new java/lang/NoClassDefFoundError
15: dup_x1
16: invokespecial java/lang/NoClassDefFoundError.<init> {() -> void}
19: invokevirtual java/lang/Throwable.initCause {(java.lang.Throwable) -> java.lang.Throwable}
22: athrow
I.e. the code uses Class.forName() to locate not the class Foo, but the
class of "an array of Foo"; this implies loading the Foo class without
intialiazing it, which is precisely what jikes wishes to achieve.
This idiom is convoluted, but it has the benefit of being compatible
with both Java 1.1 and J2ME, both of which being targets for jikes
(Class.forName(String, boolean, ClassLoader) appeared in Java 1.2, and
is not part of J2ME). Note also that the last version of jikes seems
to have been published in 2005, and knows nothing of Java 5.
--Thomas Pornin
|
|
0
|
|
|
|
Reply
|
Thomas
|
12/2/2009 1:43:27 PM
|
|
On Dec 1, 2:49=A0pm, Lew <l...@lewscanon.com> wrote:
> laredotornadowrote:
> >> Class<?> c =3D Class.forName
> >> ("myco.galc.capitol.tours.test.SelectTourValidationErrorsTest");
>
> >> in which the string
> >> "myco.galc.capitol.tours.test.SelectTourValidationErrorsTest" refers
> >> to the current class (this) and its package. =A0What is another way of
> >> writing that expression that does not rely on hard-coding the package
> >> and class name?
>
> Lothar Kimmeringer =A0wrote:
> > this.getClass().getName()
>
> Don't you mean just 'getClass()', since the assignment is to a 'Class'
> variable and not a 'String'?
>
> This is useful for getting the current class, but useless if "that
> expression" to which the OP refers is 'Class.forName()' and not
> 'Class<?> c'.
>
> If the goal is to load and initialize a class as 'forName()' does,
> then 'getClass()' will not be available, or else the class is already
> loaded and initialized and there's no need. =A0For this goal one pretty
> much needs a class name, but it can be read from an external source
> rather than hard coded.
>
> If the goal is to get the current class's 'Class' instance, then
> either 'getClass()', as you say, or
> 'SelectTourValidationErrorsTest.class' yields it. =A0The latter is
> necessary in a static context.
>
> --
> Lew
To give this thread some closure, Lothar correctly answered my
question, as it was literally asked. Lew gave an even more concise
answer and I did end up using
Class<?> c =3D this.getClass();
Just that easy! Thanks, - Dave
|
|
0
|
|
|
|
Reply
|
laredotornado
|
12/3/2009 3:21:37 PM
|
|
|
9 Replies
122 Views
(page loaded in 0.123 seconds)
|