由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - get full class name
相关主题
我想把一个Arraylist转成String[]Java中如何动态生成对象
Help! ClassCastExceptioncom.sun.tools.javac.Main 问题
[合集] How to get all classes under a package?Conditional import in Java
java这个是什么逻辑?这个是JSP的什么技术
help "java.lang.NoSuchMethodError"classpath 和 lib/ext 的区别?
interestinggenerics这样改对马?
Help!: tomcat classloading problem新手问问这个错在哪儿?
Re: Help!: tomcat classloading problemgetBytes() 卡住了 求助
相关话题的讨论汇总
话题: class话题: string话题: name话题: import话题: get
进入Java版参与讨论
1 (共1页)
j******r
发帖数: 201
1
how to dynamically get the full class name? using reflect?
for example, from "String" get "java.lang.String"
xt
发帖数: 17532
2

getClass().getName()

【在 j******r 的大作中提到】
: how to dynamically get the full class name? using reflect?
: for example, from "String" get "java.lang.String"

j******r
发帖数: 201
3
no, I dont have the class. Only have the String "String".
In other words, how to navigate the java class hierarchy.

【在 xt 的大作中提到】
:
: getClass().getName()

xt
发帖数: 17532
4

You only have the result of the Object.toString()? Then you
are helpless.

【在 j******r 的大作中提到】
: no, I dont have the class. Only have the String "String".
: In other words, how to navigate the java class hierarchy.

j******r
发帖数: 201
5
i am thinking in Java compiler, after you import the packages,
You can define your variables only by using the "short" class name.
For example:
import java.net.*;
...
Socket s = ....
And the compiler knows that the "Socket" is "java.net.Socket".
So there should be a way to find out the fully qualified name
of "Socket".

【在 xt 的大作中提到】
:
: You only have the result of the Object.toString()? Then you
: are helpless.

xt
发帖数: 17532
6

I see.
The trick is to search through all the import defined in the java file,
plus package java.lang.*. When you use short name of classes, the name
*must* be unique across the imported classes, or you will have compile
time error.
For example, it won't compile if you do:
import java.sql.*;
import java.util.*;
...
Date date = new Date();
...
This is because the compiler cannot tell if it is java.sql.Date or
java.util.Date.
This is why for better programming styles, you should limit your
import

【在 j******r 的大作中提到】
: i am thinking in Java compiler, after you import the packages,
: You can define your variables only by using the "short" class name.
: For example:
: import java.net.*;
: ...
: Socket s = ....
: And the compiler knows that the "Socket" is "java.net.Socket".
: So there should be a way to find out the fully qualified name
: of "Socket".

j******r
发帖数: 201
7
En,
Thank you.

【在 xt 的大作中提到】
:
: I see.
: The trick is to search through all the import defined in the java file,
: plus package java.lang.*. When you use short name of classes, the name
: *must* be unique across the imported classes, or you will have compile
: time error.
: For example, it won't compile if you do:
: import java.sql.*;
: import java.util.*;
: ...

m****r
发帖数: 11
8
String.class.getName()

【在 j******r 的大作中提到】
: En,
: Thank you.

qm
发帖数: 27
9
the following code can find out the full name if it's already loaded. but it
fails if the class is not loaded.
this question is not useful in practice. but it helps to understand some basic
concepts.
note that this code assumes the system default class loader is used.
public class test {
public static void main (String[] args) throws Exception {
class MyClassLoader extends ClassLoader {
public MyClassLoader (ClassLoader parent) {
super(parent);
}
public String get

【在 j******r 的大作中提到】
: no, I dont have the class. Only have the String "String".
: In other words, how to navigate the java class hierarchy.

1 (共1页)
进入Java版参与讨论
相关主题
getBytes() 卡住了 求助help "java.lang.NoSuchMethodError"
如何获得类定义的public methods?interesting
也问一个Eclipse的问题Help!: tomcat classloading problem
请教一个有关 inner class 的问题 Re: Help!: tomcat classloading problem
我想把一个Arraylist转成String[]Java中如何动态生成对象
Help! ClassCastExceptioncom.sun.tools.javac.Main 问题
[合集] How to get all classes under a package?Conditional import in Java
java这个是什么逻辑?这个是JSP的什么技术
相关话题的讨论汇总
话题: class话题: string话题: name话题: import话题: get