List of usage examples for org.apache.commons.lang3.reflect FieldUtils getField
public static Field getField(final Class<?> cls, final String fieldName, final boolean forceAccess)
From source file:org.evosuite.setup.TestAccessField.java
@Test public void testProtectedField() { Properties.CLASS_PREFIX = "some.package"; Properties.TARGET_CLASS = "some.package.Foo"; Field f = FieldUtils.getField(com.examples.with.different.packagename.AccessExamples.class, "protectedField", true); boolean result = TestUsageChecker.canUse(f); Assert.assertFalse(result);/* w w w. ja v a 2 s. c o m*/ }
From source file:org.evosuite.setup.TestAccessField.java
@Test public void testPrivateField() { Properties.CLASS_PREFIX = "some.package"; Properties.TARGET_CLASS = "some.package.Foo"; Field f = FieldUtils.getField(com.examples.with.different.packagename.AccessExamples.class, "privateField", true);/* ww w . j ava2 s.c o m*/ boolean result = TestUsageChecker.canUse(f); Assert.assertFalse(result); }
From source file:org.evosuite.setup.TestAccessField.java
@Test public void testDefaultFieldTargetPackage() { Properties.CLASS_PREFIX = "com.examples.with.different.packagename"; Properties.TARGET_CLASS = "com.examples.with.different.packagename.Foo"; Field f = FieldUtils.getField(com.examples.with.different.packagename.AccessExamples.class, "defaultField", true);/*from w w w . j a va 2 s .co m*/ boolean result = TestUsageChecker.canUse(f); Assert.assertTrue(result); }
From source file:org.evosuite.setup.TestAccessField.java
@Test public void testProtectedFieldTargetPackage() { Properties.CLASS_PREFIX = "com.examples.with.different.packagename"; Properties.TARGET_CLASS = "com.examples.with.different.packagename.Foo"; Field f = FieldUtils.getField(com.examples.with.different.packagename.AccessExamples.class, "protectedField", true); boolean result = TestUsageChecker.canUse(f); Assert.assertTrue(result);/* w w w. ja v a 2 s . c o m*/ }
From source file:org.evosuite.setup.TestAccessField.java
@Test public void testPrivateFieldTargetPackage() { Properties.CLASS_PREFIX = "com.examples.with.different.packagename"; Properties.TARGET_CLASS = "com.examples.with.different.packagename.Foo"; Field f = FieldUtils.getField(com.examples.with.different.packagename.AccessExamples.class, "privateField", true);/*w ww . jav a 2 s . c om*/ boolean result = TestUsageChecker.canUse(f); Assert.assertFalse(result); }
From source file:org.evosuite.testcarver.testcase.EvoTestCaseCodeGenerator.java
@Override public void createFieldReadAccessStmt(CaptureLog log, int logRecNo) { // assumption: all necessary statements are created and there is one variable for reach referenced object final int oid = log.objectIds.get(logRecNo); final int captureId = log.captureIds.get(logRecNo); final Object returnValue = log.returnValues.get(logRecNo); if (!CaptureLog.RETURN_TYPE_VOID.equals(returnValue)) // TODO necessary? {/*from w ww .ja v a 2s. c o m*/ Integer returnValueOID = (Integer) returnValue; // final String descriptor = log.descList.get(logRecNo); // final org.objectweb.asm.Type fieldTypeType = org.objectweb.asm.Type.getType(descriptor); final String typeName = log.getTypeName(oid); final String fieldName = log.getNameOfAccessedFields(captureId); try { // final Class<?> fieldType = getClassFromType(fieldTypeType); final Class<?> type = getClassForName(typeName); // final FieldReference valueRef = new FieldReference(testCase, // new GenericField(getDeclaredField(type, fieldName), type)); // final VariableReference targetVar = new VariableReferenceImpl(testCase, // fieldType); final FieldStatement fieldStatement = new FieldStatement(testCase, new GenericField(FieldUtils.getField(type, fieldName, true), type), this.oidToVarRefMap.get(oid)); //final AssignmentStatement assignment = new AssignmentStatement(testCase, // targetVar, valueRef); // VariableReference varRef = testCase.addStatement(assignment); VariableReference varRef = testCase.addStatement(fieldStatement); this.oidToVarRefMap.put(returnValueOID, varRef); } catch (final Exception e) { logger.debug("Error while trying to get field " + fieldName + " of class " + getClassForName(typeName) + ": " + e); CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating field read access stmt. Log: %s", logRecNo, log); } } }
From source file:org.force66.beantester.tests.ValuePropertyTest.java
@Override public boolean testProperty(Object bean, PropertyDescriptor descriptor, Object value) { Validate.notNull(bean, "Null bean not allowed"); Validate.notNull(descriptor, "Null PropertyDescriptor not allowed"); boolean answer = true; if (descriptor.getPropertyType().isPrimitive() && value == null) { return answer; // Null test doesn't apply }/*from ww w. jav a2s . com*/ boolean fieldExists = FieldUtils.getField(bean.getClass(), descriptor.getName(), true) != null; try { if (descriptor.getWriteMethod() != null) { descriptor.getWriteMethod().invoke(bean, new Object[] { value }); answer = testReadValue(bean, descriptor, value); } else if (fieldExists) { /* * Accessor exists, but no mutator. Test the accessor by forcing the test value into the field * backing that accessor. */ FieldUtils.writeField(bean, descriptor.getName(), value, true); answer = testReadValue(bean, descriptor, value); } if (descriptor.getReadMethod() != null) { /* * If an accessor exists, but has no corresponding mutator or field, all we can do * is execute it to make sure it doesn't except. */ descriptor.getReadMethod().invoke(bean); } } catch (Exception e) { throw new BeanTesterException("Failed executing assignment test for accessor/mutator", e) .addContextValue("property", descriptor) .addContextValue("value class", value == null ? null : value.getClass().getName()) .addContextValue("value", value); } return answer; }
From source file:org.kie.workbench.common.stunner.bpmn.backend.converters.fromstunner.properties.NotificationPropertyWriterTest.java
protected <T> T getFieldValue(Class parent, Object instance, String fieldName) { Field inputField = FieldUtils.getField(parent, fieldName, true); try {/* www .ja v a 2 s. co m*/ return (T) inputField.get(instance); } catch (IllegalAccessException e) { throw new Error(e); } }
From source file:org.ktc.soapui.maven.extension.impl.AbstractTestErrorHandler.java
private void initializeFailedTests() throws IllegalAccessException { List<TestCase> failedTests = new ArrayList<TestCase>(); failedTests.add(mock(TestCase.class)); Field field = FieldUtils.getField(SoapUIProTestCaseRunner.class, "failedTests", true); FieldUtils.writeField(field, runner, failedTests, true); }
From source file:org.ktc.soapui.maven.extension.impl.AbstractTestErrorHandler.java
private void initializeFailedAssertions() throws IllegalAccessException { List<TestAssertion> failedAssertions = new ArrayList<TestAssertion>(); failedAssertions.add(mock(TestAssertion.class)); Field field = FieldUtils.getField(SoapUIProTestCaseRunner.class, "assertions", true); FieldUtils.writeField(field, runner, failedAssertions, true); }