Package javassist.bytecode

Examples of javassist.bytecode.ClassFile


   }

   public int doCheck(ClassLoader cl, CheckClassInfo info)
   {
      CtClass clazz = info.getClazz();
      ClassFile file = clazz.getClassFile();
      ClassPool pool = clazz.getClassPool();
     
      int failed = 0;
     
      // Incorrect major version
      int major = file.getMajorVersion();
      if (major > 48)
      {
         if (failed == 0)
            System.out.println("==== " + info.getFile());
         System.out.println("Wrong major version " + major);
         ++failed;
      }

      // Check the super class exists
      String superClassName = file.getSuperclass();
      try
      {
         pool.get(superClassName);
      }
      catch (NotFoundException e)
      {
         if (failed == 0)
            System.out.println("==== " + info.getFile());
         System.out.println("SuperClass not found " + superClassName);
         ++failed;
      }
     
      // Check the interfaces exist
      String[] intfs = file.getInterfaces();
      for (String intf : intfs)
      {
         try
         {
            pool.get(intf);
         }
         catch (NotFoundException e)
         {
            if (failed == 0)
               System.out.println("==== " + info.getFile());
            System.out.println("Interface not found " + intf);
            ++failed;
         }
      }

      // Check the field types
      List<FieldInfo> fields = (List<FieldInfo>) file.getFields();
      for (FieldInfo field : fields)
      {
         String name = field.getName();
         String typeName = Descriptor.toJavaName(field.getDescriptor());
         try
         {
            CtField ctField = clazz.getField(name);
            ctField.getType();
         }
         catch (NotFoundException e)
         {
            if (failed == 0)
               System.out.println("==== " + info.getFile());
            System.out.println("Class not found " + typeName + " for field " + name);
            ++failed;
         }
      }

      // Check the method types
      List<MethodInfo> methods = (List<MethodInfo>) file.getMethods();
      for (MethodInfo method : methods)
      {
         String name = method.getName();
         String descriptor = method.getDescriptor();
        
         if (CLINIT.equals(name))
            continue;
        
         try
         {
            if (INIT.equals(name) == false)
            {
               CtMethod ctMethod = clazz.getMethod(name, descriptor);
               ctMethod.getReturnType();
            }
         }
         catch (NotFoundException e)
         {
            if (failed == 0)
               System.out.println("==== " + info.getFile());
            System.out.println("Return type not found for method " + name + "." + descriptor);
            ++failed;
         }
        
         if (INIT.equals(name))
         {
            try
            {
               CtConstructor ctConstructor = clazz.getConstructor(descriptor);
               ctConstructor.getParameterTypes();
            }
            catch (NotFoundException e)
            {
               if (failed == 0)
                  System.out.println("==== " + info.getFile());
               System.out.println("Cannot find constructor parameter types " + name + "." + descriptor);
               ++failed;
            }
            try
            {
               CtConstructor ctConstructor = clazz.getConstructor(descriptor);
               ctConstructor.getExceptionTypes();
            }
            catch (NotFoundException e)
            {
               if (failed == 0)
                  System.out.println("==== " + info.getFile());
               System.out.println("Cannot find constructor exception types " + name + "." + descriptor);
               ++failed;
            }
         }
         else
         {
            try
            {
               CtMethod ctMethod = clazz.getMethod(name, descriptor);
               ctMethod.getParameterTypes();
            }
            catch (NotFoundException e)
            {
               if (failed == 0)
                  System.out.println("==== " + info.getFile());
               System.out.println("Cannot find method parameter types " + name + "." + descriptor);
               ++failed;
            }
            try
            {
               CtMethod ctMethod = clazz.getMethod(name, descriptor);
               ctMethod.getExceptionTypes();
            }
            catch (NotFoundException e)
            {
               if (failed == 0)
                  System.out.println("==== " + info.getFile());
               System.out.println("Cannot find method exception types " + name + "." + descriptor);
               ++failed;
            }
         }
      }

      // Now we've checked the signatures of the class, let's look at the references
      file.compact();
      ConstPool consts = file.getConstPool();
      for (int i = 1; i < consts.getSize(); ++i) // Yes start at 1
      {
         switch (consts.getTag(i))
         {
            case ConstPool.CONST_Fieldref:
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

   }

   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

   private ArrayList<URL> classPathUrls = new ArrayList<URL>();
  
   public boolean doWeave(ClassLoader cl, ClassFileInfo info) throws BadBytecode, CannotCompileException
   {
      CtClass clazz = info.getClazz();
      ClassFile file = clazz.getClassFile();

      // Rename known classes
      file.renameClass(classRenames);

      // Run the converters
      for (CodeConverter converter : this.codeConverters)
      {
         clazz.instrument(converter);
View Full Code Here

      }
      else
      {
         dis = new DataInputStream(is);
      }
      ClassFile cf = new ClassFile(dis);
      dis.close();
      return cf;
   }
View Full Code Here

    Method[] setters = new Method[setterNames.length];
    findAccessors( targetBean, getterNames, setterNames, types, getters, setters );

    Class beanClass;
    try {
      ClassFile classfile = make( getters, setters );
      ClassLoader loader = this.getClassLoader();
      if ( writeDirectory != null ) {
        FactoryHelper.writeFile( classfile, writeDirectory );
      }
View Full Code Here

    className = className + "_$$_bulkaccess_" + counter++;
    if ( className.startsWith( "java." ) ) {
      className = "org.javassist.tmp." + className;
    }

    ClassFile classfile = new ClassFile( false, className, BULKACESSOR_CLASS_NAME );
    classfile.setAccessFlags( AccessFlag.PUBLIC );
    addDefaultConstructor( classfile );
    addGetter( classfile, getters );
    addSetter( classfile, setters );
    return classfile;
  }
View Full Code Here

    filter = f;
  }

  public void transform(File file) throws Exception {
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    ClassFile classfile = new ClassFile(in);
    transform(classfile);
    DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
    try {
      classfile.write(out);
    } finally {
      out.close();
    }
  }
View Full Code Here

      ClassLoader loader,
      String className,
      Class classBeingRedefined,
      ProtectionDomain protectionDomain,
      byte[] classfileBuffer) {
    ClassFile classfile;
    try {
      // WARNING: classfile only
      classfile = new ClassFile( new DataInputStream( new ByteArrayInputStream( classfileBuffer ) ) );
    }
    catch (IOException e) {
      LOG.unableToBuildEnhancementMetamodel( className );
      return classfileBuffer;
    }
    // This is the same as ClassPool.getDefault() but ensures a new ClassPool per
    ClassPool cp = new ClassPool();
    cp.appendSystemPath();
    cp.appendClassPath(new ClassClassPath(this.getClass()));
    cp.appendClassPath(new ClassClassPath(classfile.getClass()));
    try {
      cp.makeClassIfNew(new ByteArrayInputStream(classfileBuffer));
    } catch (IOException e) {
      throw new RuntimeException(e.getMessage(), e);
    }
    FieldTransformer transformer = getFieldTransformer( classfile, cp );
    if ( transformer != null ) {
      LOG.debugf( "Enhancing %s", className );
      DataOutputStream out = null;
      try {
        transformer.transform( classfile );
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        out = new DataOutputStream( byteStream );
        classfile.write( out );
        return byteStream.toByteArray();
      }
      catch (Exception e) {
        LOG.unableToTransformClass( e.getMessage() );
        throw new HibernateException( "Unable to transform class: " + e.getMessage() );
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

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.