if (isNeedInsertClass(ctClass.getName())) { ctClass.setModifiers(AccessFlag.setPublic(ctClass.getModifiers())); if (ctClass.isInterface() || ctClass.getDeclaredMethods().length < 1) { ClassPool classPool = ctBehavior.getDeclaringClass().getClassPool(); CtClass type = classPool.getOrNull(Constants.INTERFACE_NAME); CtField ctField = new CtField(type, Constants.INSERT_FIELD_NAME, ctClass); methodMap.put(ctBehavior.getLongName(), insertMethodCount.incrementAndGet()); try { if (ctBehavior.getMethodInfo().isMethod()) { CtMethod ctMethod = (CtMethod) ctBehavior; boolean isStatic = (ctMethod.getModifiers() & AccessFlag.STATIC) != 0; ", " + methodMap.get(ctBehavior.getLongName()) + "," + parametersClassType + "," + returnTypeString + ".class)) {"; body += getReturnStatement(returnTypeString, isStatic, methodMap.get(ctBehavior.getLongName()), parametersClassType, returnTypeString + ".class"); body += " }"; ctBehavior.insertBefore(body);
CodeIterator iterator = ca.iterator(); int retAddr = ca.getMaxLocals(); Bytecode b = new Bytecode(pool, 0, retAddr + 1); b.setStackDepth(ca.getMaxStack() + 1); Javac jv = new Javac(b, cc); try { int nvars = jv.recordParams(getParameterTypes(), Modifier.isStatic(getModifiers())); jv.recordParamNames(ca, nvars); CtClass rtype = getReturnType0(); int varNo = jv.recordReturnType(rtype, true); jv.recordLocalVariables(ca, 0); int handlerLen = insertAfterHandler(asFinally, b, rtype, varNo, jv, src); int handlerPos = iterator.getCodeLength(); if (asFinally) ca.getExceptionTable().add(getStartPosOfBody(ca), handlerPos, handlerPos, 0); if (noReturn) { adviceLen = insertAfterAdvice(b, jv, src, pool, rtype, varNo); noReturn = false; insertGoto(iterator, advicePos, pos); methodInfo.rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
protected void extendToString(StringBuffer buffer) { buffer.append(' '); buffer.append(getName()); buffer.append(' '); buffer.append(methodInfo.getDescriptor()); }
private Object[] getAnnotations(boolean ignoreNotFound) throws ClassNotFoundException { MethodInfo mi = getMethodInfo2(); AnnotationsAttribute ainfo = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag); AnnotationsAttribute ainfo2 = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag); return CtClassType.toAnnotationType(ignoreNotFound, getDeclaringClass().getClassPool(), ainfo, ainfo2); }
private static boolean hasExisting( String methname, String methsig, CtBehavior ccms[] ) throws NotFoundException { for( CtBehavior cm : ccms ) if( cm.getName ().equals(methname) && cm.getSignature().equals(methsig ) ) return true; return false; }
/** * Inserts a new parameter, which becomes the first parameter. */ public void insertParameter(CtClass type) throws CannotCompileException { declaringClass.checkModify(); String desc = methodInfo.getDescriptor(); String desc2 = Descriptor.insertParameter(type, desc); try { addParameter2(Modifier.isStatic(getModifiers()) ? 0 : 1, type, desc); } catch (BadBytecode e) { throw new CannotCompileException(e); } methodInfo.setDescriptor(desc2); }
void scanMember(CtBehavior member) throws CannotCompileException { final String key = member.getDeclaringClass().getName() + "." + member.getMethodInfo().getName() + "(" + parameterNames(member.getMethodInfo()) + ")"; //+ " #" + member.getMethodInfo().getLineNumber(0) member.instrument(new ExprEditor() { @Override public void edit(NewExpr e) throws CannotCompileException {
cc.checkModify(); ClassPool pool = cc.getClassPool(); String fname; int i = 0; fname = "_cflow$" + i++; try { cc.getDeclaredField(fname); field.setModifiers(Modifier.PUBLIC | Modifier.STATIC); cc.addField(field, CtField.Initializer.byNew(type)); insertBefore(fname + ".enter();", false); String src = fname + ".exit();"; insertAfter(src, true); throw new CannotCompileException(e);
cc.checkModify(); ConstPool cp = methodInfo.getConstPool(); CodeAttribute ca = methodInfo.getCodeAttribute(); CodeIterator iterator = ca.iterator(); Bytecode b = new Bytecode(cp, ca.getMaxStack(), ca.getMaxLocals()); b.setStackDepth(1); Javac jv = new Javac(b, cc); try { jv.recordParams(getParameterTypes(), Modifier.isStatic(getModifiers())); int var = jv.recordVariable(exceptionType, exceptionName); b.addAstore(var); ca.setMaxLocals(locals); int len = iterator.getCodeLength(); int pos = iterator.append(b.get()); ca.getExceptionTable().add(getStartPosOfBody(ca), len, len, cp.addClassInfo(exceptionType)); iterator.append(b.getExceptionTable(), pos); methodInfo.rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2()); throw new CannotCompileException(e); throw new CannotCompileException(e); } catch (BadBytecode e) { throw new CannotCompileException(e);
throws CannotCompileException CodeAttribute ca = methodInfo.getCodeAttribute(); if (ca == null) throw new CannotCompileException("no method body"); = (LineNumberAttribute)ca.getAttribute(LineNumberAttribute.tag); if (ainfo == null) throw new CannotCompileException("no line number info"); cc.checkModify(); CodeIterator iterator = ca.iterator(); Javac jv = new Javac(cc); try { jv.recordLocalVariables(ca, index); jv.recordParams(getParameterTypes(), Modifier.isStatic(getModifiers())); jv.setMaxLocals(ca.getMaxLocals()); jv.compileStmnt(src); Bytecode b = jv.getBytecode(); ca.setMaxStack(stack); throw new CannotCompileException(e);
cc.checkModify(); CodeAttribute ca = methodInfo.getCodeAttribute(); if (ca == null) throw new CannotCompileException("no method body"); CodeIterator iterator = ca.iterator(); Javac jv = new Javac(cc); try { int nvars = jv.recordParams(getParameterTypes(), Modifier.isStatic(getModifiers())); jv.recordParamNames(ca, nvars); jv.recordLocalVariables(ca, 0); jv.recordType(getReturnType0()); jv.compileStmnt(src); Bytecode b = jv.getBytecode(); int locals = b.getMaxLocals(); if (stack > ca.getMaxStack()) ca.setMaxStack(stack); if (locals > ca.getMaxLocals()) ca.setMaxLocals(locals); throw new CannotCompileException(e); throw new CannotCompileException(e);
/** * Returns true if the called method is of a superclass of the current * class. */ public boolean isSuper() { return iterator.byteAt(currentPos) == INVOKESPECIAL && !where().getDeclaringClass().getName().equals(getClassName()); }
CtClass srcClass = src.getDeclaringClass(); ConstPool cp = declaring.getClassFile2().getConstPool(); map.put(srcClass.getName(), declaring.getName()); try { boolean patch = false; methodInfo = new MethodInfo(cp, srcInfo.getName(), srcInfo, map); if (isCons && patch) methodInfo.setSuperclass(destSuperName); throw new CannotCompileException(e); throw new CannotCompileException(e);
private static void wipeBody(CtBehavior behavior) throws BadBytecode { if ((behavior.getModifiers() & Modifier.ABSTRACT) != 0) { return; } CtClass declaringClass = behavior.getDeclaringClass(); Bytecode b = new Bytecode(declaringClass.getClassFile().getConstPool()); MethodInfo methodInfo = behavior.getMethodInfo(); methodInfo.setCodeAttribute(b.toCodeAttribute()); methodInfo.rebuildStackMapIf6(declaringClass.getClassPool(), declaringClass.getClassFile2()); declaringClass.rebuildClassFile(); }
/** * Returns the immediately enclosing method of this class. * This method works only with JDK 1.5 or later. * * @return null if this class is not a local class or an anonymous * class. * @deprecated The enclosing method might be a constructor. * Use {@link #getEnclosingBehavior()}. * @see #getEnclosingBehavior() */ public final CtMethod getEnclosingMethod() throws NotFoundException { CtBehavior b = getEnclosingBehavior(); if (b == null) return null; else if (b instanceof CtMethod) return (CtMethod)b; else throw new NotFoundException(b.getLongName() + " is enclosing " + getName()); }
private void setBody(CtBehavior m, String body, String jDoc) throws Exception { // for debug StringBuffer args = new StringBuffer(); if (m.getParameterTypes() != null) { int p = 0; for (CtClass ct : m.getParameterTypes()) { p++; args.append(ct.getName() + " $" + p); if (p < m.getParameterTypes().length) { args.append(", "); } } } if (jDoc != null) { debug("/** " + jDoc + " */"); } if (m instanceof CtConstructor) { debug("public " + m.getName() + " ( " + args.toString() + " ) {"); } else { debug("method " + ((CtMethod) m).getReturnType().getName() + " " + m.getName() + " ( " + args.toString() + " ) {"); } debug(body); debug("}"); debug(""); m.setBody("{ " + body + "}"); }
else { CtBehavior cb = compileMethod(p, (MethodDecl)mem); CtClass decl = cb.getDeclaringClass(); cb.getMethodInfo2() .rebuildStackMapIf6(decl.getClassPool(), decl.getClassFile2()); return cb; throw new CompileError(e.getMessage());
private static void wrapMethodInvocation(CtBehavior ctMethodOrConstructor, boolean isStatic) throws CannotCompileException { ctMethodOrConstructor.insertAfter(RobolectricInternals.class.getName() + ".onMethodInvocationFinish(" + (isStatic ? ctMethodOrConstructor.getDeclaringClass().getName() + ".class" : "this") + ");", true); }
private boolean compareThrows(CtBehavior refMethod, CtBehavior implMethod, String className) throws NotFoundException { List refNames = new Vector(); CtClass[] refExceptions = refMethod.getExceptionTypes(); for (int i = 0; i < refExceptions.length; i++) { refNames.add(refExceptions[i].getName()); } boolean ch = false; List implNames = new Vector(); CtClass[] implExceptions = implMethod.getExceptionTypes(); for (int i = 0; i < implExceptions.length; i++) { implNames.add(implExceptions[i].getName()); String exceptionName = implExceptions[i].getName(); ch |= assertTrue(className + " " + refMethod.getName() + refMethod.getSignature() + " should not throw " + exceptionName, refNames.contains(exceptionName)); } if (!config.allowThrowsLess) { for (int i = 0; i < refExceptions.length; i++) { String exceptionName = refExceptions[i].getName(); ch |= assertTrue(className + " " + refMethod.getName() + refMethod.getSignature() + " should throw " + exceptionName, implNames.contains(exceptionName)); } } return ch; }
private void disassemble(final CtBehavior method, final PrintStream out) { out.println(method.getLongName()); final MethodInfo info = method.getMethodInfo2(); final ConstPool constPool = info.getConstPool(); final CodeAttribute code = info.getCodeAttribute(); if (code == null) return; final CodeIterator iterator = code.iterator(); while (iterator.hasNext()) { int pos; try { pos = iterator.next(); } catch (final BadBytecode e) { throw new RuntimeException(e); } out.println(pos + ": " + InstructionPrinter.instructionString(iterator, pos, constPool)); } out.println(""); }