Search in sources :

Example 1 with TypeDescriptor

use of org.springsource.loaded.TypeDescriptor in project spring-loaded by spring-projects.

the class CatcherTests method rewrite.

/* 
	 * Details on catchers
	 * 
	 * Four types of method in the super type to think about:
	 * - private
	 * - protected
	 * - default
	 * - public
	 * 
	 * And things to keep in mind:
	 * - private methods are not overridable (invokespecial is used to call them)
	 * - visibility cannot be reduced, only widened
	 * - static methods are not overridable
	 * 
	 * Catching rules:
	 * - don't need a catcher for a private method, there cannot be code out there that calls it with INVOKEVIRTUAL
	 * - visibility is preserved except for protected/default, which is widened to public - this enables the executor to call the
	 *   catcher.  Doesn't seem to have any side effects (doesn't limit the ability for an overriding method in a further
	 *   subclass to have been declared initially protected).
	 */
@Test
public void rewrite() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("catchers.B");
    loadClass("catchers.A");
    TypeDescriptor typeDescriptor = typeRegistry.getExtractor().extract(loadBytesForClass("catchers.B"), true);
    checkDoesNotContain(typeDescriptor, "privateMethod");
    checkDoesContain(typeDescriptor, "0x1 publicMethod");
    checkDoesContain(typeDescriptor, "0x1 protectedMethod");
    checkDoesContain(typeDescriptor, "0x1 defaultMethod");
    ReloadableType rtype = typeRegistry.addType("catchers.B", loadBytesForClass("catchers.B"));
    reload(rtype, "2");
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeDescriptor(org.springsource.loaded.TypeDescriptor) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 2 with TypeDescriptor

use of org.springsource.loaded.TypeDescriptor in project spring-loaded by spring-projects.

the class ReflectiveInterceptor method isDeleted.

