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 createStateField(final State state, final ClassFile file, final CodeAttribute sc) { if (state.fieldName != null) { file.addField(AccessFlag.STATIC | AccessFlag.FINAL | AccessFlag.PRIVATE, state.fieldName, "[B"); sc.ldc(state.terminalState); sc.ldc("ISO-8859-1"); sc.invokevirtual(String.class.getName(), "getBytes", "(Ljava/lang/String;)[B"); sc.putstatic(file.getName(), state.fieldName, "[B"); file.addField(AccessFlag.STATIC | AccessFlag.FINAL | AccessFlag.PRIVATE, state.httpStringFieldName, HTTP_STRING_DESCRIPTOR); sc.aload(CONSTRUCTOR_HTTP_STRING_MAP_VAR); if (state.terminalState != null) { sc.ldc(state.terminalState); } else { sc.ldc(state.soFar); sc.invokeinterface(Map.class.getName(), "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); sc.dup(); BranchEnd end = sc.ifnull(); sc.checkcast(HTTP_STRING_CLASS); sc.putstatic(file.getName(), state.httpStringFieldName, HTTP_STRING_DESCRIPTOR); BranchEnd done = sc.gotoInstruction(); sc.branchEnd(end); sc.pop(); sc.newInstruction(HTTP_STRING_CLASS); sc.dup(); if (state.terminalState != null) { sc.ldc(state.terminalState); } else { sc.ldc(state.soFar);
} catch (ClassNotFoundException e) { try { final ClassFile file = new ClassFile(NAME, SerializationHackProxy.class.getName()); final ClassMethod method = file.addMethod(Modifier.PUBLIC, "read", "Ljava/lang/Object;", "Ljava/io/ObjectInputStream;"); final CodeAttribute codeAttribute = method.getCodeAttribute(); codeAttribute.aload(1); codeAttribute.invokevirtual("java/io/ObjectInputStream", "readObject", "()Ljava/lang/Object;"); codeAttribute.returnInstruction(); ClassMethod ctor = file.addMethod(Modifier.PUBLIC, "<init>", "V"); ctor.getCodeAttribute().aload(0); ctor.getCodeAttribute().invokespecial(SerializationHackProxy.class.getName(), "<init>", "()V"); ctor.getCodeAttribute().returnInstruction(); clazz = file.define(loader); } catch (RuntimeException ex) { try {
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); ca.iconst(0); ca.anewarray(Object.class.getName()); ca.iconst(paramTypes.length); ca.anewarray(Object.class.getName()); int index = 1; for (int j = 0; j < paramTypes.length; j++) { ca.dup(); ca.iconst(j); if (!type.isPrimitive()) { ca.aload(index); } else if (type.equals(double.class)) { ca.dload(index); Boxing.boxDouble(ca); index++; } else if (type.equals(long.class)) { ca.lload(index); Boxing.boxLong(ca); index++;
c.aload(PARSE_STATE_VAR); c.getfield(parseStateClass, "leftOver", "B"); c.dup(); final BranchEnd end = c.ifne(); c.pop(); c.aload(BYTE_BUFFER_VAR); c.invokevirtual(ByteBuffer.class.getName(), "get", "()B"); BranchEnd cont = c.gotoInstruction(); c.branchEnd(end); c.aload(PARSE_STATE_VAR); c.iconst(0); c.putfield(parseStateClass, "leftOver", "B"); c.branchEnd(cont); c.aload(BYTE_BUFFER_VAR); c.invokevirtual(ByteBuffer.class.getName(), "get", "()B"); c.dup(); final Set<BranchEnd> tokenEnds = new LinkedHashSet<>(); final Set<BranchEnd> badTokenEnds = new LinkedHashSet<>(); final Map<State, BranchEnd> ends = new IdentityHashMap<State, BranchEnd>(); for (State state : currentState.next.values()) { c.iconst(state.value); ends.put(state, c.ifIcmpeq()); c.dup(); c.iconst(':');
Class<?> superclass, String stubClassName) { final ClassFile asm = new ClassFile(stubClassName, superclass.getName(), interfaceAnalysis.getCls().getName()); final ClassMethod ctor = asm.addMethod(Modifier.PUBLIC, "<init>", "V"); ctor.getCodeAttribute().aload(0); ctor.getCodeAttribute().invokespecial(superclass.getName(), "<init>", "()V"); ctor.getCodeAttribute().returnInstruction(); asm.addField(Modifier.PRIVATE + Modifier.STATIC, ID_FIELD_NAME, String[].class); final CodeAttribute idMethod = asm.addMethod(Modifier.PUBLIC + Modifier.FINAL, "_ids", "[Ljava/lang/String;").getCodeAttribute(); idMethod.getstatic(stubClassName, ID_FIELD_NAME, "[Ljava/lang/String;"); idMethod.returnInstruction(); final CodeAttribute clinit = asm.addMethod(Modifier.STATIC, "<clinit>", "V").getCodeAttribute(); clinit.iconst(ids.length); clinit.anewarray(String.class.getName()); for (int i = 0; i < ids.length; i++) { clinit.dup(); clinit.iconst(i); clinit.ldc(ids[i]); clinit.aastore(); clinit.putstatic(stubClassName, ID_FIELD_NAME, "[Ljava/lang/String;"); clinit.invokestatic(stubClassName, init(methodIndex), "()V");
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 static Class<?> toClass(ClassFile ct, ClassLoader loader, ProtectionDomain domain) { if (MAGIC_IN_PROGRESS.get()) { try { final Class<?> originalProxyClass = loader.loadClass(ct.getName()); try { ByteArrayDataOutputStream out = new ByteArrayDataOutputStream(); ct.write(out); Map<Class<?>, byte[]> reloadMap = new HashMap<Class<?>, byte[]>(); reloadMap.put(originalProxyClass, out.getBytes()); // TODO : is this standard way how to reload class? PluginManager.getInstance().hotswap(reloadMap); return originalProxyClass; } catch (Exception e) { throw new RuntimeException(e); } } catch (ClassNotFoundException e) { //it has not actually been loaded yet return ClassFileUtils.toClass(ct, loader, domain); } } return ClassFileUtils.toClass(ct, loader, domain); }
/** * Adds a field with the given name and descriptor. * */ public ClassField addField(int accessFlags, String name, String descriptor) { return addField(accessFlags, name, descriptor, null); }
RunWithinInterceptionDecorationContextGenerator(ClassMethod classMethod, ProxyFactory<?> factory) { this.classMethod = classMethod; this.b = classMethod.getCodeAttribute(); this.factory = factory; }
/** * Construct a new instance. * * @param className the class name * @param superClass the superclass * @param classLoader the defining class loader * @param protectionDomain the protection domain */ protected AbstractProxyFactory(String className, Class<T> superClass, ClassLoader classLoader, ProtectionDomain protectionDomain, final ReflectionMetadataSource reflectionMetadataSource) { super(className, superClass, classLoader, protectionDomain, reflectionMetadataSource); staticConstructor = classFile.addMethod(AccessFlag.of(AccessFlag.PUBLIC, AccessFlag.STATIC), "<clinit>", "V"); }
public Class<?> compileToClass(Class<?> intf, String stubClassName) { return compile(intf, stubClassName).define(intf.getClassLoader(), intf.getProtectionDomain()); }
/** * Adds a field with the given name and descriptor. * */ public ClassField addField(int accessFlags, String name, String descriptor) { return addField(accessFlags, name, descriptor, null); }
RunWithinInterceptionDecorationContextGenerator(ClassMethod classMethod, ProxyFactory<?> factory) { this.classMethod = classMethod; this.b = classMethod.getCodeAttribute(); this.factory = factory; }
try { final ClassFile clazz = IIOPStubCompiler.compile(myClass, stubClassName); theClass = clazz.define(cl, myClass.getProtectionDomain()); } catch (Throwable ex) {
/** * Adds a field with the given name and descriptor. * */ public ClassField addField(int accessFlags, String name, String descriptor) { return addField(accessFlags, name, descriptor, null); }
RunWithinInterceptionDecorationContextGenerator(ClassMethod classMethod, ProxyFactory<?> factory) { this.classMethod = classMethod; this.b = classMethod.getCodeAttribute(); this.factory = factory; }
/** * Adds a field with the given name and descriptor. * */ public ClassField addField(int accessFlags, String name, String descriptor) { return addField(accessFlags, name, descriptor, null); }
RunWithinInterceptionDecorationContextGenerator(ClassMethod classMethod, ProxyFactory<?> factory) { this.classMethod = classMethod; this.b = classMethod.getCodeAttribute(); this.factory = factory; }
/** * Adds a field with the given name and descriptor. * */ public ClassField addField(int accessFlags, String name, String descriptor) { return addField(accessFlags, name, descriptor, null); }