public void testStaticArrays() throws Exception { CtClass cc = sloader.makeClass("StaticArrays"); CtField f = new CtField(sloader.get("test2.StaticArraysMem[]"), "myStaticField", cc); f.setModifiers(Modifier.STATIC); cc.addField(f); CtConstructor init = cc.makeClassInitializer(); String body = "{\n"; body += ("myStaticField = new test2.StaticArraysMem[2];\n"); body += ("\n}"); init.setBody(body); }
public void testStaticFinal() throws Exception { CtClass cc = sloader.makeClass("test2.StaticFinal"); CtField f = new CtField(CtClass.intType, "sff1", cc); f.setModifiers(Modifier.STATIC | Modifier.FINAL); cc.addField(f, "5"); assertEquals(new Integer(5), f.getConstantValue()); f = new CtField(CtClass.longType, "sff2", cc); f.setModifiers(Modifier.STATIC | Modifier.FINAL); cc.addField(f, "6"); assertEquals(new Long(6), f.getConstantValue()); f = new CtField(CtClass.floatType, "sff3", cc); f.setModifiers(Modifier.STATIC | Modifier.FINAL); cc.addField(f, "7"); assertEquals(new Float(7.0F), f.getConstantValue()); f = new CtField(CtClass.floatType, "sff4", cc); f.setModifiers(Modifier.STATIC | Modifier.FINAL); cc.addField(f, "8.0"); assertEquals(new Float(8.0F), f.getConstantValue()); f = new CtField(CtClass.doubleType, "sff5", cc); f.setModifiers(Modifier.STATIC | Modifier.FINAL); cc.addField(f, "9"); assertEquals(new Double(9.0), f.getConstantValue()); f = new CtField(CtClass.doubleType, "sff6", cc); f.setModifiers(Modifier.STATIC | Modifier.FINAL); cc.addField(f, "10.0"); assertEquals(new Double(10.0), f.getConstantValue()); f = new CtField(sloader.get("java.lang.String"), "sff7", cc); f.setModifiers(Modifier.STATIC | Modifier.FINAL); cc.addField(f, "\"test\""); assertEquals("test", f.getConstantValue()); f = new CtField(sloader.get("java.lang.String"), "sff8", cc); f.setModifiers(Modifier.STATIC); cc.addField(f, "\"static\""); assertEquals(null, f.getConstantValue()); cc.addMethod(CtNewMethod.make("public int foo(){ return sff1 + sff7.length(); }", cc)); cc.writeFile(); Object obj = make(cc.getName()); assertEquals(9, invoke(obj, "foo")); }