Package javassist

Examples of javassist.CtConstructor


                                // filter caller methods
                                if (methodFilterCaller(where)) {
                                    return;
                                }

                                CtConstructor ctConstructor = newExpr.getConstructor();
                                String calleeClassName = newExpr.getClassName();

                                // filter callee classes
                                if (!definition.inIncludePackage(calleeClassName)) {
                                    return;
                                }
                                // filter the constructors
                                if (constructorFilter(ctConstructor)) {
                                    return;
                                }

                                // create the class meta-data
                                ClassMetaData calleeSideClassMetaData;
                                try {
                                    calleeSideClassMetaData =
                                    JavassistMetaDataMaker.createClassMetaData(
                                            context.getClassPool().get(calleeClassName)
                                    );
                                }
                                catch (NotFoundException e) {
                                    throw new WrappedRuntimeException(e);
                                }

                                // create the method meta-data
                                ConstructorMetaData constructorMetaData =
                                        JavassistMetaDataMaker.createConstructorMetaData(newExpr.getConstructor());

                                // is this a caller side method pointcut?
                                if (definition.isPickedOutByCallPointcut(calleeSideClassMetaData, constructorMetaData)) {

                                    // check the callee class is not the same as target class, if that is the case
                                    // then we have have class loaded and set in the ___AW_clazz already
                                    String declaringClassMethodName = TransformationUtil.STATIC_CLASS_FIELD;

                                    CtClass declaringClass = ctConstructor.getDeclaringClass();
                                    if (!declaringClass.getName().replace('/', '.').equals(where.getDeclaringClass().getName().replace('/', '.'))) {
                                        declaringClassMethodName =
                                        addCalleeMethodDeclaringClassField(ctClass, ctConstructor);
                                    }

                                    // call the wrapper method instead of the callee method
                                    StringBuffer body = new StringBuffer();
                                    body.append('{');
                                    if (ctConstructor.getParameterTypes().length > 0) {
                                        body.append("Object[] args = $args; ");
                                    }
                                    else {
                                        body.append("Object[] args = null; ");
                                    }
View Full Code Here


      // new class
      ClassPool pool = ClassPool.getDefault();
      CtClass clazz = pool.makeClass(fullName);

      // add a default constructor
      CtConstructor constructor = new CtConstructor(null, clazz);
      constructor.setBody("{super();}");
      clazz.addConstructor(constructor);

      // add the method implementation
      CtMethod m = CtNewMethod.make(methodDef, clazz);
      clazz.addMethod(m);
View Full Code Here

        CtClass[] tlist = gen.makeThrowsList(md);
        recordParams(plist, Modifier.isStatic(mod));
        md = p.parseMethod2(stable, md);
        try {
            if (md.isConstructor()) {
                CtConstructor cons = new CtConstructor(plist,
                                                   gen.getThisClass());
                cons.setModifiers(mod);
                md.accept(gen);
                cons.getMethodInfo().setCodeAttribute(
                                        bytecode.toCodeAttribute());
                cons.setExceptionTypes(tlist);
                return cons;
            }
            else {
                Declarator r = md.getReturn();
                CtClass rtype = gen.resolver.lookupClass(r);
View Full Code Here

    {
        String initializer = _idAllocator.allocateId("initializer");

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

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

            _ctClass.addMethod(initializerMethod);
        }
        catch (Exception ex)
        {
            throw new RuntimeException(ex);
        }

        _formatter.format("convert default constructor: %s();\n\n", initializer);

        int count = _constructorArgs.size();

        CtClass[] types = new CtClass[count];

        for (int i = 0; i < count; i++)
        {
            ConstructorArg arg = _constructorArgs.get(i);

            types[i] = arg.getType();
        }

        // Add a call to the initializer; the method converted fromt the classes default
        // constructor.

        _constructor.append("  ");
        _constructor.append(initializer);

        // This finally matches the "{" added inside the constructor

        _constructor.append("();\n\n}");

        String constructorBody = _constructor.toString();

        try
        {
            CtConstructor cons = CtNewConstructor.make(types, null, constructorBody, _ctClass);
            _ctClass.addConstructor(cons);
        }
        catch (CannotCompileException ex)
        {
            throw new RuntimeException(ex);
View Full Code Here

    {
        CtClass[] ctParameters = convertClasses(parameterTypes);
        CtClass[] ctExceptions = convertClasses(exceptions);

        try {
            CtConstructor constructor = new CtConstructor(ctParameters, getCtClass());
            constructor.setExceptionTypes(ctExceptions);
            constructor.setBody(body);

            getCtClass().addConstructor(constructor);

            _constructors.add(new AddedConstructor(parameterTypes, exceptions, body));
        } catch (Exception ex) {
View Full Code Here

        try
        {
            // And have a public constructor.

            CtConstructor ctor = ctClass.getConstructor("()V");

            if (!Modifier.isPublic(ctor.getModifiers())) return;
        }
        catch (NotFoundException ex)
        {
            return;
        }
View Full Code Here

                    hasDefaultConstructor = true;
                    break;
                }
            }
            if (!hasDefaultConstructor && !ctClass.isInterface()) {
                CtConstructor defaultConstructor = CtNewConstructor.make("public " + ctClass.getSimpleName() + "() {}", ctClass);
                ctClass.addConstructor(defaultConstructor);
            }
        } catch (Exception e) {
            Logger.error(e, "Error in PropertiesEnhancer");
            throw new UnexpectedException("Error in PropertiesEnhancer", e);
        }

        if (isScala(applicationClass)) {
            // Temporary hack for Scala. Done.
            applicationClass.enhancedByteCode = ctClass.toBytecode();
            ctClass.defrost();
            return;
        }

        for (CtField ctField : ctClass.getDeclaredFields()) {
            try {

                if (isProperty(ctField)) {

                    // Property name
                    String propertyName = ctField.getName().substring(0, 1).toUpperCase() + ctField.getName().substring(1);
                    String getter = "get" + propertyName;
                    String setter = "set" + propertyName;

                    try {
                        CtMethod ctMethod = ctClass.getDeclaredMethod(getter);
                        if (ctMethod.getParameterTypes().length > 0 || Modifier.isStatic(ctMethod.getModifiers())) {
                            throw new NotFoundException("it's not a getter !");
                        }
                    } catch (NotFoundException noGetter) {

                        // Créé le getter
                        String code = "public " + ctField.getType().getName() + " " + getter + "() { return this." + ctField.getName() + "; }";
                        CtMethod getMethod = CtMethod.make(code, ctClass);
                        getMethod.setModifiers(getMethod.getModifiers() | AccessFlag.SYNTHETIC);
                        ctClass.addMethod(getMethod);
                    }

                    try {
                        CtMethod ctMethod = ctClass.getDeclaredMethod(setter);
                        if (ctMethod.getParameterTypes().length != 1 || !ctMethod.getParameterTypes()[0].equals(ctField.getType()) || Modifier.isStatic(ctMethod.getModifiers())) {
                            throw new NotFoundException("it's not a setter !");
                        }
                    } catch (NotFoundException noSetter) {
                        // Créé le setter
                        CtMethod setMethod = CtMethod.make("public void " + setter + "(" + ctField.getType().getName() + " value) { this." + ctField.getName() + " = value; }", ctClass);
                        setMethod.setModifiers(setMethod.getModifiers() | AccessFlag.SYNTHETIC);
                        ctClass.addMethod(setMethod);
                        createAnnotation(getAnnotations(setMethod), PlayPropertyAccessor.class);
                    }

                }

            } catch (Exception e) {
                Logger.error(e, "Error in PropertiesEnhancer");
                throw new UnexpectedException("Error in PropertiesEnhancer", e);
            }

        }

        // Add a default constructor if needed
        try {
            boolean hasDefaultConstructor = false;
            for (CtConstructor constructor : ctClass.getDeclaredConstructors()) {
                if (constructor.getParameterTypes().length == 0) {
                    hasDefaultConstructor = true;
                    break;
                }
            }
            if (!hasDefaultConstructor) {
                CtConstructor defaultConstructor = CtNewConstructor.defaultConstructor(ctClass);
                ctClass.addConstructor(defaultConstructor);
            }
        } catch (Exception e) {
            Logger.error(e, "Error in PropertiesEnhancer");
            throw new UnexpectedException("Error in PropertiesEnhancer", e);
View Full Code Here

            CtClass ctClass = classPool.get(constructor.getDeclaringClass().getName());
            CtClass[] cc = new CtClass[constructor.getParameterTypes().length];
            for (int i = 0; i < constructor.getParameterTypes().length; i++) {
                cc[i] = classPool.get(constructor.getParameterTypes()[i].getName());
            }
            CtConstructor ctConstructor = ctClass.getDeclaredConstructor(cc);

            // Signatures names
            CodeAttribute codeAttribute = (CodeAttribute) ctConstructor.getMethodInfo().getAttribute("Code");
            if (codeAttribute != null) {
                LocalVariableAttribute localVariableAttribute = (LocalVariableAttribute) codeAttribute.getAttribute("LocalVariableTable");
                if (localVariableAttribute != null && localVariableAttribute.tableLength() >= ctConstructor.getParameterTypes().length) {
                    for (int i = 0; i < ctConstructor.getParameterTypes().length + 1; i++) {
                        String name = localVariableAttribute.getConstPool().getUtf8Info(localVariableAttribute.nameIndex(i));
                        if (!name.equals("this")) {
                            parameters.add(name);
                        }
                    }
View Full Code Here

    // example just to set a field
    if (cc.getConstructors().length > 0) {
      cc.removeConstructor(cc.getConstructors()[0]);
    }
    CtConstructor ccCon = null;
    if (1 == nStep) {
      ccCon = CtNewConstructor.make("public TemplateClass1() { idField = \"alex_id\"; testField = \"alex\"; }", cc);
    }
    else {
      ccCon = CtNewConstructor.make("public TemplateClass2() { idField = \"alex_id2\"; testField = \"alex\"; testField2 = \"alex2\"; }", cc);     
View Full Code Here

            invokeBody.append("}");
            CtMethod invoke = CtNewMethod.make(invokeTemplate.getReturnType(), "invoke", invokeTemplate.getParameterTypes(), invokeTemplate.getExceptionTypes(), invokeBody.toString(), clazz);
            invoke.setModifiers(javassist.Modifier.PUBLIC);
            clazz.addMethod(invoke);

            CtConstructor ctor = CtNewConstructor.defaultConstructor(clazz);
            ctor.setBody("{super.adviceName = \"" + adviceName + "\";}");
            clazz.addConstructor(ctor);
           
            ProtectionDomain pd = aspect.getClass().getProtectionDomain();
            iclass = TransformerCommon.toClass(clazz, cl, pd);
         }
View Full Code Here

TOP

Related Classes of javassist.CtConstructor

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.