how do I make Class.forName("Integer") returning java.lang.Integer?

J

Johannes Zellner

Hello,

How do I make Class.forName("Integer") returning java.lang.Integer?
To be more general:

1. How do I make Class.forName searching in some packages?


2. Can I make Class.forName respecting the import statements of the
calling class? -- e.g. in

import java.lang.Integer;
class Fred() {
Fred() {
Class.forName("Integer");
}
}

I'd like Class.forName() returning java.lang.Integer, because it
was imported earlier.

Any help much appreciated!
 
S

Stefan Ram

Johannes Zellner said:
How do I make Class.forName("Integer") returning java.lang.Integer?

"java.lang.Integer" is a Class.
A class is not a value, so it can not be returned.

To make "Class.forName" return the string
»"java.lang.Integer"«:

public class Class
{ public static java.lang.String forName
( final java.lang.Object dummy )
{ return "java.lang.Integer"; }}

(Sorry!)
 
J

J. Verdrengh

"java.lang.Integer" is a Class.
A class is not a value, so it can not be returned.

Afaik each class in Java is represented by an instance of type Class, so a
class (==instance of Class) can be returned..
 
J

J. Verdrengh

"java.lang.Integer" is a Class.
A class is not a value, so it can not be returned.

Afaik each class in Java is represented by an instance of type Class, so a
class (==instance of Class) can be returned..
 
S

Stefan Ram

J. Verdrengh said:
Afaik each class in Java is represented by an instance of type Class,
so a class (==instance of Class) can be returned..

This, I would have written as »java.lang.Integer.class«.
 
S

Stefan Ram

Alan Krueger said:
Not if you were calling Class.forName.

The OP wrote:

How do I make Class.forName("Integer") returning
java.lang.Integer?
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
So "java.lang.Integer" was intended to be the value
of this expression, not the argument.
 
R

Roedy Green

"java.lang.Integer" is a Class.
A class is not a value, so it can not be returned.

java.lang.Integer is the name of a class

"java.lang.Integer" is a String, and hence an object.

java.lang.Integer.class is the Class object for Integer.

Strings and Class objects can be returned from methods.
 
R

ricky.clarkson

If you want to find the class that represents java.lang.Integer, you
can do this with:

Class<Integer> integerClass=Integer.class; (I might have got the thing
between <> wrong).

If you don't mind fully qualifying, you can do
Class.forName("java.lang.Integer");.

If you want to find all classes on the classpath called Integer, you
can iterate through the entries in the classpath via the system
classloader, which happens to be a URLClassLoader, normally.

Otherwise, state your requirements.
 
S

Stefan Ram

Roedy Green said:
How do I make Class.forName("Integer") returning
java.lang.Integer?

The method "forName" of the class "java.lang.Class"
expects fully qualified class names, i.e., names
preceded by a package.

However, a class "Class" could be written to implement
the behavior required.
 
S

Stefan Ram

Roedy Green said:
There already is a Class class.

Actually a class "java.lang.Class" - but not a class "Class"
in an unnamed package.

Without certain import declarations, the identifier "Class"
refers to the class "Class" of an unnamed package - should
such a class declaration exist.
 
C

Chris Smith

Stefan Ram said:
Without certain import declarations, the identifier "Class"
refers to the class "Class" of an unnamed package - should
such a class declaration exist.

Are you sure of that last bit?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Stefan Ram

Chris Smith said:
Are you sure of that last bit?

I am never sure about anything.

What I had I mind was:

class Class
{ final static java.lang.Class forName
( final java.lang.String name )
{ return java.lang.Double.class; }}

public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println
( Class.forName( "java.lang.Integer" ).getName() ); }}

This compiles and prints:

java.lang.Double
 
C

Chris Smith

Stefan Ram said:
I am never sure about anything.

What I had I mind was:

Yes. What doesn't work is using an import to get the default-package
Class to be used in preference to the java.lang.Class, from code that
resides outside the default package. If you are in the default package,
then this works and doesn't require an import at all. In other named
packages, you could make it work by placing Class in the same package as
the code. But you cannot import something from the default package.

That's all I meant.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Johannes Zellner

If you want to find the class that represents java.lang.Integer, you
can do this with:

Class<Integer> integerClass=Integer.class; (I might have got the thing
between <> wrong).

If you don't mind fully qualifying, you can do
Class.forName("java.lang.Integer");.

If you want to find all classes on the classpath called Integer, you
can iterate through the entries in the classpath via the system
classloader, which happens to be a URLClassLoader, normally.

the last on is excactly what I'd like to do. But HOW do I do this?
I can easily get all the packages from the system class loader by

ClassLoader.getSystemClassLoader().getPackages()

but how can I get the class names each package provides?
 
R

Roedy Green

Without certain import declarations, the identifier "Class"
refers to the class "Class" of an unnamed package - should
such a class declaration exist.

I would not try naming classes the same as those in java.lang. Even
if you don't confuse the compiler, you will confuse anyone reading the
code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top