選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Declarator.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. package javassist.compiler.ast;
  17. import javassist.compiler.CompileError;
  18. import javassist.compiler.TokenId;
  19. /**
  20. * Variable declarator.
  21. */
  22. public class Declarator extends ASTList implements TokenId {
  23. /** default serialVersionUID */
  24. private static final long serialVersionUID = 1L;
  25. protected int varType;
  26. protected int arrayDim;
  27. protected int localVar;
  28. protected String qualifiedClass; // JVM-internal representation
  29. public Declarator(int type, int dim, int lineNumber) {
  30. super(null, lineNumber);
  31. varType = type;
  32. arrayDim = dim;
  33. localVar = -1;
  34. qualifiedClass = null;
  35. }
  36. public Declarator(ASTList className, int dim, int lineNumber) {
  37. super(null, lineNumber);
  38. varType = CLASS;
  39. arrayDim = dim;
  40. localVar = -1;
  41. qualifiedClass = astToClassName(className, '/');
  42. }
  43. /* For declaring a pre-defined? local variable.
  44. */
  45. public Declarator(int type, String jvmClassName, int dim,
  46. int var, Symbol sym, int lineNumber) {
  47. super(null, lineNumber);
  48. varType = type;
  49. arrayDim = dim;
  50. localVar = var;
  51. qualifiedClass = jvmClassName;
  52. setLeft(sym);
  53. append(this, null, lineNumber); // initializer
  54. }
  55. public Declarator make(Symbol sym, int dim, ASTree init, int lineNumber) {
  56. Declarator d = new Declarator(this.varType, this.arrayDim + dim, lineNumber);
  57. d.qualifiedClass = this.qualifiedClass;
  58. d.setLeft(sym);
  59. append(d, init, lineNumber);
  60. return d;
  61. }
  62. /* Returns CLASS, BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT,
  63. * or DOUBLE (or VOID)
  64. */
  65. public int getType() { return varType; }
  66. public int getArrayDim() { return arrayDim; }
  67. public void addArrayDim(int d) { arrayDim += d; }
  68. public String getClassName() { return qualifiedClass; }
  69. public void setClassName(String s) { qualifiedClass = s; }
  70. public Symbol getVariable() { return (Symbol)getLeft(); }
  71. public void setVariable(Symbol sym) { setLeft(sym); }
  72. public ASTree getInitializer() {
  73. ASTList t = tail();
  74. if (t != null)
  75. return t.head();
  76. return null;
  77. }
  78. public void setLocalVar(int n) { localVar = n; }
  79. public int getLocalVar() { return localVar; }
  80. @Override
  81. public String getTag() { return "decl"; }
  82. @Override
  83. public void accept(Visitor v) throws CompileError {
  84. v.atDeclarator(this);
  85. }
  86. public static String astToClassName(ASTList name, char sep) {
  87. if (name == null)
  88. return null;
  89. StringBuilder sbuf = new StringBuilder();
  90. astToClassName(sbuf, name, sep);
  91. return sbuf.toString();
  92. }
  93. private static void astToClassName(StringBuilder sbuf, ASTList name,
  94. char sep) {
  95. for (;;) {
  96. ASTree h = name.head();
  97. if (h instanceof Symbol)
  98. sbuf.append(((Symbol)h).get());
  99. else if (h instanceof ASTList)
  100. astToClassName(sbuf, (ASTList)h, sep);
  101. name = name.tail();
  102. if (name == null)
  103. break;
  104. sbuf.append(sep);
  105. }
  106. }
  107. }