List of usage examples for org.objectweb.asm Opcodes ACC_PRIVATE
int ACC_PRIVATE
To view the source code for org.objectweb.asm Opcodes ACC_PRIVATE.
Click Source Link
From source file:org.evosuite.runtime.instrumentation.CreateClassResetClassAdapter.java
License:Open Source License
@Deprecated // This method is a code clone from MethodCallReplacementClassAdapter private void createSerialisableUID() { // Only add this for serialisable classes if (serialUID < 0) return;/*from w w w. ja v a 2s .c o m*/ /* * If the class is serializable, then adding a hashCode will change the * serialVersionUID if it is not defined in the class. Hence, if it is * not defined, we have to define it to avoid problems in serialising * the class. */ logger.info("Adding serialId to class " + className); visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "serialVersionUID", "J", null, serialUID); }
From source file:org.evosuite.runtime.instrumentation.MethodCallReplacementClassAdapter.java
License:Open Source License
@Override public void visitEnd() { if (canChangeSignature && !definesHashCode && !isInterface && RuntimeSettings.mockJVMNonDeterminism) { logger.info("No hashCode defined for: " + className + ", superclass = " + superClassName); if (superClassName.equals("java.lang.Object")) { //TODO: why only if superclass is Object??? unclear Method hashCodeMethod = Method.getMethod("int hashCode()"); GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, hashCodeMethod, null, null, this); mg.loadThis();/*from www . ja v a 2 s . c o m*/ mg.visitAnnotation(Type.getDescriptor(EvoSuiteExclude.class), true); mg.invokeStatic(Type.getType(org.evosuite.runtime.System.class), Method.getMethod("int identityHashCode(Object)")); mg.returnValue(); mg.endMethod(); } } /* * If the class is serializable, then doing any change (adding hashCode, static reset, etc) * will change the serialVersionUID if it is not defined in the class. * Hence, if it is not defined, we have to define it to * avoid problems in serialising the class, as reading Master will not do instrumentation. * The serialVersionUID HAS to be the same as the un-instrumented class */ if (!definesUid && !isInterface && RuntimeSettings.applyUIDTransformation) { ClassLoader threadCL = Thread.currentThread().getContextClassLoader(); try { ClassLoader evoCL = MethodCallReplacementClassAdapter.class.getClassLoader(); Thread.currentThread().setContextClassLoader(evoCL); Class<?> clazz = Class.forName(className.replace('/', '.'), false, evoCL); if (Serializable.class.isAssignableFrom(clazz)) { ObjectStreamClass c = ObjectStreamClass.lookup(clazz); long serialID = c.getSerialVersionUID(); logger.info("Adding serialId to class " + className); visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "serialVersionUID", "J", null, serialID); } } catch (ClassNotFoundException e) { logger.warn("Failed to add serialId to class " + className + ": " + e.getMessage()); } catch (NoClassDefFoundError e) { logger.warn("Failed to add serialId to class " + className + ": " + e.getMessage()); } finally { Thread.currentThread().setContextClassLoader(threadCL); } } super.visitEnd(); }
From source file:org.evosuite.setup.InheritanceTreeGenerator.java
License:Open Source License
public static boolean canUse(ClassNode cn) { if ((cn.access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE) { logger.debug(cn.name + " is private, ignoring it"); return false; }/*from w ww. j a v a 2 s . c om*/ if (ANONYMOUS_MATCHER1.matcher(cn.name).matches()) { logger.debug(cn.name + " looks like an anonymous class, ignoring it"); return false; } if (ANONYMOUS_MATCHER2.matcher(cn.name).matches()) { logger.debug(cn.name + " looks like an anonymous class, ignoring it"); return false; } // TODO: Handle Deprecated if (cn.name.startsWith("junit")) return false; // If the SUT is not in the default package, then // we cannot import classes that are in the default // package /* if ((cn.access & Opcodes.ACC_PUBLIC) != Opcodes.ACC_PUBLIC) { String nameWithDots = cn.name.replace('/', '.'); String packageName = ClassUtils.getPackageName(nameWithDots); if (!packageName.equals(Properties.CLASS_PREFIX)) { return false; } } */ // ASM has some problem with the access of inner classes // so we check if the inner class name is the current class name // and if so, check if the inner class is actually accessible @SuppressWarnings("unchecked") List<InnerClassNode> in = cn.innerClasses; for (InnerClassNode inc : in) { if (cn.name.equals(inc.name)) { // logger.debug("ASM weird behaviour: Inner class equals class: " + inc.name); if ((inc.access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE) { return false; } /* if ((inc.access & Opcodes.ACC_PUBLIC) != Opcodes.ACC_PUBLIC) { String nameWithDots = inc.name.replace('/', '.'); String packageName = ClassUtils.getPackageName(nameWithDots); if (!packageName.equals(Properties.CLASS_PREFIX)) { return false; } } */ logger.debug("Can use inner class: " + inc.name); return true; } } logger.debug(cn.name + " is accessible"); return true; }
From source file:org.evosuite.symbolic.instrument.AccessFlags.java
License:Open Source License
static boolean isPrivate(int access) { return is(access, Opcodes.ACC_PRIVATE); }
From source file:org.evosuite.testcarver.instrument.Instrumenter.java
License:Open Source License
@SuppressWarnings("unchecked") public void transformClassNode(ClassNode cn, final String internalClassName) { if (!TransformerUtil.isClassConsideredForInstrumentation(internalClassName)) { logger.debug("Class {} has not been instrumented because its name is on the blacklist", internalClassName);/*from w w w . j a v a 2s . c om*/ return; } // consider only public and protected classes which are not interfaces if ((cn.access & Opcodes.ACC_INTERFACE) != 0) { return; } // No private if ((cn.access & Opcodes.ACC_PRIVATE) != 0) { // TODO: Why is this not detecting $DummyIntegrator? logger.debug("Ignoring private class {}", cn.name); return; } String packageName = internalClassName.replace('/', '.'); if (packageName.contains(".")) packageName = packageName.substring(0, packageName.lastIndexOf('.')); // ASM has some problem with the access of inner classes // so we check if the inner class name is the current class name // and if so, check if the inner class is actually accessible List<InnerClassNode> in = cn.innerClasses; for (InnerClassNode inc : in) { if (cn.name.equals(inc.name)) { logger.info("ASM Bug: Inner class equals class."); if ((inc.access & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED) { if (!Properties.CLASS_PREFIX.equals(packageName)) { return; } } if ((inc.access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE) { return; } logger.debug("Can use inner class {}", inc.name); } } logger.info("Checking package {} for class {}", packageName, cn.name); // Protected/default only if in same package if ((cn.access & Opcodes.ACC_PUBLIC) == 0) { if (!Properties.CLASS_PREFIX.equals(packageName)) { logger.info("Not using protected/default class because package name does not match"); return; } else { logger.info("Using protected/default class because package name matches"); } } /* if( (cn.access & Opcodes.ACC_PUBLIC) == 0 && (cn.access & Opcodes.ACC_PROTECTED) == 0) { return; } */ final ArrayList<MethodNode> wrappedMethods = new ArrayList<MethodNode>(); MethodNode methodNode; final Iterator<MethodNode> methodIter = cn.methods.iterator(); while (methodIter.hasNext()) { methodNode = methodIter.next(); // consider only public methods which are not abstract or native if (!TransformerUtil.isPrivate(methodNode.access) && !TransformerUtil.isAbstract(methodNode.access) && !TransformerUtil.isNative(methodNode.access) && !methodNode.name.equals("<clinit>")) { if (!TransformerUtil.isPublic(methodNode.access)) { //if(!Properties.CLASS_PREFIX.equals(packageName)) { transformWrapperCalls(methodNode); continue; //} } if (methodNode.name.equals("<init>")) { if (TransformerUtil.isAbstract(cn.access)) { // We cannot invoke constructors of abstract classes directly continue; } this.addFieldRegistryRegisterCall(methodNode); } this.instrumentPUTXXXFieldAccesses(cn, internalClassName, methodNode); this.instrumentGETXXXFieldAccesses(cn, internalClassName, methodNode); this.instrumentMethod(cn, internalClassName, methodNode, wrappedMethods); } else { transformWrapperCalls(methodNode); } } final int numWM = wrappedMethods.size(); for (int i = 0; i < numWM; i++) { cn.methods.add(wrappedMethods.get(i)); } TraceClassVisitor tcv = new TraceClassVisitor(new PrintWriter(System.err)); cn.accept(tcv); }
From source file:org.evosuite.testcarver.instrument.Instrumenter.java
License:Open Source License
/** * public int myMethod(int i)//from w w w . j a va 2 s. c o m { try { return _sw_prototype_original_myMethod(i) } finally { Capturer.enable(); } } * @param classNode * @param className * @param methodNode */ @SuppressWarnings("unchecked") private MethodNode wrapMethod(final ClassNode classNode, final String className, final MethodNode methodNode) { methodNode.maxStack += 4; // create wrapper for original method final MethodNode wrappingMethodNode = new MethodNode(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()])); wrappingMethodNode.maxStack = methodNode.maxStack; // assign annotations to wrapping method wrappingMethodNode.visibleAnnotations = methodNode.visibleAnnotations; wrappingMethodNode.visibleParameterAnnotations = methodNode.visibleParameterAnnotations; // remove annotations from wrapped method to avoid wrong behavior controlled by annotations methodNode.visibleAnnotations = null; methodNode.visibleParameterAnnotations = null; // rename original method methodNode.access = TransformerUtil.modifyVisibility(methodNode.access, Opcodes.ACC_PRIVATE); final LabelNode l0 = new LabelNode(); final LabelNode l1 = new LabelNode(); final LabelNode l2 = new LabelNode(); final InsnList wInstructions = wrappingMethodNode.instructions; if ("<init>".equals(methodNode.name)) { // wrap a constructor methodNode.name = WRAP_NAME_PREFIX + "init" + WRAP_NAME_PREFIX; // move call to other constructors to new method AbstractInsnNode ins = null; ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator(); int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call while (iter.hasNext()) { ins = iter.next(); iter.remove(); wInstructions.add(ins); if (ins instanceof MethodInsnNode) { MethodInsnNode mins = (MethodInsnNode) ins; if (ins.getOpcode() == Opcodes.INVOKESPECIAL) { if (mins.name.startsWith("<init>")) { if (numInvokeSpecials == 0) { break; } else { numInvokeSpecials--; } } } } else if (ins instanceof TypeInsnNode) { TypeInsnNode typeIns = (TypeInsnNode) ins; if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) { numInvokeSpecials++; } } } } else { methodNode.name = WRAP_NAME_PREFIX + methodNode.name; } int varReturnValue = 0; final Type returnType = Type.getReturnType(methodNode.desc); if (returnType.equals(Type.VOID_TYPE)) { wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, "java/lang/Throwable")); } else { wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Throwable")); //--- create "Object returnValue = null;" if (!TransformerUtil.isStatic(methodNode.access)) { // load "this" varReturnValue++; } // consider method arguments to find right variable index final Type[] argTypes = Type.getArgumentTypes(methodNode.desc); for (int i = 0; i < argTypes.length; i++) { varReturnValue++; // long/double take two registers if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) { varReturnValue++; } } // push NULL on the stack and initialize variable for return value for it wInstructions.add(new InsnNode(Opcodes.ACONST_NULL)); wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue)); } int var = 0; // --- L0 wInstructions.add(l0); wInstructions.add(this.addCaptureCall(TransformerUtil.isStatic(methodNode.access), className, wrappingMethodNode.name, wrappingMethodNode.desc, Type.getArgumentTypes(methodNode.desc))); // --- construct call to wrapped methode if (!TransformerUtil.isStatic(methodNode.access)) { // load "this" to call method wInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0)); var++; } final Type[] argTypes = Type.getArgumentTypes(methodNode.desc); for (int i = 0; i < argTypes.length; i++) { this.addLoadInsn(wInstructions, argTypes[i], var++); // long/double take two registers if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) { var++; } } if (TransformerUtil.isStatic(methodNode.access)) { wInstructions.add( new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name, methodNode.name, methodNode.desc)); } else { wInstructions.add( new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, methodNode.name, methodNode.desc)); } var++; if (returnType.equals(Type.VOID_TYPE)) { wInstructions.add(new JumpInsnNode(Opcodes.GOTO, l2)); // --- L1 wInstructions.add(l1); wInstructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" })); wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var)); this.addCaptureEnableStatement(className, methodNode, wInstructions, -1); wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var)); wInstructions.add(new InsnNode(Opcodes.ATHROW)); // FIXME <--- DUPLICATE CODE // --- L2 wInstructions.add(l2); wInstructions.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null)); this.addCaptureEnableStatement(className, methodNode, wInstructions, -1); wInstructions.add(new InsnNode(Opcodes.RETURN)); } else { // construct store of the wrapped method call's result this.addBoxingStmt(wInstructions, returnType); wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue)); wInstructions.add(new VarInsnNode(Opcodes.ALOAD, varReturnValue)); this.addUnBoxingStmt(wInstructions, returnType); final int storeOpcode = returnType.getOpcode(Opcodes.ISTORE); wInstructions.add(new VarInsnNode(storeOpcode, ++var)); // might be only var // --- L1 wInstructions.add(l1); this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue); // construct load of the wrapped method call's result int loadOpcode = returnType.getOpcode(Opcodes.ILOAD); wInstructions.add(new VarInsnNode(loadOpcode, var)); // construct return of the wrapped method call's result this.addReturnInsn(wInstructions, returnType); //---- L2 wInstructions.add(l2); wInstructions.add( new FrameNode(Opcodes.F_FULL, 2, new Object[] { className, this.getInternalName(returnType) }, 1, new Object[] { "java/lang/Throwable" })); wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var)); this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue); wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var)); wInstructions.add(new InsnNode(Opcodes.ATHROW)); } transformWrapperCalls(methodNode); return wrappingMethodNode; }
From source file:org.evosuite.testcarver.instrument.TransformerUtil.java
License:Open Source License
public static int modifyVisibility(int access, final int targetAccess) { access &= ~Opcodes.ACC_PRIVATE; access &= ~Opcodes.ACC_PROTECTED; access &= ~Opcodes.ACC_PUBLIC;/* w w w . j av a2 s. c o m*/ return access |= targetAccess; }
From source file:org.fehrman.jcm.find.Find.java
License:CDDL license
@SuppressWarnings("unchecked") private String processMethod(MethodNode methodNode) throws Exception //---------------------------------------------------------------- { StringBuilder methodDescription = new StringBuilder(); String className = null;/*from www. j a v a 2 s .c om*/ Type returnType = Type.getReturnType(methodNode.desc); Type[] argumentTypes = Type.getArgumentTypes(methodNode.desc); List<String> thrownInternalClassNames = methodNode.exceptions; _logger.entering(CLASS, Thread.currentThread().getStackTrace()[1].getMethodName()); if ((methodNode.access & Opcodes.ACC_PUBLIC) != 0) { methodDescription.append("public "); } if ((methodNode.access & Opcodes.ACC_PRIVATE) != 0) { methodDescription.append("private "); } if ((methodNode.access & Opcodes.ACC_PROTECTED) != 0) { methodDescription.append("protected "); } if ((methodNode.access & Opcodes.ACC_STATIC) != 0) { methodDescription.append("static "); } if ((methodNode.access & Opcodes.ACC_ABSTRACT) != 0) { methodDescription.append("abstract "); } if ((methodNode.access & Opcodes.ACC_SYNCHRONIZED) != 0) { methodDescription.append("synchronized "); } className = returnType.getClassName(); if (className != null && className.startsWith(JAVA_LANG)) { methodDescription.append(className.substring(JAVA_LANG.length())); } else { methodDescription.append(className); } methodDescription.append(" "); methodDescription.append(methodNode.name); methodDescription.append("("); for (int i = 0; i < argumentTypes.length; i++) { Type argumentType = argumentTypes[i]; if (i > 0) { methodDescription.append(", "); } className = argumentType.getClassName(); if (className != null && className.startsWith(JAVA_LANG)) { methodDescription.append(className.substring(JAVA_LANG.length())); } else { methodDescription.append(className); } } methodDescription.append(")"); if (!thrownInternalClassNames.isEmpty()) { methodDescription.append(" throws "); int i = 0; for (String thrownInternalClassName : thrownInternalClassNames) { if (i > 0) { methodDescription.append(", "); } className = Type.getObjectType(thrownInternalClassName).getClassName(); if (className != null && className.startsWith(JAVA_LANG)) { methodDescription.append(className.substring(JAVA_LANG.length())); } else { methodDescription.append(className); } i++; } } _logger.exiting(CLASS, Thread.currentThread().getStackTrace()[1].getMethodName()); return methodDescription.toString(); }
From source file:org.formulacompiler.compiler.internal.bytecode.ArrayAccessorForConstDataCompiler.java
License:Open Source License
@Override protected void compileBody() throws CompilerException { final GeneratorAdapter mv = mv(); final ArrayDescriptor dim = this.arrayNode.arrayDescriptor(); final int n = dim.numberOfElements(); final DataType eltDataType = this.arrayNode.getDataType(); final TypeCompiler eltCompiler = section().engineCompiler().typeCompiler(eltDataType); final Type eltType = eltCompiler.type(); // private double[] xy; final FieldVisitor fv = cw().visitField(Opcodes.ACC_PRIVATE, methodName(), arrayDescriptor(), null, null); fv.visitEnd();/*from w w w .jav a2 s . c o m*/ // if (this.xy == null) { final Label skipInit = mv.newLabel(); mv.loadThis(); mv.visitFieldInsn(Opcodes.GETFIELD, section().classInternalName(), methodName(), arrayDescriptor()); mv.ifNonNull(skipInit); // ... new double[ n ] mv.loadThis(); mv.push(n); mv.newArray(eltType); // ... { c1, c2, ... } int i = 0; for (ExpressionNode elt : this.arrayNode.arguments()) { if (elt instanceof ExpressionNodeForConstantValue) { mv.visitInsn(Opcodes.DUP); mv.push(i); final ExpressionNodeForConstantValue constElt = (ExpressionNodeForConstantValue) elt; eltCompiler.compileConst(mv, constElt.value()); mv.arrayStore(eltType); } i++; } // this.xy *=* new double[] { ... } mv.visitFieldInsn(Opcodes.PUTFIELD, section().classInternalName(), methodName(), arrayDescriptor()); // } // return this.xy; mv.mark(skipInit); mv.loadThis(); mv.visitFieldInsn(Opcodes.GETFIELD, section().classInternalName(), methodName(), arrayDescriptor()); mv.visitInsn(Opcodes.ARETURN); }
From source file:org.formulacompiler.compiler.internal.bytecode.ArrayAccessorForFullDataCompiler.java
License:Open Source License
@Override protected void compileBody() throws CompilerException { final GeneratorAdapter mv = mv(); final DataType eltDataType = this.arrayNode.getDataType(); final ExpressionCompiler eltCompiler = expressionCompiler(eltDataType); final Type eltType = eltCompiler.type(); final String initName = methodName() + "$init"; final String initDesc = "Z"; // private boolean xy$init; final FieldVisitor fv = cw().visitField(Opcodes.ACC_PRIVATE, initName, initDesc, null, null); fv.visitEnd();//from www .j a v a 2 s . c o m // if (!this.xy$init) { final Label skipInit = mv.newLabel(); mv.loadThis(); mv.visitFieldInsn(Opcodes.GETFIELD, section().classInternalName(), initName, initDesc); mv.visitJumpInsn(Opcodes.IFNE, skipInit); // this.xy$init = true; mv.loadThis(); mv.push(true); mv.visitFieldInsn(Opcodes.PUTFIELD, section().classInternalName(), initName, initDesc); // this.xy = { ?, c1, c2, ... } mv.loadThis(); section().getArrayAccessorForConstDataOnly(this.arrayNode).compileCall(mv); int i = 0; for (ExpressionNode elt : this.arrayNode.arguments()) { if (!(elt instanceof ExpressionNodeForConstantValue)) { mv.visitInsn(Opcodes.DUP); mv.visitIntInsn(Opcodes.BIPUSH, i); eltCompiler.compile(elt); mv.arrayStore(eltType); } i++; } // return this.xy; mv.visitInsn(Opcodes.ARETURN); // } else // return this.xy; mv.mark(skipInit); mv.loadThis(); section().getArrayAccessorForConstDataOnly(this.arrayNode).compileCall(mv); mv.visitInsn(Opcodes.ARETURN); if (section().hasReset()) { final GeneratorAdapter reset = section().resetter(); // this.xy$init = false; reset.loadThis(); reset.push(false); reset.visitFieldInsn(Opcodes.PUTFIELD, section().classInternalName(), initName, initDesc); } }