Beispiel #1
0
 private void testRemove2(CtClass cc, String fieldName) throws Exception {
   CtField f = cc.getField(fieldName);
   cc.removeField(f);
   try {
     CtField f2 = cc.getField(fieldName);
     fail("the removed field still exists");
   } catch (NotFoundException e) {
   }
 }
Beispiel #2
0
 /** @param name a qualified class name. e.g. java.lang.String */
 public CtField lookupField(String className, Symbol fieldName) throws CompileError {
   CtClass cc = lookupClass(className, false);
   try {
     return cc.getField(fieldName.get());
   } catch (NotFoundException e) {
   }
   throw new CompileError("no such field: " + fieldName.get());
 }
Beispiel #3
0
  /**
   * Only used by fieldAccess() in MemberCodeGen and TypeChecker.
   *
   * @param jvmClassName a JVM class name. e.g. java/lang/String
   */
  public CtField lookupFieldByJvmName2(String jvmClassName, Symbol fieldSym, ASTree expr)
      throws NoFieldException {
    String field = fieldSym.get();
    CtClass cc = null;
    try {
      cc = lookupClass(jvmToJavaName(jvmClassName), true);
    } catch (CompileError e) {
      // EXPR might be part of a qualified class name.
      throw new NoFieldException(jvmClassName + "/" + field, expr);
    }

    try {
      return cc.getField(field);
    } catch (NotFoundException e) {
      // maybe an inner class.
      jvmClassName = javaToJvmName(cc.getName());
      throw new NoFieldException(jvmClassName + "$" + field, expr);
    }
  }
Beispiel #4
0
  public void testRemove() throws Exception {
    CtClass cc = sloader.get("test2.Remove");
    testRemove2(cc, "f1");
    testRemove2(cc, "f6");
    testRemove2(cc, "f3");
    CtField p = cc.getField("p");
    try {
      cc.removeField(p);
      fail("non-existing field has been removed");
    } catch (NotFoundException e) {
    }

    testRemove3(cc, "bar");
    testRemove3(cc, "bar2");
    testRemove4(cc, "(I)V");
    cc.writeFile();
    Object obj = make(cc.getName());
    assertEquals(7, invoke(obj, "foo"));
  }
Beispiel #5
0
  public void testConstField() throws Exception {
    CtClass cc = sloader.get("test2.ConstField");
    CtField f;
    f = cc.getField("b");
    assertEquals(true, ((Boolean) f.getConstantValue()).booleanValue());
    f = cc.getField("i");
    assertEquals(3, ((Integer) f.getConstantValue()).intValue());
    f = cc.getField("j");
    assertEquals(7L, ((Long) f.getConstantValue()).longValue());
    f = cc.getField("f");
    assertEquals(8.0F, ((Float) f.getConstantValue()).floatValue(), 0.0);
    f = cc.getField("d");
    assertEquals(9.0, ((Double) f.getConstantValue()).doubleValue(), 0.0);
    f = cc.getField("s");
    assertEquals("const", f.getConstantValue());
    f = cc.getField("obj");
    assertEquals(null, f.getConstantValue());
    f = cc.getField("integer");
    assertEquals(null, f.getConstantValue());
    f = cc.getField("k");
    assertEquals(null, f.getConstantValue());

    cc.getClassFile().prune();

    f = cc.getField("i");
    assertEquals(3, ((Integer) f.getConstantValue()).intValue());
    f = cc.getField("k");
    assertEquals(null, f.getConstantValue());
  }