Package javassist

Examples of javassist.CtMethod


            if (ctClass == null)
            {
               ctClass = getCtClass(annotation);
            }
           
            CtMethod method;
            try
            {
               method = ctClass.getDeclaredMethod(methods[i].getName());
            }
            catch (NotFoundException e)
            {
               throw new RuntimeException("Unable to find method " + methods[i].getName() + " for " + annotation.getName());
            }
            Object defaultValue = null;
            MethodInfo minfo = method.getMethodInfo2();
            AnnotationDefaultAttribute defAttr = (AnnotationDefaultAttribute)minfo.getAttribute(AnnotationDefaultAttribute.tag);
           
            if (defAttr != null)
            {
               MemberValue value = defAttr.getDefaultValue();    // default value of age
View Full Code Here


   }

   public void testMethod() throws Exception
   {
      CtClass ctClass = getCtClass(SignatureTester.class);
      CtMethod ctMethod = ctClass.getDeclaredMethod("applyTimeZone", new CtClass[]{getCtClass(Calendar.class), getCtClass(TimeZone.class)});
      Signature sig = JavassistSignatureFactory.getSignature(ctMethod);
      assertEquals(getClassMethodSignature(), sig);
      assertEquals(getStringMethodSignature(), sig);
      assertEquals(getMethodSignature(), sig);
      assertEquals(getMethodInfoSignature(), sig);
View Full Code Here

   }

   public void testMethodParams() throws Exception
   {
      CtClass ctClass = getCtClass(SignatureTester.class);
      CtMethod ctMethod = ctClass.getDeclaredMethod("applyTimeZone", new CtClass[]{getCtClass(Calendar.class), getCtClass(TimeZone.class)});
      Signature sig = new JavassistMethodParametersSignature(ctMethod, 0);
      assertEquals(getClassMethodParametersSignature(), sig);
      assertEquals(getStringMethodParametersSignature(), sig);
      assertEquals(getMethodParametersSignature(), sig);
      assertEquals(getMethodInfoParametersSignature(), sig);
View Full Code Here

              "if(" + EnhancerConstants.TRACKER_FIELD_NAME + " == null) " +
              EnhancerConstants.TRACKER_FIELD_NAME + " = new java.util.HashSet();" +
              EnhancerConstants.TRACKER_COLLECTION_CHANGED_FIELD_NAME + "(" +
              EnhancerConstants.TRACKER_FIELD_NAME + ");" +
              "return " + EnhancerConstants.TRACKER_FIELD_NAME + "; }";
      CtMethod getMethod = CtNewMethod.make( trackerGetMethod, managedCtClass );

      MethodInfo methodInfo = getMethod.getMethodInfo();
      SignatureAttribute signatureAttribute =
          new SignatureAttribute( methodInfo.getConstPool(), "()Ljava/util/Set<Ljava/lang/String;>;" );
      methodInfo.addAttribute( signatureAttribute );
      managedCtClass.addMethod( getMethod );
View Full Code Here

    // is not enabled as lazy-loadable return a plain simple getter as the reader
    if ( !enhancementContext.isLazyLoadable( persistentField ) ) {
      // not lazy-loadable...
      // EARLY RETURN!!!
      try {
        final CtMethod reader = CtNewMethod.getter( readerName, persistentField );
        managedCtClass.addMethod( reader );
        return reader;
      }
      catch (CannotCompileException e) {
        throw new EnhancementException(
            String.format(
                "Could not enhance entity class [%s] to add field reader method [%s]",
                managedCtClass.getName(),
                readerName
            ),
            e
        );
      }
    }

    // temporary solution...
    final String methodBody = typeDescriptor.buildReadInterceptionBodyFragment( fieldName )
        + " return this." + fieldName + ";";

    try {
      final CtMethod reader = CtNewMethod.make(
          Modifier.PRIVATE,
          persistentField.getType(),
          readerName,
          null,
          null,
View Full Code Here

    final FieldInfo fieldInfo = persistentField.getFieldInfo();
    final String fieldName = fieldInfo.getName();
    final String writerName = EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + fieldName;

    final CtMethod writer;

    try {
      if ( !enhancementContext.isLazyLoadable( persistentField ) ) {
        // not lazy-loadable...
        writer = CtNewMethod.setter( writerName, persistentField );
      }
      else {
        final String methodBody = typeDescriptor.buildWriteInterceptionBodyFragment( fieldName );
        writer = CtNewMethod.make(
            Modifier.PRIVATE,
            CtClass.voidType,
            writerName,
            new CtClass[] {persistentField.getType()},
            null,
            "{" + methodBody + "}",
            managedCtClass
        );
      }

      if ( enhancementContext.doDirtyCheckingInline( managedCtClass ) && !isComposite ) {
        writer.insertBefore( typeDescriptor.buildInLineDirtyCheckingBodyFragment( persistentField ) );
      }

      if ( isComposite ) {
        StringBuilder builder = new StringBuilder();
        builder.append( " if(  " )
            .append( EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME )
            .append( " != null) " )
            .append( EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME )
            .append( ".callOwner(\"." )
            .append( persistentField.getName() )
            .append( "\");" );

        writer.insertBefore( builder.toString() );
      }

      //composite types
      if ( persistentField.getAnnotation( Embedded.class ) != null ) {
        //make sure to add the CompositeOwner interface
        if ( !doClassInheritCompositeOwner( managedCtClass ) ) {
          managedCtClass.addInterface( classPool.get( "org.hibernate.engine.spi.CompositeOwner" ) );
        }
        //if a composite have a embedded field we need to implement the method as well
        if ( isComposite ) {
          createTrackChangeCompositeMethod( managedCtClass );
        }


        writer.insertBefore( cleanupPreviousOwner( persistentField ) );

        writer.insertAfter( compositeMethodBody( persistentField ) );
      }

      managedCtClass.addMethod( writer );
      return writer;
    }
View Full Code Here

    if (!cc.isInterface()) {
      cc.addField(new CtField(CtClass.intType, "_counter", cc));
    }
    CtMethod[] ms = cc.getDeclaredMethods();
    for (int j = 0; j < ms.length; ++j) {
      CtMethod m = ms[j];
      int modifiers = m.getModifiers();
      if (!Modifier.isStatic(modifiers) && !Modifier.isAbstract(modifiers) && !Modifier.isNative(modifiers)) {
        if (!m.isEmpty()) {
          MethodInfo info = m.getMethodInfo();
          Bytecode bc = new Bytecode(info.getConstPool(), 1, 0);
          bc.addAload(0);
          bc.addAload(0);
          bc.addGetfield(cc, "_counter", "I");
          bc.add(Bytecode.ICONST_1);
View Full Code Here

        CtClass[] params = createParameters(objectType, m.getParameterTypes().length);
        CtClass[] empty = new CtClass[0];

        for (int length = params.length; length >= 0; length--) {
          CtMethod method = abstractMethod(returnType, m.getName(), Arrays.copyOf(params, length), empty, inter);
          if (used.add(method)) {
            inter.addMethod(method);
            logger.debug("added method {} to interface {}", method.getName(), controller);
          }
        }

        CtMethod getter = abstractMethod(returnType, String.format("get%s", capitalize(name)), empty, empty, inter);
        if (used.add(getter)) {
          inter.addMethod(getter);
          logger.debug("added getter {} to interface {}", getter.getName(), controller);
        }
      }
      return inter.toClass();
    } catch (CannotCompileException | NotFoundException e) {
      throw new ProxyCreationException(e);
View Full Code Here

      try {
         carCt.addField(CtField.make("public int year;", carCt));
         Class carClass = carCt.toClass();
         if (isNewExternalizer) {
            CtClass carExtCt = pool.get(CAR_EXT);
            CtMethod writeObjMeth = carExtCt.getMethod("writeObject",
                  "(Ljava/io/ObjectOutput;Ljava/lang/Object;)V");
            writeObjMeth.setBody("{\n" +
               "$1.writeObject(((" + CAR + ") $2).plateNumber);\n" +
               "$1.writeInt(((" + CAR + ") $2).year);\n" +
            "}\n"
            );
            CtMethod readObjMeth = carExtCt.getMethod("readObject",
                  "(Ljava/io/ObjectInput;)Ljava/lang/Object;");
            readObjMeth.setBody("{\n" +
               CAR + " o = new " + CAR + "();\n" +
               "o.plateNumber = (String) $1.readObject();\n" +
               "int b1 = $1.read();\n" +
               "System.out.println(b1);\n" +
               "if (b1 != -1) {\n" + // Check whether end of stream has been reached.
View Full Code Here

      try {
         ct.addField(CtField.make("public String name;", ct));
         Class clazz = ct.toClass();
         if (isNewExternalizer) {
            CtClass extCt = pool.get(PERSON_EXT);
            CtMethod writeObjMeth = extCt.getMethod("writeObject", "(Ljava/io/ObjectOutput;Ljava/lang/Object;)V");
            writeObjMeth.setBody("{\n" +
               "$1.writeInt(((" + PERSON + ") $2).age);\n" +
               "$1.writeObject((("  + PERSON + ") $2).name);\n" +
            "}\n"
            );
            CtMethod readObjMeth = extCt.getMethod("readObject", "(Ljava/io/ObjectInput;)Ljava/lang/Object;");
            readObjMeth.setBody("{\n" +
               PERSON + " o = new " + PERSON + "();\n" +
               "o.age = $1.readInt();\n" +
               "try {\n" +
               "   o.name = (String) $1.readObject();\n" +
               "} catch(java.io.OptionalDataException e) {}\n" +
View Full Code Here

TOP

Related Classes of javassist.CtMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.