Package javassist.bytecode

Examples of javassist.bytecode.ClassFile


    {
        String classname = annotation.getTypeName();
        if (pool != null) {
            try {
                CtClass cc = pool.get(classname);
                ClassFile cf = cc.getClassFile2();
                MethodInfo minfo = cf.getMethod(name);
                if (minfo != null) {
                    AnnotationDefaultAttribute ainfo
                        = (AnnotationDefaultAttribute)
                          minfo.getAttribute(AnnotationDefaultAttribute.tag);
                    if (ainfo != null) {
View Full Code Here


   }

   protected void deployElement(InputStream stream, Ejb3HandlerFactory factory, InitialContext ctx) throws Exception
   {
      DataInputStream dstream = new DataInputStream(new BufferedInputStream(stream));
      ClassFile cf = null;
      try
      {
         cf = new ClassFile(dstream);
      }
      finally
      {
         dstream.close();
         stream.close();
View Full Code Here

     * @throws IOException
     */
    public void scanClass(InputStream bits) throws IOException
    {
        DataInputStream dstream = new DataInputStream(new BufferedInputStream(bits));
        ClassFile cf;
        try
        {
            cf = new ClassFile(dstream);
            classIndex.put(cf.getName(), new HashSet<String>());

            if (scanClassAnnotations)
            {
                scanClass(cf);
            }

            if (scanMethodAnnotations || scanParameterAnnotations)
            {
                scanMethods(cf);
            }

            if (scanFieldAnnotations)
            {
                scanFields(cf);
            }

            // create an index of interfaces the class implements
            if (cf.getInterfaces() != null)
            {
                Set<String> intfs = new HashSet<String>();
                Collections.addAll(intfs, cf.getInterfaces());
                implementsIndex.put(cf.getName(), intfs);
            }

        }
        finally
        {
View Full Code Here

      }
      DataInputStream dstream = new DataInputStream(stream);

      try
      {
         return new ClassFile(dstream);
      }
      finally
      {
         dstream.close();
         stream.close();
View Full Code Here

      {
         String classname = filenameToClassname(name);
         String filename = componentFilename(name);
         try
         {
            ClassFile classFile = getClassFile(name, classLoader);
            boolean installable = ( hasAnnotation(classFile, Name.class) || classLoader.getResources(filename).hasMoreElements() )
                     && !"false".equals( getAnnotationValue(classFile, Install.class, "value") );
            if (installable)
            {
               log.trace("found component class: " + name);
View Full Code Here

      if (name.endsWith(".class"))
      {
         String classname = filenameToClassname(name);
         try
         {
            ClassFile classFile = getClassFile(name, classLoader);
            Class clazz = null;
            for (Class<? extends Annotation> annotationType: annotations)
            {
               if (hasAnnotation(classFile, annotationType))
               {
View Full Code Here

        throws javassist.NotFoundException, CannotCompileException, InvocationTargetException, IllegalAccessException
    {
        ClassPool pool = ClassPool.getDefault();
        CtClass cc = pool.get(clazz.getCanonicalName());
        for (CtMethod method : cc.getDeclaredMethods()) {
            ClassFile ccFile = cc.getClassFile();
            ConstPool constpool = ccFile.getConstPool();
            AnnotationsAttribute attr;
            attr = filterExistingAnnotations(add, method);
            addNewAnnotations(add, method, constpool, pool, attr);
        }
        cc.setName(cc.getName() + "$ClassModifier$" + COUNTER.increment());
View Full Code Here

   * @throws IOException
   */
  public void scanClass(InputStream bits) throws IOException
  {
    DataInputStream dstream = new DataInputStream(new BufferedInputStream(bits));
    ClassFile cf = null;
    try
    {
      cf = new ClassFile(dstream);
      classIndex.put(cf.getName(), new HashSet<String>());
      scanClass(cf);
      if (scanMethodAnnotations || scanParameterAnnotations)
      {
        scanMethods(cf);
      }
      if (scanFieldAnnotations)
      {
        scanFields(cf);
      }

      // create an index of interfaces the class implements
      if (cf.getInterfaces() != null)
      {
        Set<String> intfs = new HashSet<String>();
        for (String intf : cf.getInterfaces())
        {
          intfs.add(intf);
        }
        implementsIndex.put(cf.getName(), intfs);
      }
    }
    finally
    {
      dstream.close();
View Full Code Here

    public static Object generate(Object service, Map<String, String> commands, String suffix) throws Exception {
        // generate class with unique name
        CtClass ctClass = POOL.makeClass(AbstractFelixCommandsService.class.getName() + suffix);
        try {
            if (!ctClass.isFrozen()) {
                ClassFile ccFile = ctClass.getClassFile();
                ccFile.setVersionToJava5();
                ConstPool constPool = ccFile.getConstPool();

                // set superclass
                CtClass abstractCtClass = POOL.getCtClass(AbstractFelixCommandsService.class.getName());
                ctClass.setSuperclass(abstractCtClass);
View Full Code Here

                    return cached.toClass();
                }
            }

            CtClass originalClass = classPool.get(intf.getName());
            ClassFile originalClassFile = originalClass.getClassFile();

            CtClass newClass = classPool.makeInterface(simplifiedName);

            newClass.defrost();

            ClassFile newClassFile = newClass.getClassFile();

            //we'll be adding new constants to the class file (for generics and annotations)
            ConstPool constPool = newClassFile.getConstPool();

            //copy the annotations on the class
            AnnotationsAttribute annotations = (AnnotationsAttribute) originalClassFile
                .getAttribute(AnnotationsAttribute.visibleTag);
            AnnotationsAttribute newAnnotations = copyAnnotations(annotations, constPool);

            //add our @Simplified annotation to the new class
            newAnnotations = addSimplifiedClassAnnotation(originalClass.getName(), newAnnotations, constPool);
            newClassFile.addAttribute(newAnnotations);

            //copy the generic signature of the class
            SignatureAttribute signature = (SignatureAttribute) originalClassFile.getAttribute(SignatureAttribute.tag);
            if (signature != null) {
                newClassFile.addAttribute(new SignatureAttribute(constPool, signature.getSignature()));
            }

            //now copy over the methods
            CtMethod[] methods = originalClass.getMethods();

            for (CtMethod originalMethod : methods) {

                //we are only simplifying interfaces here, but the CtClass.getMethods() also returns concrete methods
                //inherited from Object. Let's just skip those - we don't need to worry about them...
                if (!Modifier.isAbstract(originalMethod.getModifiers())) {
                    continue;
                }

                CtClass[] params = originalMethod.getParameterTypes();

                //capture all the runtime visible method annotations on the original method
                annotations = (AnnotationsAttribute) originalMethod.getMethodInfo().getAttribute(
                    AnnotationsAttribute.visibleTag);

                //capture all the runtime visible parameter annotations on the original method
                ParameterAnnotationsAttribute parameterAnnotations = (ParameterAnnotationsAttribute) originalMethod
                    .getMethodInfo().getAttribute(ParameterAnnotationsAttribute.visibleTag);

                //capture the generic signature of the original method.
                signature = (SignatureAttribute) originalMethod.getMethodInfo().getAttribute(
                    SignatureAttribute.tag);

                boolean simplify = params.length > 0 && params[0].getName().equals(Subject.class.getName());

                if (simplify) {
                    //generate new params, leaving out the first parameter (the subject)
                    CtClass[] simpleParams = new CtClass[params.length - 1];

                    System.arraycopy(params, 1, simpleParams, 0, params.length - 1);
                    params = simpleParams;
                }

                //generate the new method with possibly modified parameters
                CtMethod newMethod = CtNewMethod.abstractMethod(originalMethod.getReturnType(),
                    originalMethod.getName(), params, originalMethod.getExceptionTypes(), newClass);

                //copy over the method annotations
                annotations = copyAnnotations(annotations, constPool);

                if (simplify) {
                    //add the @SimplifiedMethod to the method annotations
                    annotations = addSimplifiedMethodAnnotation(annotations, constPool);

                    if (signature != null) {
                        //fun, we need to modify the signature, too, because we have left out the parameter
                        MethodSignature sig = MethodSignature.parse(signature.getSignature());

                        sig.paramTypes.remove(0);

                        signature = new SignatureAttribute(constPool, sig.toString());
                    }

                    //next, we need to copy the parameter annotations
                    parameterAnnotations = copyParameterAnnotations(parameterAnnotations, constPool, 1);
                } else {
                    //just copy the sig and parameter annotations verbatim
                    if (signature != null) {
                        signature = new SignatureAttribute(constPool, signature.getSignature());
                    }

                    parameterAnnotations = copyParameterAnnotations(parameterAnnotations, constPool, 0);
                }

                if (parameterAnnotations != null) {
                    newMethod.getMethodInfo().addAttribute(parameterAnnotations);
                }

                if (signature != null) {
                    newMethod.getMethodInfo().addAttribute(signature);
                }

                if (annotations != null) {
                    newMethod.getMethodInfo().addAttribute(annotations);
                }

                //it is important to add the method directly to the classfile, not the class
                //because otherwise the generics info wouldn't survive
                newClassFile.addMethod(newMethod.getMethodInfo());
            }

            return newClass.toClass();

        } catch (Exception e) {
View Full Code Here

TOP

Related Classes of javassist.bytecode.ClassFile

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.