/** * Performs the enhancement. * * @param className The name of the class whose bytecode is being enhanced. * @param originalBytes The class's original (pre-enhancement) byte code * * @return The enhanced bytecode. Could be the same as the original bytecode if the original was * already enhanced or we could not enhance it for some reason. * * @throws EnhancementException Indicates a problem performing the enhancement */ @Override public synchronized byte[] enhance(String className, byte[] originalBytes) throws EnhancementException { CtClass managedCtClass = null; try { managedCtClass = classPool.makeClassIfNew( new ByteArrayInputStream( originalBytes ) ); return enhance( managedCtClass ) ? getByteCode( managedCtClass ) : null; } catch (IOException e) { log.unableToBuildEnhancementMetamodel( className ); return null; } finally { if ( managedCtClass != null ) { managedCtClass.detach(); } } }
public TypeInfo createTypeInfo(ResourceContext context) throws Exception { InputStream stream = context.getInputStream(); if (stream == null) throw new IllegalArgumentException("Null stream: " + context); try { CtClass ctClass = pool.makeClassIfNew(stream); return JavassistUtil.toTypeInfo(ctClass); } finally { stream.close(); } } }
@Override public MigrationResult process(String oldClassName, String newClassName, byte[] oldClassFile, Inspection transformedInspection) throws Exception { MigrationResult result = new MigrationResult(); ClassPool pool = new ClassPool(); CtClass clazz = pool.makeClassIfNew(new ByteArrayInputStream(oldClassFile)); clazz.replaceClassName(oldClassName, newClassName); @SuppressWarnings("unchecked") Class<Serializable> migratedClass = (Class<Serializable>) clazz.toClass(); result.bytecode = clazz.toBytecode(); try { Class<?> oldClass = transformedInspection.getClass(); Serializable migratedInspection = (Serializable) InstanceCreator.createInstance(migratedClass); for (Field newF : migratedClass.getDeclaredFields()) { if (java.lang.reflect.Modifier.isStatic(newF.getModifiers()) && java.lang.reflect.Modifier.isFinal(newF.getModifiers())) { continue; } Field oldF = oldClass.getDeclaredField(newF.getName()); oldF.setAccessible(true); newF.setAccessible(true); newF.set(migratedInspection, oldF.get(transformedInspection)); } result.serializedMigratedInspection = SerializationUtils.serializeToBytes(migratedInspection); } catch (Exception e) { throw new IllegalStateException("Unable to clone " + transformedInspection.getClass().getName() + " to migrated class " + migratedClass.getName(), e); } return result; }
public TypeInfo getTypeInfo(ResourceContext resource) throws Throwable { InputStream stream = resource.getInputStream(); if (stream == null) throw new IllegalArgumentException("Null stream: " + resource); try { CtClass ctClass = getClassPool(resource.getClassLoader()).makeClassIfNew(stream); return JavassistUtil.toTypeInfo(ctClass); } finally { stream.close(); } } }
try (InputStream stream = new FileInputStream(path.toFile())) { return this.pool.makeClassIfNew(stream);
/** * Performs the enhancement. * * @param className The name of the class whose bytecode is being enhanced. * @param originalBytes The class's original (pre-enhancement) byte code * * @return The enhanced bytecode. Could be the same as the original bytecode if the original was * already enhanced or we could not enhance it for some reason. * * @throws EnhancementException Indicates a problem performing the enhancement */ @Override public synchronized byte[] enhance(String className, byte[] originalBytes) throws EnhancementException { CtClass managedCtClass = null; try { managedCtClass = classPool.makeClassIfNew( new ByteArrayInputStream( originalBytes ) ); return enhance( managedCtClass ) ? getByteCode( managedCtClass ) : null; } catch (IOException e) { log.unableToBuildEnhancementMetamodel( className ); return null; } finally { if ( managedCtClass != null ) { managedCtClass.detach(); } } }
@Override public CtClass enhanceThisClass(DataInputStream dataInputStream) throws Exception { CtClass ctClass = classPool.makeClassIfNew(dataInputStream); if (!ctClass.subtypeOf(classPool.get("net.csdn.mongo.Document"))) { return ctClass; } CtClass document = ctClass.getSuperclass(); //copy static fields to subclass.Importance because of inheritance strategy of java copyStaticFieldsToSubclass(document, ctClass); //copy static methods to subclass copyStaticMethodsToSubclass(document, ctClass); enhanceCriteriaClassMethods(ctClass); //enhance getter/setter methods to put them into attributes field enhanceGetterSetterMethods(ctClass); //enhance related association enhanceAssociationMethods(ctClass); //enhance embedded association enhanceAssociationEmbedded(ctClass); return ctClass; }
@Override public CtClass enhanceThisClass(DataInputStream dataInputStream) throws Exception { CtClass ctClass = classPool.makeClassIfNew(dataInputStream); if (!ctClass.subtypeOf(classPool.get("net.csdn.mongo.Document"))) { return ctClass; } CtClass document = ctClass.getSuperclass(); //copy static fields to subclass.Importance because of inheritance strategy of java copyStaticFieldsToSubclass(document, ctClass); //copy static methods to subclass copyStaticMethodsToSubclass(document, ctClass); enhanceCriteriaClassMethods(ctClass); //enhance getter/setter methods to put them into attributes field enhanceGetterSetterMethods(ctClass); //enhance related association enhanceAssociationMethods(ctClass); //enhance embedded association enhanceAssociationEmbedded(ctClass); return ctClass; }