package org.eclipse.core.internal.expressions;
import org.eclipse.core.expressions.IPropertyTester;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
public class TypeExtension {
private static final TypeExtension[] EMPTY_TYPE_EXTENSION_ARRAY= new TypeExtension[0];
static final IPropertyTester CONTINUE= new IPropertyTester() {
@Override
public boolean handles(String namespace, String method) {
return false;
}
@Override
public boolean isInstantiated() {
return true;
}
@Override
public boolean isDeclaringPluginActive() {
return true;
}
@Override
public IPropertyTester instantiate() {
return this;
}
@Override
public boolean test(Object receiver, String method, Object[] args, Object expectedValue) {
return false;
}
};
private static final TypeExtension END_POINT= new TypeExtension() {
@Override
IPropertyTester findTypeExtender(TypeExtensionManager manager, String namespace, String name, boolean staticMethod, boolean forcePluginActivation) throws CoreException {
return CONTINUE;
}
};
private Class<?> fType;
private IPropertyTester[] fExtenders;
private TypeExtension fExtends;
private TypeExtension[] fImplements;
private TypeExtension() {
}
TypeExtension(Class<?> type) {
Assert.isNotNull(type);
fType= type;
}
IPropertyTester findTypeExtender(TypeExtensionManager manager, String namespace, String method, boolean staticMethod, boolean forcePluginActivation) throws CoreException {
if (fExtenders == null) {
fExtenders= manager.loadTesters(fType);
}
IPropertyTester result;
for (int i= 0; i < fExtenders.length; i++) {
IPropertyTester extender= fExtenders[i];
if (extender == null || !extender.handles(namespace, method))
continue;
if (extender.isInstantiated()) {
return extender;
} else {
if (extender.isDeclaringPluginActive() || forcePluginActivation) {
try {
PropertyTesterDescriptor descriptor= (PropertyTesterDescriptor)extender;
IPropertyTester inst= descriptor.instantiate();
((PropertyTester)inst).internalInitialize(descriptor);
fExtenders[i]= extender= inst;
return extender;
} catch (CoreException e) {
fExtenders[i]= null;
throw e;
} catch (ClassCastException e) {
fExtenders[i]= null;
throw new CoreException(new ExpressionStatus(
ExpressionStatus.TYPE_EXTENDER_INCORRECT_TYPE,
ExpressionMessages.TypeExtender_incorrectType,
e));
}
} else {
return extender;
}
}
}
if (staticMethod)
return CONTINUE;
if (fExtends == null) {
Class<?> superClass= fType.getSuperclass();
if (superClass != null) {
fExtends= manager.get(superClass);
} else {
fExtends= END_POINT;
}
}
result= fExtends.findTypeExtender(manager, namespace, method, staticMethod, forcePluginActivation);
if (result != CONTINUE)
return result;
if (fImplements == null) {
Class<?>[] interfaces= fType.getInterfaces();
if (interfaces.length == 0) {
fImplements= EMPTY_TYPE_EXTENSION_ARRAY;
} else {
fImplements= new TypeExtension[interfaces.length];
for (int i= 0; i < interfaces.length; i++) {
fImplements[i]= manager.get(interfaces[i]);
}
}
}
for (TypeExtension fImplement : fImplements) {
result= fImplement.findTypeExtender(manager, namespace, method, staticMethod, forcePluginActivation);
if (result != CONTINUE)
return result;
}
return CONTINUE;
}
}