Package javassist

Examples of javassist.CtField$LongInitializer


        CtClass ctType = toCtClass(type);

        try
        {
            CtField field = new CtField(ctType, name, getCtClass());
            field.setModifiers(modifiers);

            getCtClass().addField(field);
        }
        catch (CannotCompileException ex)
        {
View Full Code Here


    {
        CtClass ctType = _source.getCtClass(type);

        try
        {
            CtField field = new CtField(ctType, name, _ctClass);
            field.setModifiers(Modifier.PRIVATE);

            _ctClass.addField(field);
        }
        catch (CannotCompileException ex)
        {
View Full Code Here

        return annotations;
    }

    private List<Annotation> findAnnotationsForField(String fieldName)
    {
        CtField field = findDeclaredCtField(fieldName);

        return extractAnnotations(field);
    }
View Full Code Here

    private CtClass getFieldCtType(String fieldName)
    {
        try
        {
            CtField field = _ctClass.getDeclaredField(fieldName);

            return field.getType();
        }
        catch (NotFoundException ex)
        {
            throw new RuntimeException(ex);
        }
View Full Code Here

        try
        {
            CtClass ctType = convertNameToCtType(type);

            CtField field = new CtField(ctType, fieldName, _ctClass);
            field.setModifiers(modifiers);

            _ctClass.addField(field);
        }
        catch (NotFoundException ex)
        {
View Full Code Here

        {
            for (String fieldName : _removedFieldNames)
            {
                try
                {
                    CtField field = _ctClass.getDeclaredField(fieldName);
                    _ctClass.removeField(field);
                }
                catch (NotFoundException ex)
                {
                    throw new RuntimeException(ex);
View Full Code Here

            String parameterTypeName = advisedMethod.getParameterTypes()[i];

            CtClass parameterType = classSource.toCtClass(parameterTypeName);

            CtField field = new CtField(parameterType, name, invocationCtClass);
            field.setModifiers(Modifier.PRIVATE);
            invocationCtClass.addField(field);

            parameterTypes[2 + i] = parameterType;

            builder.addln("%s = $%d;", name, 3 + i);
View Full Code Here

       */
      CtClass superclass = implementation.getSuperclass();
      if (!isInstrumented(superclass)) {
         if (!isInstrumentable(superclass)) {
            //we're the top-most instrumentable class, so add the handler field
           CtField handlerField = new CtField(handlerClass, "handler", implementation);
           handlerField.setModifiers(Modifier.PROTECTED);
           Initializer handlerInitializer = Initializer.byCall(handlerClass, "create");
           implementation.addField(handlerField, handlerInitializer);
           CtMethod getHandlerMethod = CtNewMethod.getter("getHandler", handlerField);
           implementation.addMethod(getHandlerMethod);
         }
         else {
            //in order for the below code to make reference to the handler instance we need to
            //recursively instrument until we reach the top of the instrumentable class tree
            instrumentClass(superclass);
         }
      }

      CtField wicketComponentField = new CtField(componentClass, "component", implementation);
      wicketComponentField.setModifiers(Modifier.STATIC);
      Initializer componentInit = Initializer.byExpr("new org.jboss.seam.wicket.WicketComponent(" + className + ".class)");
      implementation.addField(wicketComponentField, componentInit);

      CtClass exception = classPool.get(Exception.class.getName());

View Full Code Here

            ResourceBundle resBundle = ResourceBundle.getBundle(ifaceName, locale);
            CtConstructor ctorMethod = new CtConstructor(new CtClass[] {localeClass}, impl);
            impl.addConstructor(ctorMethod);
            ctorMethod.setModifiers(Modifier.PUBLIC);
            ctorMethod.setBody("super();");
            CtField localeField = new CtField(localeClass, "_locale", impl);
            impl.addField(localeField, CtField.Initializer.byParameter(0));
            TreeMap<String,String> keys = new TreeMap<String,String>();
            for(CtMethod method : iface.getMethods()) {
                // Only abstract methods
                if((method.getModifiers() & Modifier.ABSTRACT) == 0)
                    continue;
                CtMethod methodImpl = new CtMethod(method, impl, null);
                String methodKey = null;
                String meaning = null;
                String defaultValue = null;
                for(Object ann : method.getAnnotations()) {
                    if(ann instanceof Key) {
                        methodKey = ((Key)ann).value();
                    } else if(ann instanceof Meaning) {
                        meaning = ((Meaning)ann).value();
                    } else if(ann instanceof DefaultStringValue) {
                        defaultValue = ((DefaultStringValue)ann).value();
                    } else if(ann instanceof DefaultBooleanValue) {
                        defaultValue = String.valueOf(((DefaultBooleanValue)ann).value());
                    } else if(ann instanceof DefaultDoubleValue) {
                        defaultValue = String.valueOf(((DefaultDoubleValue)ann).value());
                    } else if(ann instanceof DefaultMessage) {
                        defaultValue = ((DefaultMessage)ann).value();
                    }
                }
                if(methodKey == null) {
                    methodKey = keyGenerator.generateKey(ifaceName, method.getName(), defaultValue, meaning);
                }
                String value;
                try {
                    value = resBundle.getString(methodKey);
                } catch (java.util.MissingResourceException mre) {
                    if(defaultValue != null) {
                        value = defaultValue;
                    } else throw mre;
                }
                CtField patternField = new CtField(stringClass, method.getName()+"Pattern", impl);
                impl.addField(patternField, CtField.Initializer.constant(value));
                CtField formatField = new CtField(messageFormat, method.getName()+"MessageFormat", impl);
                impl.addField(formatField, CtField.Initializer.byExpr("new java.text.MessageFormat("+patternField.getName()+", _locale)"));
                methodImpl.setModifiers(Modifier.PUBLIC);
                methodImpl.setBody("return "+formatField.getName()+".format($args, new StringBuffer(), null).toString();");
                impl.addMethod(methodImpl);
                keys.put(methodKey, defaultValue);
            }
            final Class implClazz = impl.toClass();
            final Constructor ctor = implClazz.getConstructor(Locale.class);
View Full Code Here

            CtClass baseConstantsImpl = classPool.get(BaseConstantsImpl.class.getName());
            CtClass stringClass = classPool.get(String.class.getName());
            CtClass impl = classPool.makeClass(ifaceName+"Impl_"+locale.toString(), baseConstantsImpl);
            impl.addInterface(iface);
            final CtClass resourceBundle = classPool.get(ResourceBundle.class.getName());
            CtField resField = new CtField(resourceBundle, "res", impl);
            impl.addField(resField);
           
            KeyGenerator keyGenerator = new MethodNameKeyGenerator();
            for(Object ann : iface.getAnnotations()) {
                if(ann instanceof GenerateKeys) {
                    String keyGeneratorClassName = ((GenerateKeys)ann).value();
                    Class<KeyGenerator> keyGeneratorClass;
                    try {
                        keyGeneratorClass = (Class<KeyGenerator>) Class.forName(keyGeneratorClassName);
                    } catch(ClassNotFoundException cnfe) {
                        keyGeneratorClass = (Class<KeyGenerator>) Class.forName(KeyGenerator.class.getName()+"."+keyGeneratorClassName);
                    }
                    keyGenerator = keyGeneratorClass.newInstance();
                }
            }
           
            ResourceBundle resBundle = ResourceBundle.getBundle(ifaceName, locale);
            for(CtMethod method : iface.getMethods()) {
                // Only abstract methods
                if((method.getModifiers() & Modifier.ABSTRACT) == 0)
                    continue;
                String methodKey = null;
                String meaning = null;
                Object defaultValue = null;
                for(Object ann : method.getAnnotations()) {
                    if(ann instanceof Key) {
                        methodKey = ((Key)ann).value();
                    } else if(ann instanceof Meaning) {
                        meaning = ((Meaning)ann).value();
                    } else if(ann instanceof DefaultStringValue) {
                        defaultValue = ((DefaultStringValue)ann).value();
                    } else if(ann instanceof DefaultBooleanValue) {
                        defaultValue = ((DefaultBooleanValue)ann).value();
                    } else if(ann instanceof DefaultDoubleValue) {
                        defaultValue = ((DefaultDoubleValue)ann).value();
                    } else if(ann instanceof DefaultIntValue) {
                        defaultValue = ((DefaultIntValue)ann).value();
                    } else if(ann instanceof DefaultFloatValue) {
                        defaultValue = ((DefaultFloatValue)ann).value();
                    } else if(ann instanceof DefaultMessage) {
                        defaultValue = ((DefaultMessage)ann).value();
                    }
                }
                if(methodKey == null) {
                    methodKey = keyGenerator.generateKey(ifaceName, method.getName(), String.valueOf(defaultValue), meaning);
                }
                CtMethod methodImpl = new CtMethod(method, impl, null);
                CtClass returnType = methodImpl.getReturnType();
                Initializer initializer;
                String valueString;
                try {
                    valueString = resBundle.getString(methodKey);
                } catch(MissingResourceException mre) {
                    if(defaultValue != null)
                        valueString = String.valueOf(defaultValue);
                    else
                        throw mre;
                }
                if(returnType.isPrimitive()) {
                    if(returnType.getSimpleName().equals("int"))
                        initializer = CtField.Initializer.constant(Integer.parseInt(valueString));
                    else if(returnType.getSimpleName().equals("float"))
                        initializer = CtField.Initializer.byExpr(Float.parseFloat(valueString)+"f");
                    else if(returnType.getSimpleName().equals("double"))
                        initializer = CtField.Initializer.constant(Double.parseDouble(valueString));
                    else if(returnType.getSimpleName().equals("boolean"))
                        initializer = CtField.Initializer.byExpr(String.valueOf(Boolean.parseBoolean(valueString)));
                    else throw new IllegalStateException(returnType+" is not a supported primitive return type of a constant in "+iface.getName());
                } else if(returnType.equals(stringClass)) {
                    initializer = CtField.Initializer.constant(valueString);
                } else if(returnType.getSimpleName().equals("Map")){
                    CtField keyField = new CtField(stringClass, method.getName().toUpperCase(), impl);
                    keyField.setModifiers(Modifier.FINAL|Modifier.STATIC);
                    impl.addField(keyField, CtField.Initializer.constant(methodKey));
                    initializer = CtField.Initializer.byExpr("getMap("+keyField.getName()+")");
                } else if(returnType.getSimpleName().equals("String[]")){
                    CtField keyField = new CtField(stringClass, method.getName().toUpperCase(), impl);
                    keyField.setModifiers(Modifier.FINAL|Modifier.STATIC);
                    impl.addField(keyField, CtField.Initializer.constant(methodKey));
                    initializer = CtField.Initializer.byExpr("getStringArray("+keyField.getName()+")");
                    returnType = classPool.get(String[].class.getName());
                } else throw new IllegalStateException(returnType+" is not a supported return type of a constant in "+iface.getName());
               
                CtField valueField = new CtField(returnType, method.getName(), impl);
                impl.addField(valueField, initializer);
                //sb.append("this."+valueField.getName()+" = get"+getter+"(\""+methodKey+"\"));\n");
                methodImpl.setModifiers(Modifier.PUBLIC);
                methodImpl.setBody("return this."+valueField.getName()+";");
                impl.addMethod(methodImpl);
            }
            CtConstructor ctor = new CtConstructor(new CtClass[]{resourceBundle}, impl);
            ctor.setModifiers(Modifier.PUBLIC);
            ctor.setExceptionTypes(new CtClass[] {classPool.get(MissingResourceException.class.getName())});
View Full Code Here

TOP

Related Classes of javassist.CtField$LongInitializer

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.