下面列出了org.objectweb.asm.Opcodes# NEW 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void visitTypeInsn(final int opcode, final String type) {
Type objectType = Type.getObjectType(type);
switch (opcode) {
case Opcodes.NEW:
anew(objectType);
break;
case Opcodes.ANEWARRAY:
newarray(objectType);
break;
case Opcodes.CHECKCAST:
checkcast(objectType);
break;
case Opcodes.INSTANCEOF:
instanceOf(objectType);
break;
default:
throw new IllegalArgumentException();
}
}
@Override
public void visitTypeInsn(final int opcode, final String type) {
Type objectType = Type.getObjectType(type);
switch (opcode) {
case Opcodes.NEW:
anew(objectType);
break;
case Opcodes.ANEWARRAY:
newarray(objectType);
break;
case Opcodes.CHECKCAST:
checkcast(objectType);
break;
case Opcodes.INSTANCEOF:
instanceOf(objectType);
break;
default:
throw new IllegalArgumentException();
}
}
private void addWhereAmI() {
// 0: new #2; //class java/lang/Exception
// 3: dup
// 4: invokespecial #3; //Method java/lang/Exception."<init>":()V
// 7: invokevirtual #4; //Method
// java/lang/Exception.printStackTrace:()V
// 10: return
// super.visitTypeInsn(Opcodes.NEW, type);
String exClass = Type.getInternalName(Exception.class);
super.visitTypeInsn(Opcodes.NEW, exClass);
super.visitInsn(Opcodes.DUP);
super.visitMethodInsn(Opcodes.INVOKESPECIAL, exClass, "<init>",
"()V");
super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, exClass,
"printStackTrace", "()V");
}
@Override
public void visitTypeInsn(final int opcode, final String type) {
Type objectType = Type.getObjectType(type);
switch (opcode) {
case Opcodes.NEW:
anew(objectType);
break;
case Opcodes.ANEWARRAY:
newarray(objectType);
break;
case Opcodes.CHECKCAST:
checkcast(objectType);
break;
case Opcodes.INSTANCEOF:
instanceOf(objectType);
break;
default:
throw new IllegalArgumentException();
}
}
@Override
public void visitTypeInsn(int opcode, String s) {
if (opcode == Opcodes.NEW) {
// state can only normal or dup_after new
if (state == MachineState.AFTER_NEW) {
throw new RuntimeException("Panic, two NEW opcode without a DUP");
}
if (isInSamePackage(s)) {
// this is a new allocation in the same package, this could be protected or
// package private class, we must go through reflection, otherwise not.
// set our state so we swallow the next DUP we encounter.
state = MachineState.AFTER_NEW;
// swallow the NEW, we will also swallow the DUP associated with the new
return;
}
}
super.visitTypeInsn(opcode, s);
}
/**
* {@inheritDoc}
*
* @see org.objectweb.asm.MethodVisitor#visitTypeInsn(int, java.lang.String)
*/
@Override
public void visitTypeInsn ( int opcode, String type ) {
JVMImpl.handleJVMTypeInsn(opcode, type, this.stack);
if ( opcode == Opcodes.NEW ) {
String className = type.replace('/', '.');
if ( this.log.isDebugEnabled() ) {
this.log.debug("Found instantiation " + className); //$NON-NLS-1$
}
// if ( this.ref.isCalleeTainted() ) {
this.parent.getAnalyzer().getState().trackInstantiable(className, this.ref, this.parent.getAnalyzer().getConfig(), false);
// }
}
super.visitTypeInsn(opcode, type);
}
private void removeLastAllocation() {
AbstractInsnNode insn = instructions.getLast();
while (insn != null && insn.getPrevious() != null) {
AbstractInsnNode prev = insn.getPrevious();
if (prev.getOpcode() == Opcodes.NEW
&& insn.getOpcode() == Opcodes.DUP
&& ((TypeInsnNode) prev).desc.equals(lambdaInfo.methodReference().getOwner())) {
instructions.remove(prev);
instructions.remove(insn);
return;
}
insn = prev;
}
throw new IllegalStateException(
"Couldn't find allocation to rewrite ::new reference " + lambdaInfo.methodReference());
}
@Override
public void visitTypeInsn(int opcode, String type) {
if (opcode == Opcodes.NEW && LEVEL_GENERATOR_TYPE_NAME.equals(type)) {
type = PATCHWORK_LEVEL_GENERATOR_TYPE_NAME;
}
super.visitTypeInsn(opcode, type);
}
private boolean willPush(AbstractInsnNode ain)
{
if(ain.getOpcode() == Opcodes.LDC && (((LdcInsnNode)ain).cst instanceof Long || ((LdcInsnNode)ain).cst instanceof Double))
return false;
return (Utils.willPushToStack(ain.getOpcode()) || ain.getOpcode() == Opcodes.NEW) && ain.getOpcode() != Opcodes.GETSTATIC
&& ain.getOpcode() != Opcodes.LLOAD && ain.getOpcode() != Opcodes.DLOAD;
}
/**
* Find the call to <tt>super()</tt> or <tt>this()</tt> in a constructor.
* This attempts to locate the first call to <tt><init></tt> which
* isn't an inline call to another object ctor being passed into the super
* invocation.
*
* @param ctor ctor to scan
* @param superName name of superclass
* @param ownerName name of owning class
* @return Call to <tt>super()</tt>, <tt>this()</tt> or
* <tt>DelegateInitialiser.NONE</tt> if not found
*/
public static DelegateInitialiser findDelegateInit(MethodNode ctor, String superName, String ownerName) {
if (!Constants.CTOR.equals(ctor.name)) {
return DelegateInitialiser.NONE;
}
int news = 0;
for (Iterator<AbstractInsnNode> iter = ctor.instructions.iterator(); iter.hasNext();) {
AbstractInsnNode insn = iter.next();
if (insn instanceof TypeInsnNode && insn.getOpcode() == Opcodes.NEW) {
news++;
} else if (insn instanceof MethodInsnNode && insn.getOpcode() == Opcodes.INVOKESPECIAL) {
MethodInsnNode methodNode = (MethodInsnNode)insn;
if (Constants.CTOR.equals(methodNode.name)) {
if (news > 0) {
news--;
} else {
boolean isSuper = methodNode.owner.equals(superName);
if (isSuper || methodNode.owner.equals(ownerName)) {
return new DelegateInitialiser(methodNode, isSuper);
}
}
}
}
}
return DelegateInitialiser.NONE;
}
@SuppressWarnings("unchecked")
@Override
public boolean find(String desc, InsnList insns, Collection<AbstractInsnNode> nodes) {
boolean found = false;
int ordinal = 0;
Collection<TypeInsnNode> newNodes = new ArrayList<TypeInsnNode>();
Collection<AbstractInsnNode> candidates = (Collection<AbstractInsnNode>) (this.desc != null ? newNodes : nodes);
ListIterator<AbstractInsnNode> iter = insns.iterator();
while (iter.hasNext()) {
AbstractInsnNode insn = iter.next();
if (insn instanceof TypeInsnNode && insn.getOpcode() == Opcodes.NEW && this.matchesOwner((TypeInsnNode) insn)) {
if (this.ordinal == -1 || this.ordinal == ordinal) {
candidates.add(insn);
found = this.desc == null;
}
ordinal++;
}
}
if (this.desc != null) {
for (TypeInsnNode newNode : newNodes) {
if (this.findCtor(insns, newNode)) {
nodes.add(newNode);
found = true;
}
}
}
return found;
}
@Override
public void visitTypeInsn(int opcode, String type) {
this.currentBuildingBlock.add(opcode);
// If this is a new, att the type to the allocation list for the block.
if (Opcodes.NEW == opcode) {
this.currentAllocationList.add(type);
}
}
@Test
public void test_throwException() throws Exception {
List<BasicBlock> initBlocks = METHOD_BLOCKS.get("throwException()I");
int[][] expectedInitBlocks = new int[][]{
{Opcodes.NEW, Opcodes.DUP, Opcodes.INVOKESPECIAL, Opcodes.ATHROW},
};
boolean didMatch = compareBlocks(expectedInitBlocks, initBlocks);
Assert.assertTrue(didMatch);
BytecodeFeeScheduler s = new BytecodeFeeScheduler();
s.initialize();
}
@Override
public void visitTypeInsn(final int opcode, final String type) {
checkVisitCodeCalled();
checkVisitMaxsNotCalled();
checkOpcodeMethod(opcode, Method.VISIT_TYPE_INSN);
checkInternalName(version, type, "type");
if (opcode == Opcodes.NEW && type.charAt(0) == '[') {
throw new IllegalArgumentException("NEW cannot be used to create arrays: " + type);
}
super.visitTypeInsn(opcode, type);
++insnCount;
}
@Override
public void visitTypeInsn(final int opcode, final String type) {
checkStartCode();
checkEndCode();
checkOpcode(opcode, 3);
checkInternalName(type, "type");
if (opcode == Opcodes.NEW && type.charAt(0) == '[') {
throw new IllegalArgumentException("NEW cannot be used to create arrays: " + type);
}
super.visitTypeInsn(opcode, type);
++insnCount;
}
@Override
public void visitEnd() {
if (!hasCode) {
super.visitTypeInsn(Opcodes.NEW, EXCEPTION_INTERNAL_NAME);
super.visitInsn(Opcodes.DUP);
super.visitMethodInsn(
Opcodes.INVOKESPECIAL, EXCEPTION_INTERNAL_NAME, "<init>", "()V", /*itf*/ false);
super.visitInsn(Opcodes.ATHROW);
super.visitMaxs(0, 0); // triggers computation of the actual max's
}
super.visitEnd();
}
private void patchMethod(Map<String, AtomicInteger> invokeCount, MethodNode method) {
if (invokeCount.containsKey("getStackTrace") && invokeCount.containsKey("getClassName")) {
for (AbstractInsnNode insn = method.instructions.getFirst(); insn != null; insn = insn.getNext()) {
if (insn.getOpcode() == Opcodes.NEW && (((TypeInsnNode) insn).desc.endsWith("Exception") || ((TypeInsnNode) insn).desc.endsWith("Error"))) {
((TypeInsnNode) insn).desc = "java/lang/RuntimeException";
} else if (insn instanceof MethodInsnNode && (((MethodInsnNode) insn).owner.endsWith("Exception") || ((MethodInsnNode) insn).owner.endsWith("Error"))) {
((MethodInsnNode) insn).owner = "java/lang/RuntimeException";
}
}
}
}
@Override
protected void addTargetNode(Target target, List<InjectionNode> myNodes, AbstractInsnNode insn, Set<InjectionPoint> nominators) {
InjectionNode node = target.getInjectionNode(insn);
ConstructorRedirectData ctorData = null;
int fuzz = BeforeFieldAccess.ARRAY_SEARCH_FUZZ_DEFAULT;
int opcode = 0;
if (insn instanceof MethodInsnNode && Constants.CTOR.equals(((MethodInsnNode)insn).name)) {
throw new InvalidInjectionException(this.info, String.format("Illegal %s of constructor specified on %s",
this.annotationType, this));
}
if (node != null ) {
Meta other = node.getDecoration(Meta.KEY);
if (other != null && other.getOwner() != this) {
if (other.priority >= this.meta.priority) {
Injector.logger.warn("{} conflict. Skipping {} with priority {}, already redirected by {} with priority {}",
this.annotationType, this.info, this.meta.priority, other.name, other.priority);
return;
} else if (other.isFinal) {
throw new InvalidInjectionException(this.info, String.format("%s conflict: %s failed because target was already remapped by %s",
this.annotationType, this, other.name));
}
}
}
for (InjectionPoint ip : nominators) {
if (ip instanceof BeforeNew) {
ctorData = this.getCtorRedirect((BeforeNew)ip);
ctorData.wildcard = !((BeforeNew)ip).hasDescriptor();
} else if (ip instanceof BeforeFieldAccess) {
BeforeFieldAccess bfa = (BeforeFieldAccess)ip;
fuzz = bfa.getFuzzFactor();
opcode = bfa.getArrayOpcode();
}
}
InjectionNode targetNode = target.addInjectionNode(insn);
targetNode.decorate(Meta.KEY, this.meta);
targetNode.decorate(RedirectInjector.KEY_NOMINATORS, nominators);
if (insn instanceof TypeInsnNode && insn.getOpcode() == Opcodes.NEW) {
targetNode.decorate(ConstructorRedirectData.KEY, ctorData);
} else {
targetNode.decorate(RedirectInjector.KEY_FUZZ, Integer.valueOf(fuzz));
targetNode.decorate(RedirectInjector.KEY_OPCODE, Integer.valueOf(opcode));
}
myNodes.add(targetNode);
}
@Override
protected void inject(Target target, InjectionNode node) {
if (!this.preInject(node)) {
return;
}
if (node.isReplaced()) {
throw new UnsupportedOperationException("Redirector target failure for " + this.info);
}
if (node.getCurrentTarget() instanceof MethodInsnNode) {
this.checkTargetForNode(target, node, RestrictTargetLevel.ALLOW_ALL);
this.injectAtInvoke(target, node);
return;
}
if (node.getCurrentTarget() instanceof FieldInsnNode) {
this.checkTargetForNode(target, node, RestrictTargetLevel.ALLOW_ALL);
this.injectAtFieldAccess(target, node);
return;
}
if (node.getCurrentTarget() instanceof TypeInsnNode) {
int opcode = node.getCurrentTarget().getOpcode();
if (opcode == Opcodes.NEW) {
if (!this.isStatic && target.isStatic) {
throw new InvalidInjectionException(this.info, String.format(
"non-static callback method %s has a static target which is not supported", this));
}
this.injectAtConstructor(target, node);
return;
} else if (opcode == Opcodes.INSTANCEOF) {
this.checkTargetModifiers(target, false);
this.injectAtInstanceOf(target, node);
return;
}
}
throw new InvalidInjectionException(this.info, String.format("%s annotation on is targetting an invalid insn in %s in %s",
this.annotationType, target, this));
}
public TypeInsnNode() {
super(Opcodes.NEW);
desc = "";
}