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