Package javassist

Examples of javassist.CtClass


    if (method instanceof CtMethod == false) {
      return false;
    }

    CtClass returnType = ((CtMethod) method).getReturnType();
    String returnTypeName = returnType.getName();

    boolean isVoidMethod = "void".equals(returnTypeName);

    boolean methodReturnsValue = isVoidMethod == false;
    return methodReturnsValue;
View Full Code Here


   * @throws NotFoundException
   */
  public static String getSignature(CtBehavior method)
      throws NotFoundException {

    CtClass parameterTypes[] = method.getParameterTypes();

    CodeAttribute codeAttribute = method.getMethodInfo().getCodeAttribute();

    LocalVariableAttribute locals = null;

    if (codeAttribute != null) {
      AttributeInfo attribute;
      attribute = codeAttribute.getAttribute("LocalVariableTable");
      locals = (LocalVariableAttribute) attribute;
    }

    String methodName = method.getName();

    StringBuffer sb = new StringBuffer(methodName + "(\" ");
    for (int i = 0; i < parameterTypes.length; i++) {
      if (i > 0) {
        // add a comma and a space between printed values
        sb.append(" + \", \" ");
      }

      CtClass parameterType = parameterTypes[i];
      boolean isArray = parameterType.isArray();
      CtClass arrayType = parameterType.getComponentType();
      if (isArray) {
        while (arrayType.isArray()) {
          arrayType = arrayType.getComponentType();
        }
      }

      sb.append(" + \"");
      try {
View Full Code Here

   * @param b
   * @return
   */
  private byte[] doClass(String name, Class<?> clazz, byte[] b) {
    ClassPool pool = ClassPool.getDefault();
    CtClass cl = null;
    try {
      cl = pool.makeClass(new ByteArrayInputStream(b));
      if (cl.isInterface() == false) {

        loggerName = "_____log";

        // We have to declare the log variable.

        String pattern1 = "private static org.slf4j.Logger {};";
        String loggerDefinition = format(pattern1, loggerName).getMessage();
        CtField field = CtField.make(loggerDefinition, cl);

        // and assign it the appropriate value.

        String pattern2 = "org.slf4j.LoggerFactory.getLogger({}.class);";
        String replace = name.replace('/', '.');
        String getLogger = format(pattern2, replace).getMessage();

        cl.addField(field, getLogger);

        // then check every behaviour (which includes methods). We are
        // only
        // interested in non-empty ones, as they have code.
        // NOTE: This will be changed, as empty methods should be
        // instrumented too.

        CtBehavior[] methods = cl.getDeclaredBehaviors();
        for (int i = 0; i < methods.length; i++) {
          if (methods[i].isEmpty() == false) {
            doMethod(methods[i]);
          }
        }
        b = cl.toBytecode();
      }
    } catch (Exception e) {
      System.err.println("Could not instrument " + name + ", " + e);
      e.printStackTrace(System.err);
    } finally {
      if (cl != null) {
        cl.detach();
      }
    }
    return b;
  }
View Full Code Here

            final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            if (contextClassLoader != null)
            {
                pool.insertClassPath(new LoaderClassPath(contextClassLoader));
            }
            final CtClass ctClass = pool.get(className);
           
            // - make sure the class isn't frozen
            ctClass.defrost();

            final String scriptWrapperFieldName = "scriptWrapper";
            try
            {
                ctClass.getField(scriptWrapperFieldName);
            }
            catch (Exception exception)
            {
                final CtField scriptWrapper =
                    new CtField(
                        convert(
                            pool,
                            this.scriptWrapperName),
                        scriptWrapperFieldName,
                        ctClass);
                scriptWrapper.setModifiers(Modifier.PRIVATE + Modifier.FINAL);
                ctClass.addField(
                    scriptWrapper,
                    getScriptWrapperInitialization(
                        scriptDirectory,
                        className));
            }

            final CtMethod[] existingMethods = ctClass.getDeclaredMethods();
            for (int ctr = 0; ctr < existingMethods.length; ctr++)
            {
                final CtMethod method = existingMethods[ctr];
                if (!Modifier.isStatic(method.getModifiers()))
                {
                    final CtClass returnType = method.getReturnType();
                    String methodBody;
                    if (returnType.equals(CtClass.voidType))
                    {
                        methodBody =
                            "{" + contructArgumentString(method) + "scriptWrapper.invoke(\"" + method.getName() +
                            "\", arguments);}";
                    }
                    else
                    {
                        if (returnType.isPrimitive())
                        {
                            methodBody =
                                "{" + contructArgumentString(method) + " return ((" + getWrapperTypeName(returnType) +
                                ")scriptWrapper.invoke(\"" + method.getName() + "\", arguments))." +
                                returnType.getName() + "Value();}";
                        }
                        else
                        {
                            methodBody =
                                "{" + contructArgumentString(method) + " return (" + method.getReturnType().getName() +
View Full Code Here

        final int argumentNumber = argumentTypes.length;
        final StringBuffer arguments =
            new StringBuffer("final Object[] arguments = new Object[" + argumentNumber + "];");
        for (int ctr = 1; ctr <= argumentNumber; ctr++)
        {
            final CtClass argumentType = argumentTypes[ctr - 1];
            arguments.append("arguments[" + (ctr - 1) + "] = ");
            if (argumentType.isPrimitive())
            {
                arguments.append("new java.lang." + getWrapperTypeName(argumentType) + "($" + ctr + ");");
            }
            else
            {
View Full Code Here

    private CtClass convert(
        final ClassPool pool,
        final String className)
        throws NotFoundException
    {
        CtClass ctClass = null;
        if (className != null)
        {
            ctClass = pool.get(className);
        }
        return ctClass;
View Full Code Here

    {

        Class thisClass = this.getClass();
        this.pool = TranslatorClassPool.getPool(thisClass.getClassLoader());

        CtClass ctTranslatorClass = pool.get(thisClass.getName());

        CtField adaptedField = new CtField(CtClass.booleanType, FIELD_ADAPTED, ctTranslatorClass);

        ctTranslatorClass.addField(adaptedField);

        //get the "inA" methods from the analysisClass
        CtMethod[] analysisMethods = ctTranslatorClass.getMethods();

        if (analysisMethods != null)
        {

            int methodNum = analysisMethods.length;

            for (int ctr = 0; ctr < methodNum; ctr++)
            {
                CtMethod method = analysisMethods[ctr];
                String methodName = method.getName();

                if (methodName.startsWith(INA_PREFIX))
                {
                    // add the new overriden "inA" methods
                    this.methods.put(method, this.getInAMethodBody(method));
                }
                else if (methodName.startsWith(OUTA_PREFIX))
                {
                    // add the new overriden "outA" methods
                    this.methods.put(method, this.getOutAMethodBody(method));
                }
                else if (methodName.startsWith(CASE_PREFIX))
                {
                    // add the new overridden "case" methods
                    this.methods.put(method, this.getCaseMethodBody(method));
                }
            }

            //now add all the methods to the class
            Iterator allMethods = this.methods.keySet().iterator();
            while (allMethods.hasNext())
            {
                CtMethod method = (CtMethod)allMethods.next();
                CtMethod newMethod = new CtMethod(method, ctTranslatorClass, null);
                String methodBody = (String)this.methods.get(method);
                newMethod.setBody(methodBody);
                ctTranslatorClass.addMethod(newMethod);
            }

        }
        this.writeAdaptedClass();
        return ctTranslatorClass.toClass();
    }
View Full Code Here

            // ooops, nothing found
            if ( targetMtd == null ) throw new NoSuchMethodException( cacheKey );

            // create a clean, empty proxy class
            CtClass clas = _pool.makeClass( proxyName );
            CtClass target = _pool.get( tClasName );
            CtClass ctIDynProxy = _pool.get( IDynAccess.class.getName() );

            // add target object field to class
            CtField field = new CtField( target, "m_dynTarget", clas );
            clas.addField( field );
View Full Code Here

      NotFoundException {
  LOG.debug("started generating write method in template class %s",
    new Object[] { tmplCtClass.getName() });
  String mbody = buildWriteMethodBody();
  int mod = javassist.Modifier.PUBLIC;
  CtClass returnType = CtClass.voidType;
  String mname = "write";
  CtClass[] paramTypes = new CtClass[] {
    director.getCtClass(Packer.class.getName()),
    director.getCtClass(Object.class.getName()),
                CtClass.booleanType
View Full Code Here

      NotFoundException {
  LOG.debug("started generating read method in template class %s",
    new Object[] { tmplCtClass.getName() });
  String mbody = buildReadMethodBody();
  int mod = javassist.Modifier.PUBLIC;
  CtClass returnType = director.getCtClass(Object.class.getName());
  String mname = "read";
  CtClass[] paramTypes = new CtClass[] {
    director.getCtClass(Unpacker.class.getName()),
    director.getCtClass(Object.class.getName()),
                CtClass.booleanType
View Full Code Here

TOP

Related Classes of javassist.CtClass

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.