public String apply(StaticFieldDescriptor from) { // this is quite simple. First we create a proxy String proxyName = ProxyDefinitionStore.getProxyName(); ClassFile proxy = new ClassFile(false, proxyName, "java.lang.Object"); ClassDataStore.instance().registerProxyName(from.getClazz(), proxyName); proxy.setAccessFlags(AccessFlag.PUBLIC); FieldInfo newField = new FieldInfo(proxy.getConstPool(), from.getName(), from.getDescriptor()); newField.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.STATIC); if (from.getSigniture() != null) { SignatureAttribute sig = new SignatureAttribute(proxy.getConstPool(), from.getSigniture()); newField.addAttribute(sig); } try { proxy.addField(newField); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bytes); try { proxy.write(dos); } catch (IOException e) { throw new RuntimeException(e); } ProxyDefinitionStore.saveProxyDefinition( from.getClazz().getClassLoader(), proxyName, bytes.toByteArray()); } catch (DuplicateMemberException e) { // can't happen } return proxyName; }
/** * Client proxies are equal to other client proxies for the same bean. * * <p>The corresponding java code: <code> * return other instanceof MyProxyClassType.class * </code> */ @Override protected MethodInfo generateEqualsMethod(ClassFile proxyClassType) { MethodInfo method = new MethodInfo(proxyClassType.getConstPool(), "equals", "(Ljava/lang/Object;)Z"); method.setAccessFlags(AccessFlag.PUBLIC); Bytecode b = new Bytecode(proxyClassType.getConstPool()); b.addAload(1); b.addInstanceof(proxyClassType.getName()); b.add(Opcode.IRETURN); b.setMaxLocals(2); b.setMaxStack(1); method.setCodeAttribute(b.toCodeAttribute()); return method; }
private Bytecode createAbstractMethodCode(ClassFile file, MethodInformation method) throws NotFoundException { if ((delegateField != null) && (!Modifier.isPrivate(delegateField.getModifiers()))) { // Call the corresponding method directly on the delegate Bytecode b = new Bytecode(file.getConstPool()); int localVariables = MethodUtils.calculateMaxLocals(method.getMethod()); b.setMaxLocals(localVariables); // load the delegate field b.addAload(0); b.addGetfield( file.getName(), delegateField.getName(), DescriptorUtils.classToStringRepresentation(delegateField.getType())); // load the parameters BytecodeUtils.loadParameters(b, method.getDescriptor()); // invoke the delegate method b.addInvokeinterface( delegateField.getType().getName(), method.getName(), method.getDescriptor(), localVariables); // return the value if applicable BytecodeUtils.addReturnInstruction(b, method.getReturnType()); return b; } else { if (!Modifier.isPrivate(method.getMethod().getModifiers())) { // if it is a parameter injection point we need to initalize the // injection point then handle the method with the method handler return createAbstractMethodHandler(file, method); } else { // if the delegate is private we need to use the method handler return createInterceptorBody(file, method); } } }
public void testJIRA256() throws Exception { // CtClass ec = sloader.get("test5.Entity"); CtClass cc = sloader.makeClass("test5.JIRA256"); ClassFile ccFile = cc.getClassFile(); ConstPool constpool = ccFile.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); javassist.bytecode.annotation.Annotation entityAnno = new javassist.bytecode.annotation.Annotation("test5.Entity", constpool); // = new javassist.bytecode.annotation.Annotation(constpool, ec); entityAnno.addMemberValue( "value", new javassist.bytecode.annotation.ArrayMemberValue(constpool)); attr.addAnnotation(entityAnno); ccFile.addAttribute(attr); cc.writeFile(); Object o = make(cc.getName()); assertTrue(o.getClass().getName().equals("test5.JIRA256")); java.lang.annotation.Annotation[] annotations = o.getClass().getDeclaredAnnotations(); assertEquals(1, annotations.length); }
/** Client proxies use the following hashCode: <code>MyProxyName.class.hashCode()</code> */ @Override protected MethodInfo generateHashCodeMethod(ClassFile proxyClassType) { MethodInfo method = new MethodInfo(proxyClassType.getConstPool(), "hashCode", "()I"); method.setAccessFlags(AccessFlag.PUBLIC); Bytecode b = new Bytecode(proxyClassType.getConstPool()); // MyProxyName.class.hashCode() int classLocation = proxyClassType.getConstPool().addClassInfo(proxyClassType.getName()); b.addLdc(classLocation); // now we have the class object on top of the stack b.addInvokevirtual("java.lang.Object", "hashCode", "()I"); // now we have the hashCode b.add(Opcode.IRETURN); b.setMaxLocals(1); b.setMaxStack(1); method.setCodeAttribute(b.toCodeAttribute()); return method; }
private Bytecode createAbstractMethodHandler(ClassFile file, MethodInformation methodInfo) { // this is slightly different to a normal method handler call, as we pass // in a TargetInstanceBytecodeMethodResolver. This resolver uses the // method handler to call getTargetClass to get the correct class type to // resolve the method with, and then resolves this method Bytecode b = new Bytecode(file.getConstPool()); invokeMethodHandler(file, b, methodInfo, true, TargetInstanceBytecodeMethodResolver.INSTANCE); return b; }
@Override protected void addMethodsFromClass(ClassFile proxyClassType) { Method initializerMethod = null; int delegateParameterPosition = -1; if (delegateInjectionPoint instanceof ParameterInjectionPoint<?, ?>) { ParameterInjectionPoint<?, ?> parameterIP = (ParameterInjectionPoint<?, ?>) delegateInjectionPoint; if (parameterIP.getMember() instanceof Method) { initializerMethod = ((Method) parameterIP.getMember()); delegateParameterPosition = parameterIP.getPosition(); } } try { if (delegateParameterPosition >= 0) { addHandlerInitializerMethod(proxyClassType); } Class<?> cls = getBeanType(); while (cls != null) { for (Method method : cls.getDeclaredMethods()) { MethodInformation methodInfo = new RuntimeMethodInformation(method); if (!method.getDeclaringClass().getName().equals("java.lang.Object") || method.getName().equals("toString")) { Bytecode methodBody = null; if ((delegateParameterPosition >= 0) && (initializerMethod.equals(method))) { methodBody = createDelegateInitializerCode( proxyClassType, methodInfo, delegateParameterPosition); } if (Modifier.isAbstract(method.getModifiers())) { methodBody = createAbstractMethodCode(proxyClassType, methodInfo); } if (methodBody != null) { log.trace("Adding method " + method); proxyClassType.addMethod( MethodUtils.makeMethod( methodInfo, method.getExceptionTypes(), methodBody, proxyClassType.getConstPool())); } } } cls = cls.getSuperclass(); } } catch (Exception e) { throw new WeldException(e); } }
private void addHandlerInitializerMethod(ClassFile proxyClassType) throws Exception { StaticMethodInformation methodInfo = new StaticMethodInformation( "_initMH", new Class[] {Object.class}, void.class, proxyClassType.getName(), Modifier.PRIVATE); proxyClassType.addMethod( MethodUtils.makeMethod( methodInfo, new Class[] {}, createMethodHandlerInitializerBody(proxyClassType), proxyClassType.getConstPool())); }
/** * calls _initMH on the method handler and then stores the result in the methodHandler field as * then new methodHandler */ private Bytecode createMethodHandlerInitializerBody(ClassFile proxyClassType) { Bytecode b = new Bytecode(proxyClassType.getConstPool(), 1, 2); b.add(Opcode.ALOAD_0); StaticMethodInformation methodInfo = new StaticMethodInformation( "_initMH", new Class[] {Object.class}, void.class, proxyClassType.getName()); invokeMethodHandler(proxyClassType, b, methodInfo, false, DEFAULT_METHOD_RESOLVER); b.addCheckcast("javassist/util/proxy/MethodHandler"); b.addPutfield( proxyClassType.getName(), "methodHandler", DescriptorUtils.classToStringRepresentation(MethodHandler.class)); b.add(Opcode.RETURN); log.trace("Created MH initializer body for decorator proxy: " + getBeanType()); return b; }
/** * When creates the delegate initializer code when the delegate is injected into a method. * * <p>super initializer method is called first, and then _initMH is called * * @param file * @param intializerMethodInfo * @param delegateParameterPosition * @return */ private Bytecode createDelegateInitializerCode( ClassFile file, MethodInformation intializerMethodInfo, int delegateParameterPosition) { Bytecode b = new Bytecode(file.getConstPool()); // we need to push all the pareters on the stack to call the corresponding // superclass arguments b.addAload(0); // load this int localVariables = 1; int actualDelegateParamterPosition = 0; for (int i = 0; i < intializerMethodInfo.getMethod().getParameterTypes().length; ++i) { if (i == delegateParameterPosition) { // figure out the actual position of the delegate in the local // variables actualDelegateParamterPosition = localVariables; } Class<?> type = intializerMethodInfo.getMethod().getParameterTypes()[i]; BytecodeUtils.addLoadInstruction( b, DescriptorUtils.classToStringRepresentation(type), localVariables); if (type == long.class || type == double.class) { localVariables = localVariables + 2; } else { localVariables++; } } b.addInvokespecial( file.getSuperclass(), intializerMethodInfo.getName(), intializerMethodInfo.getDescriptor()); // if this method returns a value it is now sitting on top of the stack // we will leave it there are return it later // now we need to call _initMH b.addAload(0); // load this b.addAload(actualDelegateParamterPosition); // load the delegate b.addInvokevirtual(file.getName(), "_initMH", "(Ljava/lang/Object;)V"); // return the object from the top of the stack that we got from calling // the superclass method earlier BytecodeUtils.addReturnInstruction(b, intializerMethodInfo.getReturnType()); b.setMaxLocals(localVariables); return b; }
@Override public byte[] transform( ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { // Be careful with Apache library usage in this class (e.g. ArrayUtils). Usage will likely cause // a ClassCircularityError // under JRebel. Favor not including outside libraries and unnecessary classes. CtClass clazz = null; try { boolean mySkipOverlaps = skipOverlaps; boolean myRenameMethodOverlaps = renameMethodOverlaps; String convertedClassName = className.replace('/', '.'); ClassPool classPool = null; String xformKey = convertedClassName; String[] xformVals = null; Boolean[] xformSkipOverlaps = null; Boolean[] xformRenameMethodOverlaps = null; if (!xformTemplates.isEmpty()) { if (xformTemplates.containsKey(xformKey)) { xformVals = xformTemplates.get(xformKey).split(","); classPool = ClassPool.getDefault(); clazz = classPool.makeClass(new ByteArrayInputStream(classfileBuffer), false); } } else { if (annotationTransformedClasses.contains(convertedClassName)) { logger.warn( convertedClassName + " has already been transformed by a previous instance of DirectCopyTransfomer. " + "Skipping this annotation based transformation. Generally, annotation-based transformation is handled " + "by bean id blAnnotationDirectCopyClassTransformer with template tokens being added to " + "blDirectCopyTransformTokenMap via EarlyStageMergeBeanPostProcessor."); } boolean isValidPattern = true; List<DirectCopyIgnorePattern> matchedPatterns = new ArrayList<DirectCopyIgnorePattern>(); for (DirectCopyIgnorePattern pattern : ignorePatterns) { boolean isPatternMatch = false; for (String patternString : pattern.getPatterns()) { isPatternMatch = convertedClassName.matches(patternString); if (isPatternMatch) { break; } } if (isPatternMatch) { matchedPatterns.add(pattern); } isValidPattern = !(isPatternMatch && pattern.getTemplateTokenPatterns() == null); if (!isValidPattern) { return null; } } if (isValidPattern) { classPool = ClassPool.getDefault(); clazz = classPool.makeClass(new ByteArrayInputStream(classfileBuffer), false); List<?> attributes = clazz.getClassFile().getAttributes(); Iterator<?> itr = attributes.iterator(); List<String> templates = new ArrayList<String>(); List<Boolean> skips = new ArrayList<Boolean>(); List<Boolean> renames = new ArrayList<Boolean>(); check: { while (itr.hasNext()) { Object object = itr.next(); if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { AnnotationsAttribute attr = (AnnotationsAttribute) object; Annotation[] items = attr.getAnnotations(); for (Annotation annotation : items) { String typeName = annotation.getTypeName(); if (typeName.equals(DirectCopyTransform.class.getName())) { ArrayMemberValue arrayMember = (ArrayMemberValue) annotation.getMemberValue("value"); for (MemberValue arrayMemberValue : arrayMember.getValue()) { AnnotationMemberValue member = (AnnotationMemberValue) arrayMemberValue; Annotation memberAnnot = member.getValue(); ArrayMemberValue annot = (ArrayMemberValue) memberAnnot.getMemberValue("templateTokens"); for (MemberValue memberValue : annot.getValue()) { String val = ((StringMemberValue) memberValue).getValue(); if (val != null && templateTokens.containsKey(val)) { templateCheck: { for (DirectCopyIgnorePattern matchedPattern : matchedPatterns) { for (String ignoreToken : matchedPattern.getTemplateTokenPatterns()) { if (val.matches(ignoreToken)) { break templateCheck; } } } templates.add(templateTokens.get(val)); } } } BooleanMemberValue skipAnnot = (BooleanMemberValue) memberAnnot.getMemberValue("skipOverlaps"); if (skipAnnot != null) { skips.add(skipAnnot.getValue()); } else { skips.add(mySkipOverlaps); } BooleanMemberValue renameAnnot = (BooleanMemberValue) memberAnnot.getMemberValue("renameMethodOverlaps"); if (renameAnnot != null) { renames.add(renameAnnot.getValue()); } else { renames.add(myRenameMethodOverlaps); } } xformVals = templates.toArray(new String[templates.size()]); xformSkipOverlaps = skips.toArray(new Boolean[skips.size()]); xformRenameMethodOverlaps = renames.toArray(new Boolean[renames.size()]); break check; } } } } } } } if (xformVals != null && xformVals.length > 0) { logger.lifecycle( LifeCycleEvent.START, String.format( "Transform - Copying into [%s] from [%s]", xformKey, StringUtils.join(xformVals, ","))); // Load the destination class and defrost it so it is eligible for modifications clazz.defrost(); int index = 0; for (String xformVal : xformVals) { // Load the source class String trimmed = xformVal.trim(); classPool.appendClassPath(new LoaderClassPath(Class.forName(trimmed).getClassLoader())); CtClass template = classPool.get(trimmed); // Add in extra interfaces CtClass[] interfacesToCopy = template.getInterfaces(); for (CtClass i : interfacesToCopy) { checkInterfaces: { CtClass[] myInterfaces = clazz.getInterfaces(); for (CtClass myInterface : myInterfaces) { if (myInterface.getName().equals(i.getName())) { if (xformSkipOverlaps[index]) { break checkInterfaces; } else { throw new RuntimeException( "Duplicate interface detected " + myInterface.getName()); } } } logger.debug(String.format("Adding interface [%s]", i.getName())); clazz.addInterface(i); } } // copy over any EntityListeners ClassFile classFile = clazz.getClassFile(); ClassFile templateFile = template.getClassFile(); ConstPool constantPool = classFile.getConstPool(); buildClassLevelAnnotations(classFile, templateFile, constantPool); // Copy over all declared fields from the template class // Note that we do not copy over fields with the @NonCopiedField annotation CtField[] fieldsToCopy = template.getDeclaredFields(); for (CtField field : fieldsToCopy) { if (field.hasAnnotation(NonCopied.class)) { logger.debug(String.format("Not adding field [%s]", field.getName())); } else { try { CtField ctField = clazz.getDeclaredField(field.getName()); String originalSignature = ctField.getSignature(); String mySignature = field.getSignature(); if (!originalSignature.equals(mySignature)) { throw new IllegalArgumentException( "Field with name (" + field.getName() + ") and signature " + "(" + field.getSignature() + ") is targeted for weaving into (" + clazz.getName() + "). " + "An incompatible field of the same name and signature of (" + ctField.getSignature() + ") " + "already exists. The field in the target class should be updated to a different name, " + "or made to have a matching type."); } if (xformSkipOverlaps[index]) { logger.debug(String.format("Skipping overlapped field [%s]", field.getName())); continue; } } catch (NotFoundException e) { // do nothing -- field does not exist } logger.debug(String.format("Adding field [%s]", field.getName())); CtField copiedField = new CtField(field, clazz); boolean defaultConstructorFound = false; String implClass = getImplementationType(field.getType().getName()); // Look through all of the constructors in the implClass to see // if there is one that takes zero parameters try { CtConstructor[] implConstructors = classPool.get(implClass).getConstructors(); if (implConstructors != null) { for (CtConstructor cons : implConstructors) { if (cons.getParameterTypes().length == 0) { defaultConstructorFound = true; break; } } } } catch (NotFoundException e) { // Do nothing -- if we don't find this implementation, it's probably because it's // an array. In this case, we will not initialize the field. } if (defaultConstructorFound) { clazz.addField(copiedField, "new " + implClass + "()"); } else { clazz.addField(copiedField); } } } // Copy over all declared methods from the template class CtMethod[] methodsToCopy = template.getDeclaredMethods(); for (CtMethod method : methodsToCopy) { if (method.hasAnnotation(NonCopied.class)) { logger.debug(String.format("Not adding method [%s]", method.getName())); } else { try { CtClass[] paramTypes = method.getParameterTypes(); CtMethod originalMethod = clazz.getDeclaredMethod(method.getName(), paramTypes); if (xformSkipOverlaps[index]) { logger.debug( String.format( "Skipping overlapped method [%s]", methodDescription(originalMethod))); continue; } if (transformedMethods.contains(methodDescription(originalMethod))) { throw new RuntimeException( "Method already replaced " + methodDescription(originalMethod)); } else { logger.debug( String.format("Marking as replaced [%s]", methodDescription(originalMethod))); transformedMethods.add(methodDescription(originalMethod)); } logger.debug(String.format("Removing method [%s]", method.getName())); if (xformRenameMethodOverlaps[index]) { originalMethod.setName(renameMethodPrefix + method.getName()); } else { clazz.removeMethod(originalMethod); } } catch (NotFoundException e) { // Do nothing -- we don't need to remove a method because it doesn't exist } logger.debug(String.format("Adding method [%s]", method.getName())); CtMethod copiedMethod = new CtMethod(method, clazz, null); clazz.addMethod(copiedMethod); } } index++; } if (xformTemplates.isEmpty()) { annotationTransformedClasses.add(convertedClassName); } logger.lifecycle( LifeCycleEvent.END, String.format( "Transform - Copying into [%s] from [%s]", xformKey, StringUtils.join(xformVals, ","))); return clazz.toBytecode(); } } catch (ClassCircularityError error) { error.printStackTrace(); throw error; } catch (Exception e) { throw new RuntimeException("Unable to transform class", e); } finally { if (clazz != null) { clazz.detach(); } } return null; }
/** * Calls methodHandler.invoke with a null method parameter in order to get the underlying * instance. The invocation is then forwarded to this instance with generated bytecode. */ protected Bytecode createForwardingMethodBody(ClassFile file, MethodInformation methodInfo) throws NotFoundException { Method method = methodInfo.getMethod(); // we can only use bytecode based invocation for some methods // at the moment we restrict it solely to public methods with public // return and parameter types boolean bytecodeInvocationAllowed = Modifier.isPublic(method.getModifiers()) && Modifier.isPublic(method.getReturnType().getModifiers()); for (Class<?> paramType : method.getParameterTypes()) { if (!Modifier.isPublic(paramType.getModifiers())) { bytecodeInvocationAllowed = false; break; } } if (!bytecodeInvocationAllowed) { return createInterceptorBody(file, methodInfo); } Bytecode b = new Bytecode(file.getConstPool()); int localCount = MethodUtils.calculateMaxLocals(method) + 1; // create a new interceptor invocation context whenever we invoke a method on a client proxy // we use a try-catch block in order to make sure that endInterceptorContext() is invoked // regardless whether // the method has succeeded or not int start = b.currentPc(); b.addInvokestatic( "org.jboss.weld.bean.proxy.InterceptionDecorationContext", "startInterceptorContext", "()V"); b.add(Opcode.ALOAD_0); b.addGetfield( file.getName(), "methodHandler", DescriptorUtils.classToStringRepresentation(MethodHandler.class)); // pass null arguments to methodHandler.invoke b.add(Opcode.ALOAD_0); b.add(Opcode.ACONST_NULL); b.add(Opcode.ACONST_NULL); b.add(Opcode.ACONST_NULL); // now we have all our arguments on the stack // lets invoke the method b.addInvokeinterface( MethodHandler.class.getName(), "invoke", "(Ljava/lang/Object;Ljava/lang/reflect/Method;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;", 5); b.addCheckcast(methodInfo.getDeclaringClass()); // now we should have the target bean instance on top of the stack // we need to dup it so we still have it to compare to the return value b.add(Opcode.DUP); // lets create the method invocation String methodDescriptor = methodInfo.getDescriptor(); BytecodeUtils.loadParameters(b, methodDescriptor); if (method.getDeclaringClass().isInterface()) { b.addInvokeinterface( methodInfo.getDeclaringClass(), methodInfo.getName(), methodDescriptor, method.getParameterTypes().length + 1); } else { b.addInvokevirtual(methodInfo.getDeclaringClass(), methodInfo.getName(), methodDescriptor); } // end the interceptor context, everything was fine b.addInvokestatic( "org.jboss.weld.bean.proxy.InterceptionDecorationContext", "endInterceptorContext", "()V"); // jump over the catch block b.addOpcode(Opcode.GOTO); JumpMarker gotoEnd = JumpUtils.addJumpInstruction(b); // create catch block b.addExceptionHandler(start, b.currentPc(), b.currentPc(), 0); b.addInvokestatic( "org.jboss.weld.bean.proxy.InterceptionDecorationContext", "endInterceptorContext", "()V"); b.add(Opcode.ATHROW); // update the correct address to jump over the catch block gotoEnd.mark(); // if this method returns a primitive we just return if (method.getReturnType().isPrimitive()) { BytecodeUtils.addReturnInstruction(b, methodInfo.getReturnType()); } else { // otherwise we have to check that the proxy is not returning 'this; // now we need to check if the proxy has return 'this' and if so return // an // instance of the proxy. // currently we have result, beanInstance on the stack. b.add(Opcode.DUP_X1); // now we have result, beanInstance, result // we need to compare result and beanInstance // first we need to build up the inner conditional that just returns // the // result b.add(Opcode.IF_ACMPEQ); JumpMarker returnInstruction = JumpUtils.addJumpInstruction(b); BytecodeUtils.addReturnInstruction(b, methodInfo.getReturnType()); returnInstruction.mark(); // now add the case where the proxy returns 'this'; b.add(Opcode.ALOAD_0); b.addCheckcast(methodInfo.getMethod().getReturnType().getName()); BytecodeUtils.addReturnInstruction(b, methodInfo.getReturnType()); } if (b.getMaxLocals() < localCount) { b.setMaxLocals(localCount); } return b; }
public byte[] transform( ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { if (infos.isEmpty()) { return null; } String convertedClassName = className.replace('/', '.'); SingleTableInheritanceInfo key = new SingleTableInheritanceInfo(); key.setClassName(convertedClassName); int pos = infos.indexOf(key); if (pos >= 0) { try { if (LOG.isDebugEnabled()) { LOG.debug("Converting " + convertedClassName + " to a SingleTable inheritance strategy."); } SingleTableInheritanceInfo myInfo = infos.get(pos); ClassFile classFile = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer))); ConstPool constantPool = classFile.getConstPool(); AnnotationsAttribute annotationsAttribute = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag); List<?> attributes = classFile.getAttributes(); Iterator<?> itr = attributes.iterator(); while (itr.hasNext()) { Object object = itr.next(); if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) { AnnotationsAttribute attr = (AnnotationsAttribute) object; Annotation[] items = attr.getAnnotations(); for (Annotation annotation : items) { String typeName = annotation.getTypeName(); if (!typeName.equals(Inheritance.class.getName()) && ((myInfo.getDiscriminatorName() == null && !typeName.equals(Table.class.getName())) || myInfo.getDiscriminatorName() != null)) { annotationsAttribute.addAnnotation(annotation); } } itr.remove(); } } Annotation inheritance = new Annotation(Inheritance.class.getName(), constantPool); ClassPool pool = ClassPool.getDefault(); pool.importPackage("javax.persistence"); pool.importPackage("java.lang"); EnumMemberValue strategy = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("InheritanceType")); strategy.setType(InheritanceType.class.getName()); strategy.setValue(InheritanceType.SINGLE_TABLE.name()); inheritance.addMemberValue("strategy", strategy); annotationsAttribute.addAnnotation(inheritance); if (myInfo.getDiscriminatorName() != null) { Annotation discriminator = new Annotation(DiscriminatorColumn.class.getName(), constantPool); StringMemberValue name = new StringMemberValue(constantPool); name.setValue(myInfo.getDiscriminatorName()); discriminator.addMemberValue("name", name); EnumMemberValue discriminatorType = (EnumMemberValue) Annotation.createMemberValue(constantPool, pool.makeClass("DiscriminatorType")); discriminatorType.setType(DiscriminatorType.class.getName()); discriminatorType.setValue(myInfo.getDiscriminatorType().name()); discriminator.addMemberValue("discriminatorType", discriminatorType); IntegerMemberValue length = new IntegerMemberValue(constantPool); length.setValue(myInfo.getDiscriminatorLength()); discriminator.addMemberValue("length", length); annotationsAttribute.addAnnotation(discriminator); } classFile.addAttribute(annotationsAttribute); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream os = new DataOutputStream(bos); classFile.write(os); os.close(); return bos.toByteArray(); } catch (Exception ex) { ex.printStackTrace(); throw new IllegalClassFormatException( "Unable to convert " + convertedClassName + " to a SingleTable inheritance strategy: " + ex.getMessage()); } } else { return null; } }
public CtClassAnnotation(CtClass ctClass, Class<? extends java.lang.annotation.Annotation> type) { this.classFile = ctClass.getClassFile(); init(classFile.getConstPool(), type); }
public boolean transformClass(ClassFile file, ClassLoader loader, boolean modifiableClass) { Set<Integer> methodCallLocations = new HashSet<Integer>(); Integer newCallLocation = null; Integer methodReflectionLocation = null; Integer fakeCallRequiredLocation = null; // first we need to scan the constant pool looking for // CONSTANT_method_info_ref structures ConstPool pool = file.getConstPool(); for (int i = 1; i < pool.getSize(); ++i) { // we have a method call if (pool.getTag(i) == ConstPool.CONST_Methodref) { String className = pool.getMethodrefClassName(i); String methodName = pool.getMethodrefName(i); if (className.equals(Method.class.getName())) { if (methodName.equals("invoke")) { // store the location in the const pool of the method ref methodCallLocations.add(i); // we have found a method call // if we have not already stored a reference to our new // method in the const pool if (newCallLocation == null) { methodReflectionLocation = pool.addClassInfo("org.fakereplace.reflection.MethodReflection"); int nt = pool.addNameAndTypeInfo("fakeCallRequired", "(Ljava/lang/reflect/Method;)Z"); fakeCallRequiredLocation = pool.addMethodrefInfo(methodReflectionLocation, nt); newCallLocation = pool.addNameAndTypeInfo(METHOD_NAME, REPLACED_METHOD_DESCRIPTOR); } } } } } // this means we found an instance of the call, now we have to iterate // through the methods and replace instances of the call if (newCallLocation != null) { List<MethodInfo> methods = file.getMethods(); for (MethodInfo m : methods) { try { // ignore abstract methods if (m.getCodeAttribute() == null) { continue; } CodeIterator it = m.getCodeAttribute().iterator(); while (it.hasNext()) { // loop through the bytecode int index = it.next(); int op = it.byteAt(index); // if the bytecode is a method invocation if (op == CodeIterator.INVOKEVIRTUAL) { int val = it.s16bitAt(index + 1); // if the method call is one of the methods we are // replacing if (methodCallLocations.contains(val)) { Bytecode b = new Bytecode(file.getConstPool()); // our stack looks like Method, instance,params // we need Method, instance, params , Method b.add(Opcode.DUP_X2); b.add(Opcode.POP); b.add(Opcode.DUP_X2); b.add(Opcode.POP); b.add(Opcode.DUP_X2); b.addInvokestatic( methodReflectionLocation, "fakeCallRequired", "(Ljava/lang/reflect/Method;)Z"); b.add(Opcode.IFEQ); JumpMarker performRealCall = JumpUtils.addJumpInstruction(b); // now perform the fake call b.addInvokestatic(methodReflectionLocation, "invoke", REPLACED_METHOD_DESCRIPTOR); b.add(Opcode.GOTO); JumpMarker finish = JumpUtils.addJumpInstruction(b); performRealCall.mark(); b.addInvokevirtual(Method.class.getName(), METHOD_NAME, METHOD_DESCRIPTOR); finish.mark(); it.writeByte(CodeIterator.NOP, index); it.writeByte(CodeIterator.NOP, index + 1); it.writeByte(CodeIterator.NOP, index + 2); it.insert(b.get()); } } } m.getCodeAttribute().computeMaxStack(); } catch (Exception e) { log.error("Bad byte code transforming " + file.getName()); e.printStackTrace(); } } return true; } else { return false; } }
/** Add a method to a class that simply delegates to the parent implementation of the method */ public static void addDelegatingMethod(ClassFile file, MethodData mData) throws BadBytecode, DuplicateMemberException { MethodInfo m = new MethodInfo(file.getConstPool(), mData.getMethodName(), mData.getDescriptor()); m.setAccessFlags(mData.getAccessFlags()); Bytecode code = new Bytecode(file.getConstPool()); String[] params = DescriptorUtils.descriptorStringToParameterArray(mData.getDescriptor()); code.add(Opcode.ALOAD_0); // push this int count = 1; // zero is the this pointer int maxLocals = 1; for (String p : params) { // int char short boolean byte if (p.equals("I") || p.equals("C") || p.equals("S") || p.equals("Z") || p.equals("B")) { // push integer 0 code.addIload(count); maxLocals++; } // long else if (p.equals("J")) { code.addLload(count); maxLocals += 2; count++; } // double else if (p.equals("D")) { code.addDload(count); maxLocals += 2; count++; } // float else if (p.equals("F")) { code.addFload(count); maxLocals++; } // arrays and reference types else { code.addAload(count); maxLocals++; } count++; } code.addInvokespecial(file.getSuperclass(), mData.getMethodName(), mData.getDescriptor()); String p = DescriptorUtils.getReturnTypeInJvmFormat(mData.getDescriptor()); // int char short boolean byte if (p.equals("I") || p.equals("C") || p.equals("S") || p.equals("Z") || p.equals("B")) { code.add(Opcode.IRETURN); } // long else if (p.equals("J")) { code.add(Opcode.LRETURN); } // double else if (p.equals("D")) { code.add(Opcode.DRETURN); } // float else if (p.equals("F")) { code.add(Opcode.FRETURN); } // void else if (p.equals("V")) { code.add(Opcode.RETURN); } // arrays and reference types else { code.add(Opcode.ARETURN); } CodeAttribute ca = code.toCodeAttribute(); ca.computeMaxStack(); ca.setMaxLocals(maxLocals); m.setCodeAttribute(ca); file.addMethod(m); }
@Override public boolean transform( ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, ClassFile file) throws IllegalClassFormatException, BadBytecode { /** * Hack up the proxy factory so it stores the proxy ClassFile. We need this to regenerate * proxies. */ if (file.getName().equals("org.jboss.weld.bean.proxy.ProxyFactory")) { for (final MethodInfo method : (List<MethodInfo>) file.getMethods()) { if (method.getName().equals("createProxyClass")) { final MethodInvokationManipulator methodInvokationManipulator = new MethodInvokationManipulator(); methodInvokationManipulator.replaceVirtualMethodInvokationWithStatic( ClassLoader.class.getName(), WeldProxyClassLoadingDelegate.class.getName(), "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;", "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;", loader); methodInvokationManipulator.replaceVirtualMethodInvokationWithStatic( "org.jboss.weld.util.bytecode.ClassFileUtils", WeldProxyClassLoadingDelegate.class.getName(), "toClass", "(Ljavassist/bytecode/ClassFile;Ljava/lang/ClassLoader;Ljava/security/ProtectionDomain;)Ljava/lang/Class;", "(Ljavassist/bytecode/ClassFile;Ljava/lang/ClassLoader;Ljava/security/ProtectionDomain;)Ljava/lang/Class;", loader); HashSet<MethodInfo> modifiedMethods = new HashSet<MethodInfo>(); methodInvokationManipulator.transformClass(file, loader, true, modifiedMethods); for (MethodInfo m : modifiedMethods) { m.rebuildStackMap(ClassPool.getDefault()); } return true; } else if (method.getName().equals("<init>")) { Integer beanArgument = null; int count = 0; for (final String paramType : DescriptorUtils.descriptorStringToParameterArray(method.getDescriptor())) { if (paramType.equals("javax/enterprise/inject/spi/Bean")) { beanArgument = count; break; } else if (paramType.equals("D") || paramType.equals("J")) { count += 2; } else { count++; } } if (beanArgument == null) { log.error( "Constructor org.jboss.weld.bean.proxy.ProxyFactory.<init>" + method.getDescriptor() + " does not have a bean parameter, proxies produced by this factory will not be reloadable"); continue; } // similar to other tracked instances // but we need a strong ref Bytecode code = new Bytecode(file.getConstPool()); code.addAload(0); code.addAload(beanArgument); code.addInvokestatic( WeldClassChangeAware.class.getName(), "addProxyFactory", "(Lorg/jboss/weld/bean/proxy/ProxyFactory;)V"); CodeIterator it = method.getCodeAttribute().iterator(); it.skipConstructor(); it.insert(code.get()); } } } return false; }
protected void checkClassFile(ClassFile file) throws Exception { Map<Integer, Triple> calls = new HashMap<>(); ConstPool pool = file.getConstPool(); for (int i = 1; i < pool.getSize(); ++i) { // we have a method call BytecodeUtils.Ref ref = BytecodeUtils.getRef(pool, i); String className = ref.getClassName(pool, i); if (className != null) { String methodName = ref.getName(pool, i); String methodDesc = ref.getDesc(pool, i); fillCalls(i, className, methodName, methodDesc, calls); } } if (calls.isEmpty() && annotations.isEmpty()) { return; } String className = file.getName(); AnnotationsAttribute faa = (AnnotationsAttribute) file.getAttribute(AnnotationsAttribute.visibleTag); checkAnnotations(className, TYPE_USAGE.getMethodName(), faa, -1); List<MethodInfo> methods = file.getMethods(); for (MethodInfo m : methods) { try { // ignore abstract methods if (m.getCodeAttribute() == null) { continue; } AnnotationsAttribute maa = (AnnotationsAttribute) m.getAttribute(AnnotationsAttribute.visibleTag); boolean annotationsChecked = false; int firstLine = -1; CodeIterator it = m.getCodeAttribute().iterator(); while (it.hasNext()) { // loop through the bytecode final int index = it.next(); final int line = m.getLineNumber(index); if (annotationsChecked == false) { annotationsChecked = true; firstLine = line; checkAnnotations( className, m.getName(), maa, line - 2); // -2 to get the line above the method } int op = it.byteAt(index); // if the bytecode is a method invocation if (op == CodeIterator.INVOKEVIRTUAL || op == CodeIterator.INVOKESTATIC || op == CodeIterator.INVOKEINTERFACE || op == CodeIterator.INVOKESPECIAL) { int val = it.s16bitAt(index + 1); Triple triple = calls.get(val); if (triple != null) { Map<Tuple, Set<CodeLine>> map = report.get(triple.className); Set<CodeLine> set = map.get(triple.tuple); CodeLine cl = new CodeLine(className, m.getName(), line); set.add(cl.modify()); // check for .jsp, etc } } } if (BaseMethodExclusion.isBridge(m) == false) { SignatureAttribute.MethodSignature signature = SignatureAttribute.toMethodSignature(m.getDescriptor()); handleMethodSignature(className, m.getName(), firstLine - 1, signature.getReturnType()); handleMethodSignature( className, m.getName(), firstLine - 1, signature.getParameterTypes()); handleMethodSignature( className, m.getName(), firstLine - 1, signature.getExceptionTypes()); } ParameterAnnotationsAttribute paa = (ParameterAnnotationsAttribute) m.getAttribute(ParameterAnnotationsAttribute.visibleTag); if (paa != null) { Annotation[][] paas = paa.getAnnotations(); if (paas != null) { for (Annotation[] params : paas) { for (Annotation a : params) { for (Map.Entry<String, Boolean> entry : annotations.entrySet()) { if (entry.getKey().equals(a.getTypeName())) { checkAnnotation( className, m.getName(), firstLine - 1, entry.getValue(), entry.getKey(), a); } } } } } } m.getCodeAttribute().computeMaxStack(); } catch (Exception e) { e.printStackTrace(); } } }