blob: 1d074279d308f052af0d3b51149693f16385ca14 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.e4.core.services.internal.context;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.e4.core.services.injector.IObjectDescriptor;
import org.eclipse.e4.core.services.injector.ObjectDescriptorFactory;
public class FieldRequestor extends Requestor {
final private Field field;
public FieldRequestor(Field field, Object requestingObject, boolean track,
boolean groupUpdates, boolean optional) {
super(requestingObject, track, groupUpdates, optional);
this.field = field;
}
public Object execute() throws InvocationTargetException, InstantiationException {
setField(field, actualArgs[0]);
return null;
}
@Override
public IObjectDescriptor[] getDependentObjects() {
InjectionProperties properties = annotationSupport.getInjectProperties(field);
IObjectDescriptor objectDescriptor = ObjectDescriptorFactory.make(field.getGenericType(),
properties.getPropertyName(), properties.isOptional());
return new IObjectDescriptor[] { objectDescriptor };
}
private boolean setField(Field field, Object value) {
Object userObject = getRequestingObject();
if (userObject == null)
return false;
boolean wasAccessible = true;
if (!field.isAccessible()) {
field.setAccessible(true);
wasAccessible = false;
}
try {
field.set(userObject, value);
} catch (IllegalArgumentException e) {
logError(field, e);
return false;
} catch (IllegalAccessException e) {
logError(field, e);
return false;
} finally {
if (!wasAccessible)
field.setAccessible(false);
}
return true;
}
}