1. /*
  2. * Copyright 2000-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. package org.apache.tools.ant.util;
  18. import org.apache.tools.ant.BuildException;
  19. import java.lang.reflect.Constructor;
  20. /**
  21. * this is a weak reference on java1.2 and up, a hard
  22. * reference on java1.1
  23. * @since ant1.6
  24. */
  25. public abstract class WeakishReference {
  26. private static Constructor referenceConstructor;
  27. private final static String WEAK_REFERENCE_NAME
  28. = "org.apache.tools.ant.util.optional.WeakishReference12";
  29. /**
  30. * create the appropriate type of reference for the java version
  31. * @param object
  32. * @return reference to the Object.
  33. */
  34. public static WeakishReference createReference(Object object) {
  35. if (referenceConstructor == null) {
  36. createReferenceConstructor();
  37. }
  38. try {
  39. return (WeakishReference) referenceConstructor
  40. .newInstance(new Object[]{object});
  41. } catch (Exception e) {
  42. throw new BuildException("while creating a weakish reference", e);
  43. }
  44. }
  45. /**
  46. * create the appropriate constructor method for the
  47. */
  48. private static void createReferenceConstructor() {
  49. Class[] ctor = new Class[]{Object.class};
  50. try {
  51. referenceConstructor = HardReference.class.getConstructor(ctor);
  52. } catch (NoSuchMethodException e) {
  53. //deep trouble here
  54. throw new BuildException("when creating a Hard Reference constructor", e);
  55. }
  56. if (!JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
  57. //create a weak ref constructor. If this fails we have that hard one anyway
  58. try {
  59. Class clazz = Class.forName(WEAK_REFERENCE_NAME);
  60. referenceConstructor = clazz.getConstructor(ctor);
  61. } catch (ClassNotFoundException e) {
  62. // ignore
  63. } catch (NoSuchMethodException e) {
  64. // ignore
  65. }
  66. }
  67. }
  68. /**
  69. * Returns this reference object's referent. If this reference object has
  70. * been cleared, then this method returns <code>null</code>.
  71. *
  72. * @return The object to which this reference refers, or
  73. * <code>null</code> if this reference object has been cleared
  74. */
  75. public abstract Object get();
  76. /**
  77. * A hard reference for Java 1.1
  78. */
  79. public static class HardReference extends WeakishReference {
  80. private Object object;
  81. /**
  82. * construct
  83. * @param object
  84. */
  85. public HardReference(Object object) {
  86. this.object = object;
  87. }
  88. /**
  89. * Returns this reference object's referent.
  90. */
  91. public Object get() {
  92. return object;
  93. }
  94. }
  95. }