1 /* 2 * Javassist, a Java-bytecode translator toolkit. 3 * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 * 5 * The contents of this file are subject to the Mozilla Public License Version 6 * 1.1 (the "License"); you may not use this file except in compliance with 7 * the License. Alternatively, the contents of this file may be used under 8 * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 * or the Apache License Version 2.0. 10 * 11 * Software distributed under the License is distributed on an "AS IS" basis, 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 * for the specific language governing rights and limitations under the 14 * License. 15 */ 16 17 package javassist.compiler.ast; 18 19 import javassist.compiler.CompileError; 20 import javassist.compiler.TokenId; 21 22 /** 23 * Variable declarator. 24 */ 25 public class Declarator extends ASTList implements TokenId { 26 /** default serialVersionUID */ 27 private static final long serialVersionUID = 1L; 28 protected int varType; 29 protected int arrayDim; 30 protected int localVar; 31 protected String qualifiedClass; // JVM-internal representation 32 Declarator(int type, int dim)33 public Declarator(int type, int dim) { 34 super(null); 35 varType = type; 36 arrayDim = dim; 37 localVar = -1; 38 qualifiedClass = null; 39 } 40 Declarator(ASTList className, int dim)41 public Declarator(ASTList className, int dim) { 42 super(null); 43 varType = CLASS; 44 arrayDim = dim; 45 localVar = -1; 46 qualifiedClass = astToClassName(className, '/'); 47 } 48 49 /* For declaring a pre-defined? local variable. 50 */ Declarator(int type, String jvmClassName, int dim, int var, Symbol sym)51 public Declarator(int type, String jvmClassName, int dim, 52 int var, Symbol sym) { 53 super(null); 54 varType = type; 55 arrayDim = dim; 56 localVar = var; 57 qualifiedClass = jvmClassName; 58 setLeft(sym); 59 append(this, null); // initializer 60 } 61 make(Symbol sym, int dim, ASTree init)62 public Declarator make(Symbol sym, int dim, ASTree init) { 63 Declarator d = new Declarator(this.varType, this.arrayDim + dim); 64 d.qualifiedClass = this.qualifiedClass; 65 d.setLeft(sym); 66 append(d, init); 67 return d; 68 } 69 70 /* Returns CLASS, BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT, 71 * or DOUBLE (or VOID) 72 */ getType()73 public int getType() { return varType; } 74 getArrayDim()75 public int getArrayDim() { return arrayDim; } 76 addArrayDim(int d)77 public void addArrayDim(int d) { arrayDim += d; } 78 getClassName()79 public String getClassName() { return qualifiedClass; } 80 setClassName(String s)81 public void setClassName(String s) { qualifiedClass = s; } 82 getVariable()83 public Symbol getVariable() { return (Symbol)getLeft(); } 84 setVariable(Symbol sym)85 public void setVariable(Symbol sym) { setLeft(sym); } 86 getInitializer()87 public ASTree getInitializer() { 88 ASTList t = tail(); 89 if (t != null) 90 return t.head(); 91 return null; 92 } 93 setLocalVar(int n)94 public void setLocalVar(int n) { localVar = n; } 95 getLocalVar()96 public int getLocalVar() { return localVar; } 97 98 @Override getTag()99 public String getTag() { return "decl"; } 100 101 @Override accept(Visitor v)102 public void accept(Visitor v) throws CompileError { 103 v.atDeclarator(this); 104 } 105 astToClassName(ASTList name, char sep)106 public static String astToClassName(ASTList name, char sep) { 107 if (name == null) 108 return null; 109 110 StringBuffer sbuf = new StringBuffer(); 111 astToClassName(sbuf, name, sep); 112 return sbuf.toString(); 113 } 114 astToClassName(StringBuffer sbuf, ASTList name, char sep)115 private static void astToClassName(StringBuffer sbuf, ASTList name, 116 char sep) { 117 for (;;) { 118 ASTree h = name.head(); 119 if (h instanceof Symbol) 120 sbuf.append(((Symbol)h).get()); 121 else if (h instanceof ASTList) 122 astToClassName(sbuf, (ASTList)h, sep); 123 124 name = name.tail(); 125 if (name == null) 126 break; 127 128 sbuf.append(sep); 129 } 130 } 131 } 132