1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: TestDriver.java,v 1.6 2004/02/17 04:23:24 minchau Exp $
  18. */
  19. /**
  20. * Test driver for com.sun.org.apache.xml.internal.utils.synthetic.Class and com.sun.org.apache.xml.internal.utils.synthetic.reflection.
  21. * <p>
  22. * toSource should probably be factored out into a separate
  23. * java generator class, so we could generate other languages as well.
  24. */
  25. package com.sun.org.apache.xml.internal.utils.synthetic;
  26. import com.sun.org.apache.xml.internal.utils.synthetic.reflection.Method;
  27. /**
  28. * Class TestDriver <needs-comment/>
  29. * @xsl.usage internal
  30. */
  31. public class TestDriver
  32. {
  33. /** Field sampleField */
  34. public static int sampleField = 32;
  35. /** Field inTest */
  36. private boolean inTest = false;
  37. /**
  38. * Method _main
  39. *
  40. *
  41. * @param args
  42. */
  43. public static void _main(String[] args)
  44. {
  45. // Proxy a class
  46. try
  47. {
  48. System.out.println("Proxying java.awt.Frame...");
  49. Class myC = Class.forName("java.awt.Frame");
  50. myC.toSource(System.out, 0);
  51. System.out.println(
  52. "\nProxying com.sun.org.apache.xml.internal.utils.synthetic.TestDriver...");
  53. myC =
  54. Class.forName("com.ibm.com.sun.org.apache.xml.internal.utils.synthetic.TestDriver");
  55. myC.toSource(System.out, 0);
  56. }
  57. catch (ClassNotFoundException e)
  58. {
  59. System.out.println("Couldn't proxy: ");
  60. e.printStackTrace();
  61. }
  62. // Start getting serious
  63. try
  64. {
  65. System.out.println("\nBuild a new beast...");
  66. Class myC = Class.declareClass(
  67. "com.ibm.com.sun.org.apache.xml.internal.utils.synthetic.BuildMe");
  68. Class inner = myC.declareInnerClass("island");
  69. inner.addExtends(Class.forName("java.lang.String"));
  70. Method m = inner.declareMethod("getValue");
  71. m.setReturnType(Class.forName("java.lang.String"));
  72. m.getBody().append("return toString();");
  73. myC.toSource(System.out, 0);
  74. }
  75. catch (ClassNotFoundException e)
  76. {
  77. e.printStackTrace();
  78. }
  79. catch (SynthesisException e)
  80. {
  81. e.printStackTrace();
  82. }
  83. catch (IllegalStateException e)
  84. {
  85. System.out.println("Unwritten function: " + e);
  86. e.printStackTrace();
  87. }
  88. }
  89. /**
  90. * Method dumpClass
  91. *
  92. *
  93. * @param C
  94. */
  95. public static void dumpClass(Class C)
  96. {
  97. System.out.println("toString(): " + C);
  98. System.out.println("\tisPrimitive(): " + C.isPrimitive());
  99. System.out.println("\tisInterface(): " + C.isInterface());
  100. System.out.println("\tisInstance(\"foo\"): " + C.isInstance("foo"));
  101. System.out.println("\tisArray(): " + C.isArray());
  102. System.out.println("\tgetRealClass(): " + C.getRealClass());
  103. }
  104. /**
  105. * Method quickcheck
  106. *
  107. */
  108. public void quickcheck()
  109. {
  110. Inner a = new Inner();
  111. a.setTest(!a.getTest());
  112. }
  113. /**
  114. * Class Inner <needs-comment/>
  115. * @xsl.usage internal
  116. */
  117. private class Inner
  118. {
  119. /**
  120. * Method getTest
  121. *
  122. *
  123. * @return
  124. */
  125. public boolean getTest()
  126. {
  127. return inTest;
  128. }
  129. /**
  130. * Method setTest
  131. *
  132. *
  133. * @param test
  134. */
  135. public void setTest(boolean test)
  136. {
  137. inTest = test;
  138. }
  139. }
  140. }