开发者

True java generics (templates)

After taking a look in the Java VM specification, I noticed that a lot more than just ASCII letters could be us开发者_运维知识库ed to create an identifier.

Firstly, I was wondering if there were any extra symbols (apart from $, that are available for identifiers)

Do you think it would be possible, with the extended character set to encode additional information in an identifier, and a custom classloader, to implement true Java generics?

Of course, you would have to get around type erasure, but that could be possible with a custom parser?

So you could store generic names in a format like: $g$GenericList$_Java_lang_String$

I'm using GenericList here as I don't intend to modify the original implementation!

Load them in with the class loader, create a proper GenericList<String> version and send it back.

EDIT: I plan to use this for a language I'm building on the JVM. As it uses $'s and _'s as special characters, encoding information like that might just work!

EDIT 2: I suppose the more difficult thing to do would be generic methods? Does anyone have any information on how those would be implemented?

EDIT 3: Since classes can only be unloaded when the classloader disappears, would I be able to cache and remove resolved templates like it works in .Net, or would I do it like C++?


The JVM allows any characters in class/field/method names except /, and ; which have a special meaning. Using numbers and other character is common for obfuscators to make de-compiling difficult.

However you could just use the $ and _ for generated class/fields/methods.

Note: JDK 7 is supposed to have better generic support with the Type with a combination of Class and generics.

EDIT:

One way to have proper generic type is to always use

Set<String> set = new LinkedHashSet<String>() { }; 

The use of { } creates an anonymous class which has a parent type with the generic you want. You can get this information via reflection.

You can cache and remove class by having your own class loader which you dispose of as you wish. The most extreme case would be to have a ClassLoader per class.

Once you have your own Generic types, you could just use these in your methods, like normal types.


As you can use Unicode you can basically use everything except the few letters mentioned in the previous answer (/,;). There is nothign like "true generics" btw ... I know what you mean ;D and that is called "Templates".


Yes you can use any unicode character as identifier name in java. See here for identifier names allowed in java. But as mentioned in previous answer, you mean "templates" for "true generics".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