asm.addField(Modifier.PRIVATE + Modifier.STATIC, strategyField, StubStrategy.class); final CodeAttribute ca = asm.addMethod(m).getCodeAttribute(); ca.aload(0); ca.ldc(idlName); ca.getstatic(asm.getName(), strategyField, StubStrategy.class); } else { ca.iload(index); Boxing.boxIfNessesary(ca, DescriptorUtils.makeDescriptor(type)); invoke += (Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1)); ret = DescriptorUtils.makeDescriptor(returnType); ca.invokevirtual(superclass.getName(), invoke, "(Ljava/lang/String;Lorg/wildfly/iiop/openjdk/rmi/marshal/strategy/StubStrategy;[Ljava/lang/Object;)" + ret); final CodeAttribute init = asm.addMethod(Modifier.PRIVATE + Modifier.STATIC,initMethod, "V").getCodeAttribute(); int i; int len;
public byte[] createTokenizer(final String[] httpVerbs, String[] httpVersions, String[] standardHeaders) { final String className = existingClassName + CLASS_NAME_SUFFIX; final ClassFile file = new ClassFile(className, existingClassName); final ClassMethod ctor = file.addMethod(AccessFlag.PUBLIC, "<init>", "V", DescriptorUtils.parameterDescriptors(constructorDescriptor)); ctor.getCodeAttribute().aload(0); ctor.getCodeAttribute().loadMethodParameters(); ctor.getCodeAttribute().invokespecial(existingClassName, "<init>", constructorDescriptor); ctor.getCodeAttribute().returnInstruction(); final ClassMethod sctor = file.addMethod(AccessFlag.PUBLIC | AccessFlag.STATIC, "<clinit>", "V"); final AtomicInteger fieldCounter = new AtomicInteger(1); sctor.getCodeAttribute().invokestatic(existingClassName, "httpStrings", "()" + DescriptorUtils.makeDescriptor(Map.class)); sctor.getCodeAttribute().astore(CONSTRUCTOR_HTTP_STRING_MAP_VAR); createStateMachines(httpVerbs, httpVersions, standardHeaders, className, file, sctor, fieldCounter); sctor.getCodeAttribute().returnInstruction(); return file.toBytecode(); }
private void invokePrivateMethodHandler(CodeAttribute b, ClassMethod classMethod, MethodInformation methodInfo, ClassMethod staticConstructor) { try { classMethod.getClassFile().addField(AccessFlag.PRIVATE, PRIVATE_METHOD_HANDLER_FIELD_NAME, MethodHandler.class); } catch (DuplicateMemberException ignored) { b.aload(0); b.getfield(classMethod.getClassFile().getName(), PRIVATE_METHOD_HANDLER_FIELD_NAME, DescriptorUtils.makeDescriptor(MethodHandler.class)); b.aload(0); DEFAULT_METHOD_RESOLVER.getDeclaredMethod(classMethod, methodInfo.getDeclaringClass(), methodInfo.getName(), methodInfo.getParameterTypes(), staticConstructor); b.iconst(i); BytecodeUtils.addLoadInstruction(b, typeString, localVariableCount); b.aastore(); if (isWide(typeString)) { localVariableCount = localVariableCount + 2; } else { if (methodInfo.getReturnType().equals(BytecodeUtils.VOID_CLASS_DESCRIPTOR)) { } else if (isPrimitive(methodInfo.getReturnType())) { Boxing.unbox(b, methodInfo.getReturnType()); } else { b.checkcast(BytecodeUtils.getName(methodInfo.getReturnType()));
private void createInterceptedMethod(ClassMethod classMethod, final MethodInformation methodInfo, Method method, ClassMethod staticConstructor) { final CodeAttribute b = classMethod.getCodeAttribute(); b.aload(0); getMethodHandlerField(classMethod.getClassFile(), b); b.invokestatic(InterceptionDecorationContext.class.getName(), "getStack", "()" + DescriptorUtils.makeDescriptor(Stack.class)); b.aload(0); DEFAULT_METHOD_RESOLVER.getDeclaredMethod(classMethod, methodInfo.getDeclaringClass(), method.getName(), methodInfo.getParameterTypes(), staticConstructor); b.dup(); BytecodeUtils.addLoadInstruction(b, typeString, localVariableCount); b.aastore(); if (isWide(typeString)) { localVariableCount = localVariableCount + 2; } else { if (methodInfo.getReturnType().equals(BytecodeUtils.VOID_CLASS_DESCRIPTOR)) { b.returnInstruction(); } else if (isPrimitive(methodInfo.getReturnType())) { Boxing.unbox(b, classMethod.getReturnType()); b.returnInstruction(); } else { b.checkcast(BytecodeUtils.getName(methodInfo.getReturnType())); b.returnInstruction();
protected void generateSetMethodHandlerBody(ClassMethod method) { final CodeAttribute b = method.getCodeAttribute(); b.aload(0); b.aload(1); b.checkcast(getMethodHandlerType()); b.putfield(method.getClassFile().getName(), METHOD_HANDLER_FIELD_NAME, DescriptorUtils.makeDescriptor(getMethodHandlerType())); b.returnInstruction(); }
final CodeAttribute b = classMethod.getCodeAttribute(); b.aload(0); // load this int localVariables = 1; int actualDelegateParameterPosition = 0; for (int i = 0; i < initializerMethodInfo.getMethod().getParameterTypes().length; ++i) { if (i == delegateParameterPosition) { Class<?> type = initializerMethodInfo.getMethod().getParameterTypes()[i]; BytecodeUtils.addLoadInstruction(b, DescriptorUtils.makeDescriptor(type), localVariables); if (type == long.class || type == double.class) { localVariables = localVariables + 2; b.invokespecial(classMethod.getClassFile().getSuperclass(), initializerMethodInfo.getName(), initializerMethodInfo.getDescriptor()); b.aload(0); // load this b.aload(actualDelegateParameterPosition); // load the delegate b.invokevirtual(classMethod.getClassFile().getName(), INIT_MH_METHOD_NAME, "(" + LJAVA_LANG_OBJECT + ")" + BytecodeUtils.VOID_CLASS_DESCRIPTOR);
private void createAbstractMethodCode(ClassMethod classMethod, MethodInformation method, ClassMethod staticConstructor) { if ((delegateField != null) && (!Modifier.isPrivate(delegateField.getModifiers()))) { // Call the corresponding method directly on the delegate final CodeAttribute b = classMethod.getCodeAttribute(); // load the delegate field b.aload(0); b.getfield(classMethod.getClassFile().getName(), delegateField.getName(), DescriptorUtils.makeDescriptor(delegateField.getType())); // load the parameters b.loadMethodParameters(); // invoke the delegate method b.invokeinterface(delegateField.getType().getName(), method.getName(), method.getDescriptor()); // return the value if applicable b.returnInstruction(); } else { if (!Modifier.isPrivate(method.getMethod().getModifiers())) { // if it is a parameter injection point we need to initialize the // injection point then handle the method with the method handler // 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 invokeMethodHandler(classMethod, method, true, targetInstanceBytecodeMethodResolver, staticConstructor); } else { // if the delegate is private we need to use the method handler createInterceptorBody(classMethod, method, staticConstructor); } } }
private void writeStateMachine(final String className, final ClassFile file, final CodeAttribute c, final State initial, final List<State> allStates, int noStates, final CustomStateMachine stateMachine, boolean expectNewline) { c.aload(BYTE_BUFFER_VAR); c.invokevirtual(ByteBuffer.class.getName(), "hasRemaining", "()Z"); final BranchEnd nonZero = c.ifne(); c.iconst(0); c.aload(PARSE_STATE_VAR); c.dup(); c.getfield(parseStateClass, "stringBuilder", DescriptorUtils.makeDescriptor(StringBuilder.class)); c.astore(STATE_STRING_BUILDER_VAR); c.dup();
@SuppressWarnings("unchecked") private void createDelegateMethod(ClassFile proxyClassType, Method method, MethodInformation methodInformation) { int modifiers = (method.getModifiers() | AccessFlag.SYNTHETIC | AccessFlag.PRIVATE) & ~AccessFlag.PUBLIC & ~AccessFlag.PROTECTED; ClassMethod delegatingMethod = proxyClassType.addMethod(modifiers, method.getName() + SUPER_DELEGATE_SUFFIX, DescriptorUtils.makeDescriptor(method.getReturnType()), DescriptorUtils.parameterDescriptors(method.getParameterTypes())); delegatingMethod.addCheckedExceptions((Class<? extends Exception>[]) method.getExceptionTypes()); createDelegateToSuper(delegatingMethod, methodInformation); }
public void invokevirtual(Method method) { if (Modifier.isStatic(method.getModifiers())) { throw new InvalidBytecodeException("Cannot use invokevirtual to invoke a static method"); } else if (Modifier.isPrivate(method.getModifiers())) { throw new InvalidBytecodeException("Cannot use invokevirtual to invoke a private method"); } else if (method.getDeclaringClass().isInterface()) { throw new InvalidBytecodeException("Cannot use invokevirtual to invoke an interface method"); } invokevirtual(method.getDeclaringClass().getName(), method.getName(), DescriptorUtils.methodDescriptor(method), DescriptorUtils.makeDescriptor(method.getReturnType()), DescriptorUtils.parameterDescriptors(method .getParameterTypes())); }
public void invokespecial(Method method) { if (Modifier.isStatic(method.getModifiers())) { throw new InvalidBytecodeException("Cannot use invokespacial to invoke a static method"); } invokespecial(method.getDeclaringClass().getName(), method.getName(), DescriptorUtils.methodDescriptor(method), DescriptorUtils.makeDescriptor(method.getReturnType()), DescriptorUtils.parameterDescriptors(method .getParameterTypes())); }
protected void getMethodHandlerField(ClassFile file, CodeAttribute b) { b.getfield(file.getName(), METHOD_HANDLER_FIELD_NAME, DescriptorUtils.makeDescriptor(getMethodHandlerType())); }
public void invokestatic(Method method) { if (!Modifier.isStatic(method.getModifiers())) { throw new InvalidBytecodeException("Cannot use invokestatic to invoke a non static method"); } invokestatic(method.getDeclaringClass().getName(), method.getName(), DescriptorUtils.methodDescriptor(method), DescriptorUtils.makeDescriptor(method.getReturnType()), DescriptorUtils.parameterDescriptors(method .getParameterTypes())); }
public void invokeinterface(Method method) { if (Modifier.isStatic(method.getModifiers())) { throw new InvalidBytecodeException("Cannot use invokeinterface to invoke a static method"); } else if (Modifier.isPrivate(method.getModifiers())) { throw new InvalidBytecodeException("Cannot use invokeinterface to invoke a private method"); } else if (!method.getDeclaringClass().isInterface()) { throw new InvalidBytecodeException("Cannot use invokeinterface to invoke a non interface method"); } invokeinterface(method.getDeclaringClass().getName(), method.getName(), DescriptorUtils.methodDescriptor(method), DescriptorUtils.makeDescriptor(method.getReturnType()), DescriptorUtils.parameterDescriptors(method .getParameterTypes())); }
public void invokevirtual(String className, String methodName, String descriptor) { String[] params = DescriptorUtils.parameterDescriptors(descriptor); String returnType = DescriptorUtils.returnType(descriptor); invokevirtual(className, methodName, descriptor, returnType, params); }
public void invokespecial(String className, String methodName, String descriptor) { String[] params = DescriptorUtils.parameterDescriptors(descriptor); String returnType = DescriptorUtils.returnType(descriptor); invokespecial(className, methodName, descriptor, returnType, params); }
protected void createStateMachine(final String[] originalItems, final String className, final ClassFile file, final ClassMethod sctor, final AtomicInteger fieldCounter, final String methodName, final CustomStateMachine stateMachine, boolean expectNewline) { //list of all states except the initial final List<State> allStates = new ArrayList<State>(); final State initial = new State((byte) 0, ""); for (String value : originalItems) { addStates(initial, value, allStates); } //we want initial to be number 0 final AtomicInteger stateCounter = new AtomicInteger(-1); setupStateNo(initial, stateCounter, fieldCounter); for (State state : allStates) { setupStateNo(state, stateCounter, fieldCounter); createStateField(state, file, sctor.getCodeAttribute()); } final int noStates = stateCounter.get(); final ClassMethod handle = file.addMethod(Modifier.PROTECTED | Modifier.FINAL, methodName, "V", DescriptorUtils.makeDescriptor(ByteBuffer.class), parseStateDescriptor, httpExchangeDescriptor); handle.addCheckedExceptions(BAD_REQUEST_EXCEPTION); writeStateMachine(className, file, handle.getCodeAttribute(), initial, allStates, noStates, stateMachine, expectNewline); }
public void invokeinterface(String className, String methodName, String descriptor) { String[] params = DescriptorUtils.parameterDescriptors(descriptor); String returnType = DescriptorUtils.returnType(descriptor); invokeinterface(className, methodName, descriptor, returnType, params); }
ClassMethod(String name, String returnType, String[] parameters, int accessFlags, ClassFile classFile) { ConstPool constPool = classFile.getConstPool(); this.classFile = classFile; this.returnType = DescriptorUtils.validateDescriptor(returnType); this.parameters = parameters; this.name = name; this.descriptor = DescriptorUtils.methodDescriptor(parameters, returnType); this.accessFlags = accessFlags; this.nameIndex = constPool.addUtf8Entry(name); this.descriptorIndex = constPool.addUtf8Entry(descriptor); this.constructor = name.equals("<init>"); this.exceptionsAttribute = new ExceptionsAttribute(constPool); this.attributes.add(exceptionsAttribute); if (Modifier.isAbstract(accessFlags)) { codeAttribute = null; } else { codeAttribute = new CodeAttribute(this, constPool); attributes.add(codeAttribute); } for (String param : this.parameters) { DescriptorUtils.validateDescriptor(param); } this.runtimeVisibleAnnotationsAttribute = new AnnotationsAttribute(AnnotationsAttribute.Type.RUNTIME_VISIBLE, constPool); this.attributes.add(runtimeVisibleAnnotationsAttribute); this.runtimeVisibleParameterAnnotationsAttribute = new ParameterAnnotationsAttribute( ParameterAnnotationsAttribute.Type.RUNTIME_VISIBLE, constPool, parameters.length); this.attributes.add(runtimeVisibleParameterAnnotationsAttribute); }
public void invokestatic(String className, String methodName, String descriptor) { String[] params = DescriptorUtils.parameterDescriptors(descriptor); String returnType = DescriptorUtils.returnType(descriptor); invokestatic(className, methodName, descriptor, returnType, params); }