关于符号Symbol第二篇
?
來看一下繼承自Symbol的具體實現類。
1、TypeSymbol
/** A class for type symbols.* Type variables are represented by instances of this class, // 類型變量用這個類來表示* classes and packages by instances of subclasses. // 類和包用子類來表示*/ public class TypeSymbol extends Symbol implements TypeParameterElement {... }其中有兩個重要的方法是用來計算fullname與flatname的。TypeSymbol的具體子類有PackageSymbol和ClassSymbol,而PackageSymbol中含有fullname屬性,ClassSymbol中含有fullname與flatname屬性。
接著看具體的子類定義。
2、PackageSymbol
?
/** A class for package symbols*/ public class PackageSymbol extends TypeSymbol implements PackageElement {public Scope members_field;public Name fullname;public ClassSymbol package_info; // see bug 6443073... }主要有3個屬性。
?
3、ClassSymbol
/** A class for class symbols*/ public class ClassSymbol extends TypeSymbol implements TypeElement {/** a scope for all class members; variables, methods and inner classes* type parameters are not part of this scope* * 所有類成員的一個作用域。變量,方法和內部類 類型參數不是這個作用域的一部分*/public Scope members_field;/** the fully qualified name of the class, i.e. pck.outer.inner.* null for anonymous classes* * 類的全限定名,對于匿名類為空*/public Name fullname;/** the fully qualified name of the class after converting to flat* representation, i.e. pck.outer$inner,* set externally for local and anonymous classes* * 類的全限定名,為本地和匿名類也進行設置*/public Name flatname;/** the sourcefile where the class came from*/public JavaFileObject sourcefile;/** the classfile from where to load this class* this will have extension .class or .java*/public JavaFileObject classfile;/** the list of translated local classes (used for generating InnerClasses attribute)*/public List<ClassSymbol> trans_local;/** the constant pool of the class*/public Pool pool;... }?
下面就來看一個具體的例子,如下:
package m20170210;public class A {class B {}public void test() {class C {}} }?
1、屬性fullname與flatname
下面為PackageSymbol的截圖。
下面分別是A類,B類與C類的ClassSymbol截圖。
?
結合TypeSymbol相關的計算代碼,如下:
/** form a fully qualified name from a name and an owner*/static public Name formFullName(Name name, Symbol owner) {if (owner == null){return name;}if ((owner.kind != ERR) &&((owner.kind & (VAR | MTH)) != 0 ||(owner.kind == TYP && owner.type.tag == TYPEVAR) //TYPEVAR是類型變量)){return name;}Name prefix = owner.getQualifiedName();if (prefix == null || prefix == prefix.table.names.empty){return name;} else{return prefix.append('.', name);}}/** form a fully qualified name from a name and an owner, after* converting to flat representation*/static public Name formFlatName(Name name, Symbol owner) {if (owner == null ||(owner.kind & (VAR | MTH)) != 0 || // 是變量或者方法(owner.kind == TYP && owner.type.tag == TYPEVAR) // 是一個類型的類型變量){return name;}char sep = (owner.kind == TYP) ? '$' : '.';Name prefix = owner.flatName();if (prefix == null || prefix == prefix.table.names.empty){return name;}else{return prefix.append(sep, name);}}?
flatname主要用來為各個類生成相應的class文件時進行命名的。fullname就是一般所要求的qualifiedname。
?
2、屬性sourcefile與classfile
sourcefile表示源文件的位置,而classfile表示生成的class類的文件路徑。
為什么本地類C的classfile為空呢?
?
3、屬性members_field
?這個屬性只有PackageSymbol與ClassSymbol含有,其類型是Scope(作用域)。Scope內容將在后面詳細講解。
?
4、重要的方法
有一個isLocal()方法,具體的實現在Symbol中,如下:
/** Is this symbol declared (directly or indirectly) local to a method or variable initializer?* Also includes fields of inner classes which are in turn(反過來) local to a method or variable initializer.*/public boolean isLocal() { // return // (owner.kind & (VAR | MTH)) != 0 || // (owner.kind == TYP && owner.isLocal());if((owner.kind & MTH ) != 0 ){System.out.println(flatName()+"/owner.kind=MTH");return true;}else if((owner.kind & VAR)!=0){System.out.println(flatName()+"/owner.kind=VAR");return true;}else if(owner.kind == TYP && owner.isLocal()){System.out.println(flatName()+"/owner.kind=TYP");return true;}System.out.println(flatName()+"/false");return false;}為了方便測試和打印,我對原來的代碼進行了實現的修改并且增加了一些打印,下面是一個例子。
package m20170217;public class A { // m20170217.A/falseclass B { // m20170217.A$B/falseB b = new B() { }; // m20170217.A$B$1/owner.kind=VAR}B c = new B() { }; // m20170217.A$1/owner.kind=VAR{class C { } // m20170217.A$1C/owner.kind=MTH}public void test1() {class D { } // m20170217.A$1D/owner.kind=MTHD d = new D() { }; // m20170217.A$2/owner.kind=MTH} }?從如上例子打印結果也可以看出,只有A與B類是非local的,其它都是local。看C類,它的owner.kin為MTH,表示匿名塊在編譯處理過程中是當作匿名方法來處理的。
?
4、MethodSymbol
?
/** The code of the method. */public Code code = null;/** The parameters of the method. */public List<VarSymbol> params = null;/** The names of the parameters */public List<Name> savedParameterNames;/** For an attribute field accessor, its default value if any.* The value is null if none appeared in the method declaration.** 對于屬性字段訪問器,其默認值(如果有)。*/public Attribute defaultValue = null; // Attribute類表示的是注解的值
1、屬性params
?舉個例子,如下:?
public class B {@HelloWorldAnnotation(name = "小明")public Integer test(int a, String name, Object c) {return 1;} }查看MethodSymbol,如下截圖:
?
2、屬性defaultValue
@Retention(RetentionPolicy.RUNTIME) // 注解會在class中存在,運行時可通過反射獲取 @Target(ElementType.METHOD) // 目標是方法 @Documented // 文檔生成時,該注解將被包含在javadoc中,可去掉 public @interface HelloWorldAnnotation {public String name() default "xxxxx";}查看MethodSymbol,如下截圖:
?
5、OperatorSymbol
/** A class for predefined operators.** OperatorSymbol繼承MethodSymbol是由于操作符有操作數和返回類型,和方法參數與返回類型相似*/ public class OperatorSymbol extends MethodSymbol {public int opcode;public OperatorSymbol(Name name, Type type, int opcode, Symbol owner) {super(PUBLIC | STATIC, name, type, owner);this.opcode = opcode;}public <R, P> R accept(Symbol.Visitor<R, P> v, P p) {return v.visitOperatorSymbol(this, p);} }OperatorSymbol繼承自MethodSymbol。其中有個opcode屬性,表示操作的字節碼指令。這都是JVM先前定義好的,如:0x03表示將int類型0推送至棧頂。
6、VarSymbol
/** A class for variable symbols*/ public class VarSymbol extends Symbol implements VariableElement {public int pos = Position.NOPOS;/** The variable's address. Used for different purposes during* flow analysis, translation and code generation.** Flow analysis:* If this is a blank final or local variable, its sequence number.* Translation:* If this is a private field, its access number.* Code generation:* If this is a local variable, its logical slot number.*/public int adr = -1; }其中的adr屬性還有待繼續研究。
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/extjs4/p/6383968.html
總結
以上是生活随笔為你收集整理的关于符号Symbol第二篇的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云主机的公网带宽和私网带宽的介绍
- 下一篇: android粘贴,Android复制粘