KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > expressions > TypeExtension


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.expressions;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15
16 import org.eclipse.core.expressions.IPropertyTester;
17 import org.eclipse.core.expressions.PropertyTester;
18
19 public class TypeExtension {
20     
21     private static final TypeExtension[] EMPTY_TYPE_EXTENSION_ARRAY= new TypeExtension[0];
22
23     /* a special property tester instance that is used to signal that method searching has to continue */
24     /* package */ static final IPropertyTester CONTINUE= new IPropertyTester() {
25         public boolean handles(String JavaDoc namespace, String JavaDoc method) {
26             return false;
27         }
28         public boolean isInstantiated() {
29             return true;
30         }
31         public boolean isDeclaringPluginActive() {
32             return true;
33         }
34         public IPropertyTester instantiate() {
35             return this;
36         }
37         public boolean test(Object JavaDoc receiver, String JavaDoc method, Object JavaDoc[] args, Object JavaDoc expectedValue) {
38             return false;
39         }
40     };
41         
42     /* a special type extension instance that marks the end of an evaluation chain */
43     private static final TypeExtension END_POINT= new TypeExtension() {
44         /* package */ IPropertyTester findTypeExtender(TypeExtensionManager manager, String JavaDoc namespace, String JavaDoc name, boolean staticMethod, boolean forcePluginActivation) throws CoreException {
45             return CONTINUE;
46         }
47     };
48         
49     /* the type this extension is extending */
50     private Class JavaDoc fType;
51     /* the list of associated extenders */
52     private IPropertyTester[] fExtenders;
53     
54     /* the extension associated with <code>fType</code>'s super class */
55     private TypeExtension fExtends;
56     /* the extensions associated with <code>fTypes</code>'s interfaces */
57     private TypeExtension[] fImplements;
58     
59     private TypeExtension() {
60         // special constructor to create the CONTINUE instance
61
}
62     
63     /* package */ TypeExtension(Class JavaDoc type) {
64         Assert.isNotNull(type);
65         fType= type;
66     }
67     
68     /* package */ IPropertyTester findTypeExtender(TypeExtensionManager manager, String JavaDoc namespace, String JavaDoc method, boolean staticMethod, boolean forcePluginActivation) throws CoreException {
69         if (fExtenders == null) {
70             fExtenders= manager.loadTesters(fType);
71         }
72         IPropertyTester result;
73         
74         // handle extenders associated with this type extender
75
for (int i= 0; i < fExtenders.length; i++) {
76             IPropertyTester extender= fExtenders[i];
77             if (extender == null || !extender.handles(namespace, method))
78                 continue;
79             if (extender.isInstantiated()) {
80                 // There is no need to check for an active plug-in here. If a plug-in
81
// gets uninstalled we receive an registry event which will flush the whole
82
// type extender cache and will reinstantiate the testers. However Bundle#stop
83
// isn't handled by this. According to bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=130338
84
// we don't have to support stop in 3.2. If we have to in the future we have to
85
// reactivate the stopped plug-in if we are in forcePluginActivation mode.
86
return extender;
87             } else {
88                 if (extender.isDeclaringPluginActive() || forcePluginActivation) {
89                     try {
90                         PropertyTesterDescriptor descriptor= (PropertyTesterDescriptor)extender;
91                         IPropertyTester inst= descriptor.instantiate();
92                         ((PropertyTester)inst).internalInitialize(descriptor);
93                         fExtenders[i]= extender= inst;
94                         return extender;
95                     } catch (CoreException e) {
96                         fExtenders[i]= null;
97                         throw e;
98                     } catch (ClassCastException JavaDoc e) {
99                         fExtenders[i]= null;
100                         throw new CoreException(new ExpressionStatus(
101                             ExpressionStatus.TYPE_EXTENDER_INCORRECT_TYPE,
102                             ExpressionMessages.TypeExtender_incorrectType,
103                             e));
104                     }
105                 } else {
106                     return extender;
107                 }
108             }
109         }
110         
111         // there is no inheritance for static methods
112
if (staticMethod)
113             return CONTINUE;
114         
115         // handle extends chain
116
if (fExtends == null) {
117             Class JavaDoc superClass= fType.getSuperclass();
118             if (superClass != null) {
119                 fExtends= manager.get(superClass);
120             } else {
121                 fExtends= END_POINT;
122             }
123         }
124         result= fExtends.findTypeExtender(manager, namespace, method, staticMethod, forcePluginActivation);
125         if (result != CONTINUE)
126             return result;
127         
128         // handle implements chain
129
if (fImplements == null) {
130             Class JavaDoc[] interfaces= fType.getInterfaces();
131             if (interfaces.length == 0) {
132                 fImplements= EMPTY_TYPE_EXTENSION_ARRAY;
133             } else {
134                 fImplements= new TypeExtension[interfaces.length];
135                 for (int i= 0; i < interfaces.length; i++) {
136                     fImplements[i]= manager.get(interfaces[i]);
137                 }
138             }
139         }
140         for (int i= 0; i < fImplements.length; i++) {
141             result= fImplements[i].findTypeExtender(manager, namespace, method, staticMethod, forcePluginActivation);
142             if (result != CONTINUE)
143                 return result;
144         }
145         return CONTINUE;
146     }
147 }
148
Popular Tags