Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

xexuxjy

Member Since 19 Sep 2012
Offline Last Active May 23 2013 10:28 AM
-----

Posts I've Made

In Topic: newInstance and ClassCastException

13 March 2013 - 11:16 AM

This may not be quite what you're looking for , but it's a tweaked version of your sample that creates an object that is recognised as a subclass of SimpleEntity. Main changes (I think!) have been setting up parent class loaders and using defaults wherever possible. I'm not sure if your attempt to call createPool twice will cause problems either...

 

 

package cloud;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.Loader;
import javassist.Modifier;
import javassist.NotFoundException;

public class TestBuilder
{

	private Loader classLoader;
	private ClassPool pool;
	private CtClass abstractEntityCtClass;

	public TestBuilder()
	{

	}

	public void buildClass(String name, String msg)
	{

		System.out.println("Building class " + name);

		// Create CtClass.
		CtClass ctClass = pool.makeClass(name);

		// Set superclass to cloud.Entity.
		try
		{
			ctClass.setSuperclass(abstractEntityCtClass);
		}
		catch (CannotCompileException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}

		// Add field.
		String fieldName = "age";
		try
		{

			// Create field.
			CtField field = new CtField(CtClass.intType, fieldName, ctClass);
			field.setModifiers(Modifier.PUBLIC);
			ctClass.addField(field);

			String s = "public void say() { System.out.println(\"" + msg + "\");}";

			ctClass.addMethod(CtNewMethod.make(s, ctClass));

		}
		catch (CannotCompileException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return;
		}

		// Add property.
		String getterName = "getAge";
		String type = CtClass.intType.getName();

		StringBuffer sb = new StringBuffer();
		sb.append("public ").append(type).append(" ").append(getterName).append("(){").append("return this.")
				.append(fieldName).append(";").append("}");

		try
		{
			CtMethod method = CtMethod.make(sb.toString(), ctClass);
			ctClass.addMethod(method);
		}
		catch (CannotCompileException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}

		System.out.println("ctClass " + ctClass);
	}

	public Object createInstance(String name)
	{

		System.out.println("Creating class instance " + name);

		Object o = null;

		// Create and add to list.
		//Class<? extends SimpleEntity> newKlass;
		Class newKlass;
		try
		{
			//newKlass = (Class<? extends SimpleEntity>) classLoader.loadClass(name);
			//newKlass = classLoader.loadClass(name);
			CtClass newCtClass = pool.getCtClass(name);
			
			//o = newKlass.newInstance();
			Class c1 = newCtClass.toClass();
			o = c1.newInstance();
			
			if(o instanceof SimpleEntity)
			{
				int foo = 1;
			}


			int ibreak = 0;
			SimpleEntity e = (SimpleEntity) o;
			// objects.add(o);
			System.out.println("Created instance " + o);
		}
		catch(Throwable t)
		{
			t.printStackTrace();
		}
//		catch (ClassNotFoundException e1)
//		{
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//			return o;
//		}

		Method method;
		try
		{
			method = o.getClass().getMethod("say");
			method.invoke(o, null);
		}
		catch (NoSuchMethodException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SecurityException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalArgumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return o;

	}

	public void build()
	{

		String name = "foo.Bob";

		// Create new class pool and loader.
		buildPool();

		// Build the class.
		buildClass(name, "fred");

		// Create instance.
		Object o = createInstance(name);

		// Create new class pool and loader.
		//buildPool();

		// Rebuild the class.
		//buildClass(name, "bob");

		// Create an instance of the rebuilt class.
		//Object o2 = createInstance(name);

	}

	private void buildPool()
	{

		// Create class loader and pool.
		//this.pool = new ClassPool(ClassPool.getDefault());
		this.pool = ClassPool.getDefault();
		//this.classLoader = new Loader(ClassLoader.getSystemClassLoader(),this.pool);
		//		this.classLoader = Loader.getSystemClassLoader();
		//		this.classLoader.setClassPool(this.pool);

		//pool.insertClassPath(new ClassClassPath(this.getClass()));

		// Get the cloud.AbstractEntity class.
		try
		{
			abstractEntityCtClass = pool.get("cloud.SimpleEntity");
			System.out.println("Abstract Entity " + abstractEntityCtClass);
		}
		catch (NotFoundException e)
		{
			e.printStackTrace();
			System.exit(-2);
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{

		TestBuilder builder = new TestBuilder();
		builder.build();

	}
}

In Topic: Licensing Code Releases using Bullet 3D

11 March 2013 - 07:36 AM

Sorry, wasn't trying to be argumentitive, and I agree that inheriting is definitely a good way to access Bullet. When I've used it myself (mainly through my XNA port) I've often found it easier to change some of the source directly as the Bullet code has been written by different people with different ways of exposing data (some classes have public access to member variables, others only via accessors, some data sees to me to exist at the wrong level (e.g btMotionState only on rigid bodies),etc). Most of this could probably be cleared up by extending the classes but sometimes it's nicer just to fix it directly.


In Topic: Licensing Code Releases using Bullet 3D

06 March 2013 - 08:58 AM

Not really true - lots of stuff in bullet is non virtual on purpose for performance reasons, I've found it's definitely much easier just to add stuff in directly and a lot of people seem to have custom bullet builds.


In Topic: [XNA] 2D - Creating a Weapon Slash, Swipe, Swoosh, Trailing Effect with Shaders?

22 January 2013 - 04:21 AM

I played a bit with mercury particle engine (http://mpe.codeplex.com/) and found it pretty easy to add some emiters to my 3d game (awful picture but : http://xexuxjy-xna-games.googlecode.com/files/mc-tree-particles.png)    . The editor is also very good.

I'd definitely give it a try and it's all open source.


In Topic: Best way to downscale a heightmap during runtime

21 January 2013 - 07:06 AM

If you're using Bullet then you have access to the source, so it should be fairly easy to update btHeightfieldTerrainShape::getRawHeightFieldValue(int x,int y) to sample at a lower resolution, or with some averaging of points . But obviously you may then get odd issues with graphics and physics being a little bit out of synch (objects sinking into the ground slightly or whatever). Still, should save you having to create an extra height map.


PARTNERS