1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.tools
.development
.agent
.impl
;
5 import org
.objectweb
.asm
.ClassAdapter
;
6 import org
.objectweb
.asm
.ClassVisitor
;
7 import org
.objectweb
.asm
.MethodVisitor
;
8 import org
.objectweb
.asm
.Opcodes
;
11 import java
.util
.HashSet
;
15 * Visits method and field instructions, checking for references to
16 * blacklisted classes.
19 public class ObjectAccessVisitor
extends ClassAdapter
{
21 private static final String REJECT
= "reject";
23 private static final String REJECT_DESCRIPTOR
= "(Ljava/lang/String;)V";
25 private static final String CHECK_RESTRICTED
= "checkRestricted";
27 private static final String CHECK_RESTRICTED_DESCRIPTOR
=
28 "(ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V";
30 private final boolean isUserCode
;
31 private final boolean treatRestrictedListViolationsAsErrors
;
32 private final String callingClassName
;
33 private final String callingClassCodeSource
;
34 private final Set
<String
> referencedClasses
= new HashSet
<String
>();
36 public ObjectAccessVisitor(
37 ClassVisitor classVisitor
, boolean isUserCode
,
38 boolean treatRestrictedListViolationsAsErrors
, String callingClassName
, URL callingClassCodeSource
) {
40 this.isUserCode
= isUserCode
;
41 this.treatRestrictedListViolationsAsErrors
= treatRestrictedListViolationsAsErrors
;
42 this.callingClassName
= callingClassName
;
43 this.callingClassCodeSource
=
44 callingClassCodeSource
== null ?
"unknown location" : callingClassCodeSource
.toString();
48 public MethodVisitor
visitMethod(int access
, String name
, String desc
, String signature
,
49 String
[] exceptions
) {
50 MethodVisitor mv
= super.visitMethod(access
, name
, desc
, signature
, exceptions
);
51 return (mv
== null) ?
null : new MethodTranslator(mv
, access
, name
, desc
);
54 private class MethodTranslator
extends NonRecordingGeneratorAdapter
{
55 MethodTranslator(MethodVisitor methodVisitor
, int access
, String name
, String desc
) {
56 super(methodVisitor
, access
, name
, desc
);
60 public void visitFieldInsn(int opcode
, String owner
, String name
, String desc
) {
62 super.visitFieldInsn(opcode
, owner
, name
, desc
);
66 public void visitMethodInsn(int opcode
, String owner
, String name
, String desc
) {
68 super.visitMethodInsn(opcode
, owner
, name
, desc
);
71 private void maybeReject(String klass
) {
72 if (BlackList
.getBlackList().contains(klass
)) {
73 super.push(klass
.replace('/', '.'));
74 super.visitMethodInsn(Opcodes
.INVOKESTATIC
, AgentImpl
.AGENT_RUNTIME
,
75 REJECT
, REJECT_DESCRIPTOR
);
76 } else if (isUserCode
&& !referencedClasses
.contains(klass
)) {
77 referencedClasses
.add(klass
);
78 super.push(treatRestrictedListViolationsAsErrors
);
79 super.push(klass
.replace('/', '.'));
80 super.push(callingClassName
);
81 super.push(callingClassCodeSource
);
82 super.visitMethodInsn(Opcodes
.INVOKESTATIC
, AgentImpl
.AGENT_RUNTIME
,
83 CHECK_RESTRICTED
, CHECK_RESTRICTED_DESCRIPTOR
);