c*****t 发帖数: 1879 | 1 It is interesting that Java allows $ in the class name and thus you
can do the following:
In T.java
public class T
{
static class O
{
public static void run (String msg) { }
}
public static void main (String[] args)
{
T.O.run ("over");
}
}
Then in T$O.java
class T$O
{
public static void run (String msg) { System.out.println (msg); }
}
Compile T.java first, then T$O.java.
They probably allowed it so that some |
g*****g 发帖数: 34805 | 2 I think it's allowed to have non-public class.
e.g.
In T.java
You have
public class T{}
class P{}
P will be compiled to be like T$P.class
【在 c*****t 的大作中提到】 : It is interesting that Java allows $ in the class name and thus you : can do the following: : In T.java : public class T : { : static class O : { : public static void run (String msg) { } : } : public static void main (String[] args)
|
s***e 发帖数: 122 | 3 it's actually for nested class. in your case, it would still be P.class.
【在 g*****g 的大作中提到】 : I think it's allowed to have non-public class. : e.g. : In T.java : You have : public class T{} : class P{} : P will be compiled to be like T$P.class
|
s***e 发帖数: 122 | 4 it's a trick, and i believe it's better to not allow $ in class name in the
source code.
【在 c*****t 的大作中提到】 : It is interesting that Java allows $ in the class name and thus you : can do the following: : In T.java : public class T : { : static class O : { : public static void run (String msg) { } : } : public static void main (String[] args)
|
s******n 发帖数: 876 | 5 it's not encouraged to have $ in identifiers in source code.
machine generated source codes don't need $ either; in your
example, simply nest O within T's source file.
byte codes on the other hand much use $ in proper places
according to the spec.
【在 c*****t 的大作中提到】 : It is interesting that Java allows $ in the class name and thus you : can do the following: : In T.java : public class T : { : static class O : { : public static void run (String msg) { } : } : public static void main (String[] args)
|
c*****t 发帖数: 1879 | 6 I actually needed this trick for the following task:
User define and compile a class T (along with nested classes). Then
my code scans T's nested classes (or trying to do so). Then replace
the nested classes with additional functionalities.
Sort of like AspectJ. Doing post-compile modification, except I am
generating Java code rather than binary class directly.
the
【在 s***e 的大作中提到】 : it's a trick, and i believe it's better to not allow $ in class name in the : source code.
|
s***e 发帖数: 122 | 7 i am curious why you would need to do so. it seems to me hacking.
【在 c*****t 的大作中提到】 : I actually needed this trick for the following task: : User define and compile a class T (along with nested classes). Then : my code scans T's nested classes (or trying to do so). Then replace : the nested classes with additional functionalities. : Sort of like AspectJ. Doing post-compile modification, except I am : generating Java code rather than binary class directly. : : the
|
c*****t 发帖数: 1879 | 8 See this example:
http://code.google.com/p/cookcc/source/browse/#svn/trunk/tests/javaap/nestedclass
And look at WC1.java and WC1$Lexer.java.orig (added .orig for backup
purpose). This WC1$Lexer.java would be replaced by code generated
from a compiler-compiler.
It is not a good approach in general I agree, but I do see that it
makes it possible to organize files.
【在 s***e 的大作中提到】 : i am curious why you would need to do so. it seems to me hacking.
|
s***e 发帖数: 122 | 9 interesting. i have to admit that i don't have a whole picture of your code.
but it seems that you don't have to make Lex1 or Nested nested class. code
generation is necessary sometimes, but ways other than overwriting nested
class might be easier. btw: the project itself looks really good.
【在 c*****t 的大作中提到】 : See this example: : http://code.google.com/p/cookcc/source/browse/#svn/trunk/tests/javaap/nestedclass : And look at WC1.java and WC1$Lexer.java.orig (added .orig for backup : purpose). This WC1$Lexer.java would be replaced by code generated : from a compiler-compiler. : It is not a good approach in general I agree, but I do see that it : makes it possible to organize files.
|