Package javassist

Examples of javassist.CtMethod


   * @throws NotFoundException
   */
  private void addProducerMethod(Field field, CtClass mainClass) throws ClassNotFoundException,
      CannotCompileException, NotFoundException, IOException {

    CtMethod mnew = null;
    String injectedFieldType = field.getType().getName();
    String injectedFieldName = field.getName();
    if (producerMethodKeys.contains(injectedFieldType + injectedFieldName)) {
      return;
    } else {
      producerMethodKeys.add(injectedFieldType + injectedFieldName);
    }
    // construct producer method body
    StringBuilder sb = new StringBuilder();
    sb.append("public ");
    sb.append(injectedFieldType);
    sb.append(" get");
    sb.append(injectedFieldName);
    sb.append("() {");
    sb.append(injectedFieldType + " f = null;");
    sb.append("Component c = ZkCDIIntegrationContext.getContextComponent();");
//    sb.append("System.out.println(\"Current thread id = \" + Thread.currentThread().getId());");
    sb.append("if(c == null) {");
//    sb.append("System.out.println(\"returning dummy instance of " + injectedFieldType + "\");");
    sb.append("return new " + injectedFieldType + "();} else {");
//    sb.append("System.out.println(\"returning component retrieved from component tree\" + c);");
   
    sb.append("try {");   
   
    sb.append("f = (" + injectedFieldType + ")c.getFellow(\""
        + injectedFieldName + "\");");
//     sb.append("System.out.println(\"returning component retrieved from component tree\" + f);");
   
    sb.append("}catch(ComponentNotFoundException e) {e.printStackTrace(); throw new RuntimeException(e.getMessage());}}");
   
    sb.append("return f;");
    sb.append("}");
    mnew = CtNewMethod.make(sb.toString(), mainClass);

    ConstPool cp = mnew.getMethodInfo().getConstPool();
    AnnotationsAttribute attr = new AnnotationsAttribute(cp,
        AnnotationsAttribute.visibleTag);

    javassist.bytecode.annotation.Annotation producesAnnotation = new javassist.bytecode.annotation.Annotation(
        "javax.enterprise.inject.Produces", cp);
    javassist.bytecode.annotation.Annotation zkScopedAnnotation = new javassist.bytecode.annotation.Annotation(
        "org.zkoss.cdi.context.IdSpaceScoped", cp);
    javassist.bytecode.annotation.Annotation zkCompAnnotation = new javassist.bytecode.annotation.Annotation(
        "org.zkoss.cdi.inject.ComponentId", cp);
    zkCompAnnotation.addMemberValue("value", new StringMemberValue(field
        .getName(), cp));
   
    attr.addAnnotation(producesAnnotation);
    attr.addAnnotation(zkScopedAnnotation);
    attr.addAnnotation(zkCompAnnotation);
    mnew.getMethodInfo().addAttribute(attr);
   
    mainClass.addMethod(mnew);
  }
View Full Code Here


        builder.append(")");
        builder.append(ClassFabUtils.getTypeCode(method.getReturnType()));

        try
        {
            CtMethod ctMethod = ctClass.getMethod(method.getName(), builder.toString());

            int lineNumber = ctMethod.getMethodInfo().getLineNumber(0);

            String sourceFile = ctMethod.getDeclaringClass().getClassFile2().getSourceFile();

            return new MethodLocation(method, sourceFile, lineNumber);
        }
        catch (Exception ex)
        {
View Full Code Here

        CtClass ctReturnType = convertClass(ms.getReturnType());

        CtClass[] ctParameters = convertClasses(ms.getParameterTypes());
        CtClass[] ctExceptions = convertClasses(ms.getExceptionTypes());

        CtMethod method = new CtMethod(ctReturnType, ms.getName(), ctParameters, getCtClass());

        try
        {
            method.setModifiers(modifiers);
            method.setBody(body);
            method.setExceptionTypes(ctExceptions);

            getCtClass().addMethod(method);
        }
        catch (Exception ex)
        {
View Full Code Here

    public <T extends Annotation> T getMethodAnnotation(MethodSignature signature,
            Class<T> annotationClass)
    {
        failIfFrozen();

        CtMethod method = findMethod(signature);

        if (method == null)
            throw new IllegalArgumentException(ServicesMessages.noDeclaredMethod(
                    _ctClass,
                    signature));
View Full Code Here

        if (!Modifier.isAbstract(method.getModifiers())) return;

        try
        {
            CtMethod newMethod = CtNewMethod.copy(method, _ctClass, null);

            // Methods from interfaces are always public. We definitely
            // need to change the modifiers of the method so that
            // it is not abstract.

            newMethod.setModifiers(Modifier.PUBLIC);

            // Javassist will provide a minimal implementation for us (return null, false, 0,
            // whatever).

            newMethod.setBody(null);

            _ctClass.addMethod(newMethod);

            MethodSignature sig = getMethodSignature(newMethod);
View Full Code Here

        String action = "add";

        try
        {
            CtMethod existing = _ctClass.getDeclaredMethod(signature.getMethodName(), parameters);

            if (existing != null)
            {
                action = "replace";

                _ctClass.removeMethod(existing);
            }
        }
        catch (NotFoundException ex)
        {
            // That's ok. Kind of sloppy to rely on a thrown exception; wish getDeclaredMethod()
            // would return null for
            // that case. Alternately, we could maintain a set of the method signatures of declared
            // or added methods.
        }

        try
        {

            CtMethod method = new CtMethod(returnType, signature.getMethodName(), parameters,
                    _ctClass);

            // TODO: Check for duplicate method add

            method.setModifiers(signature.getModifiers());

            method.setBody(methodBody);
            method.setExceptionTypes(exceptions);

            _ctClass.addMethod(method);

            _addedMethods.add(method);
        }
View Full Code Here

    public void extendMethod(MethodSignature methodSignature, String methodBody)
    {
        failIfFrozen();

        CtMethod method = findMethod(methodSignature);

        try
        {
            method.insertAfter(methodBody);
        }
        catch (CannotCompileException ex)
        {
            throw new MethodCompileException(ServicesMessages.methodCompileError(
                    methodSignature,
View Full Code Here

        _formatter.format("\n%s\n\n", methodBody);
    }

    private CtMethod findMethod(MethodSignature methodSignature)
    {
        CtMethod method = findDeclaredMethod(methodSignature);

        if (method != null) return method;

        CtMethod result = addOverrideOfSuperclassMethod(methodSignature);

        if (result != null) return result;

        throw new IllegalArgumentException(ServicesMessages.noDeclaredMethod(
                _ctClass,
View Full Code Here

                    if (match(method, methodSignature))
                    {
                        // TODO: If the moethod is not overridable (i.e. private, or final)?
                        // Perhaps we should limit it to just public methods.

                        CtMethod newMethod = CtNewMethod.delegator(method, _ctClass);
                        _ctClass.addMethod(newMethod);

                        return newMethod;
                    }
                }
View Full Code Here

        try
        {
            CtConstructor defaultConstructor = _ctClass.getConstructor("()V");

            CtMethod initializerMethod = defaultConstructor.toMethod(initializer, _ctClass);

            _ctClass.addMethod(initializerMethod);
        }
        catch (Exception ex)
        {
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.