private static boolean isDeleted(Constructor<?> c) {
    ReloadableType rtype = getReloadableTypeIfHasBeenReloaded(c.getDeclaringClass());
    if (rtype == null) {
        return false;
    } else {
        TypeDescriptor desc = rtype.getLatestTypeDescriptor();
        MethodMember currentConstructor = desc.getConstructor(Type.getConstructorDescriptor(c));
        if (currentConstructor == null) {
            // Method not there, consider it deleted
            return true;
        } else {
            return false;
        }
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeDescriptor(org.springsource.loaded.TypeDescriptor) MethodMember(org.springsource.loaded.MethodMember)

Example 3 with TypeDescriptor

use of org.springsource.loaded.TypeDescriptor in project spring-loaded by spring-projects.

the class ClassReflectionTests method testSomeBug.

@Test
public void testSomeBug() throws Exception {
    reloadableClass(TARGET_PACKAGE + ".GetMethodInterface");
    String className = TARGET_PACKAGE + ".GetMethodClass";
    ReloadableType rtype = reloadableClass(className);
    reloadType(rtype, "002");
    TypeDescriptor descriptor = rtype.getLatestTypeDescriptor();
    for (MethodMember m : descriptor.getMethods()) {
        System.out.println(m);
        if (m.getName().equals("im2")) {
            //Fine!
            return;
        }
    }
    fail("There should be an im2 method");
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeDescriptor(org.springsource.loaded.TypeDescriptor) MethodMember(org.springsource.loaded.MethodMember) Test(org.junit.Test)

Example 4 with TypeDescriptor

use of org.springsource.loaded.TypeDescriptor in project spring-loaded by spring-projects.

the class IncrementalTypeDescriptorTests method deletedMethods.

//	@Test
//	public void newversionDescriptor() {
//		byte[] classBytes = loadBytesForClass("data.SimpleClassFour");
//		TypeDescriptor td = TypeDescriptorExtractor.extractFor(classBytes);
//
//		byte[] classBytes2 = retrieveRename("data.SimpleClassFour", "data.SimpleClassFour002");
//		TypeDescriptor td2 = TypeDescriptorExtractor.extractFor(classBytes2);
//
//		IncrementalTypeDescriptor nvtd = new IncrementalTypeDescriptor(td);
//		nvtd.setLatestTypeDescriptor(td2);
//
//		// Now ask it questions about the changes
//		List<MethodMember> ms = nvtd.getNewOrChangedMethods();
//		Assert.assertEquals(2, ms.size());
//
//		MethodMember rm = grabFrom(ms, "extraOne");
//		Assert.assertNotNull(rm);
//		Assert.assertEquals("0x1 extraOne(Ljava/lang/String;)V", rm.toString());
//
//		rm = grabFrom(ms, "extraTwo");
//		Assert.assertNotNull(rm);
//		Assert.assertEquals("0x9 extraTwo(I)Ljava/lang/Double;", rm.toString());
//		//
//		//		boolean b = nvtd.defines(false, "extraOne", "(Ljava/lang/String;)V");
//		//		Assert.assertTrue(b);
//		//
//		//		b = nvtd.defines(true, "extraOne", "(Ljava/lang/String;)V");
//		//		Assert.assertFalse(b);
//	}
// regular method deleted
@Test
public void deletedMethods() throws Exception {
    TypeRegistry registry = getTypeRegistry("");
    byte[] bytes = loadBytesForClass("typedescriptor.A");
    TypeDescriptor typeDescriptor = new TypeDescriptorExtractor(registry).extract(bytes, true);
    byte[] bytes2 = ClassRenamer.rename("typedescriptor.A", loadBytesForClass("typedescriptor.A2"));
    TypeDescriptor typeDescriptor2 = new TypeDescriptorExtractor(registry).extract(bytes2, true);
    IncrementalTypeDescriptor itd = new IncrementalTypeDescriptor(typeDescriptor);
    itd.setLatestTypeDescriptor(typeDescriptor2);
    Assert.assertEquals(1, itd.getDeletedMethods().size());
    Assert.assertEquals("0x1 m()V", itd.getDeletedMethods().get(0).toString());
}
Also used : IncrementalTypeDescriptor(org.springsource.loaded.IncrementalTypeDescriptor) TypeDescriptor(org.springsource.loaded.TypeDescriptor) IncrementalTypeDescriptor(org.springsource.loaded.IncrementalTypeDescriptor) TypeRegistry(org.springsource.loaded.TypeRegistry) TypeDescriptorExtractor(org.springsource.loaded.TypeDescriptorExtractor) Test(org.junit.Test)

Example 5 with TypeDescriptor

use of org.springsource.loaded.TypeDescriptor in project spring-loaded by spring-projects.

the class IncrementalTypeDescriptorTests method changedModifiers.

// More subtle changes (modifier flags)
@Test
public void changedModifiers() throws Exception {
    TypeRegistry registry = getTypeRegistry("");
    byte[] bytes = loadBytesForClass("typedescriptor.C");
    TypeDescriptor typeDescriptor = registry.getExtractor().extract(bytes, true);
    byte[] bytes2 = ClassRenamer.rename("typedescriptor.C", loadBytesForClass("typedescriptor.C2"));
    TypeDescriptor typeDescriptor2 = registry.getExtractor().extract(bytes2, true);
    IncrementalTypeDescriptor itd = new IncrementalTypeDescriptor(typeDescriptor);
    itd.setLatestTypeDescriptor(typeDescriptor2);
    System.out.println(itd.toString(true));
    List<MethodMember> changed = itd.getNewOrChangedMethods();
    MethodMember m = getMethod(changed, "staticMethod");
    Assert.assertTrue(IncrementalTypeDescriptor.isNowNonStatic(m));
    m = getMethod(changed, "instanceMethod");
    Assert.assertTrue(IncrementalTypeDescriptor.isNowStatic(m));
    m = getMethod(changed, "publicMethod1");
    Assert.assertTrue(IncrementalTypeDescriptor.hasVisibilityChanged(m));
    // TODO Not detected as protected methods always made public in reloadable types... is that OK?
    //		m = getMethod(changed, "publicMethod2");
    //		Assert.assertTrue(IncrementalTypeDescriptor.hasVisibilityChanged(m));
    m = getMethod(changed, "publicMethod3");
    Assert.assertTrue(IncrementalTypeDescriptor.hasVisibilityChanged(m));
}
Also used : IncrementalTypeDescriptor(org.springsource.loaded.IncrementalTypeDescriptor) TypeDescriptor(org.springsource.loaded.TypeDescriptor) IncrementalTypeDescriptor(org.springsource.loaded.IncrementalTypeDescriptor) TypeRegistry(org.springsource.loaded.TypeRegistry) MethodMember(org.springsource.loaded.MethodMember) Test(org.junit.Test)

Aggregations

TypeDescriptor (org.springsource.loaded.TypeDescriptor)25 TypeRegistry (org.springsource.loaded.TypeRegistry)20 Test (org.junit.Test)18 TypeDescriptorExtractor (org.springsource.loaded.TypeDescriptorExtractor)13 MethodMember (org.springsource.loaded.MethodMember)10 ReloadableType (org.springsource.loaded.ReloadableType)6 IncrementalTypeDescriptor (org.springsource.loaded.IncrementalTypeDescriptor)4 FieldMember (org.springsource.loaded.FieldMember)3 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1