|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright 2000-2009 JetBrains s.r.o. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.intellij.compiler.notNullVerification; |
| 18 | + |
| 19 | +import org.objectweb.asm.*; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author ven |
| 25 | + * @noinspection HardCodedStringLiteral |
| 26 | + * |
| 27 | + * Note: this is copy of {@link NotNullVerifyingInstrumenter} which supported JSR 305 annotation |
| 28 | + */ |
| 29 | +public class NonnullVerifyingInstrumenter extends ClassAdapter implements Opcodes { |
| 30 | + |
| 31 | + private boolean myIsModification = false; |
| 32 | + private String myClassName; |
| 33 | + |
| 34 | + public static final String NOT_NULL = "javax/annotation/Nonnull"; |
| 35 | + public static final String NOT_NULL_ANNO = "L" + NOT_NULL + ";"; |
| 36 | + public static final String IAE_CLASS_NAME = "java/lang/IllegalArgumentException"; |
| 37 | + public static final String ISE_CLASS_NAME = "java/lang/IllegalStateException"; |
| 38 | + private static final String CONSTRUCTOR_NAME = "<init>"; |
| 39 | + |
| 40 | + public NonnullVerifyingInstrumenter(final ClassVisitor classVisitor) { |
| 41 | + super(classVisitor); |
| 42 | + } |
| 43 | + |
| 44 | + public boolean isModification() { |
| 45 | + return myIsModification; |
| 46 | + } |
| 47 | + |
| 48 | + public void visit(final int version, |
| 49 | + final int access, |
| 50 | + final String name, |
| 51 | + final String signature, |
| 52 | + final String superName, |
| 53 | + final String[] interfaces) { |
| 54 | + super.visit(version, access, name, signature, superName, interfaces); |
| 55 | + myClassName = name; |
| 56 | + } |
| 57 | + |
| 58 | + public MethodVisitor visitMethod( |
| 59 | + final int access, |
| 60 | + final String name, |
| 61 | + final String desc, |
| 62 | + final String signature, |
| 63 | + final String[] exceptions) { |
| 64 | + final Type[] args = Type.getArgumentTypes(desc); |
| 65 | + final Type returnType = Type.getReturnType(desc); |
| 66 | + MethodVisitor v = cv.visitMethod(access, |
| 67 | + name, |
| 68 | + desc, |
| 69 | + signature, |
| 70 | + exceptions); |
| 71 | + return new MethodAdapter(v) { |
| 72 | + |
| 73 | + private final ArrayList myNotNullParams = new ArrayList(); |
| 74 | + private int mySyntheticCount = 0; |
| 75 | + private boolean myIsNotNull = false; |
| 76 | + //private boolean myIsUnmodifiable = false; |
| 77 | + public Label myThrowLabel; |
| 78 | + //public Label myWrapLabel; |
| 79 | + private Label myStartGeneratedCodeLabel; |
| 80 | + |
| 81 | + public AnnotationVisitor visitParameterAnnotation( |
| 82 | + final int parameter, |
| 83 | + final String anno, |
| 84 | + final boolean visible) { |
| 85 | + AnnotationVisitor av; |
| 86 | + av = mv.visitParameterAnnotation(parameter, |
| 87 | + anno, |
| 88 | + visible); |
| 89 | + if (isReferenceType(args[parameter]) && anno.equals(NOT_NULL_ANNO)) { |
| 90 | + myNotNullParams.add(new Integer(parameter)); |
| 91 | + } else if (anno.equals("Ljava/lang/Synthetic;")) { |
| 92 | + // See asm r1278 for what we do this, |
| 93 | + // http://forge.objectweb.org/tracker/index.php?func=detail&aid=307392&group_id=23&atid=100023 |
| 94 | + mySyntheticCount++; |
| 95 | + } |
| 96 | + return av; |
| 97 | + } |
| 98 | + |
| 99 | + public AnnotationVisitor visitAnnotation(String anno, |
| 100 | + boolean isRuntime) { |
| 101 | + final AnnotationVisitor av = mv.visitAnnotation(anno, isRuntime); |
| 102 | + if (isReferenceType(returnType) && |
| 103 | + anno.equals(NOT_NULL_ANNO)) { |
| 104 | + myIsNotNull = true; |
| 105 | + } |
| 106 | + |
| 107 | + return av; |
| 108 | + } |
| 109 | + |
| 110 | + public void visitCode() { |
| 111 | + if (myNotNullParams.size() > 0) { |
| 112 | + myStartGeneratedCodeLabel = new Label(); |
| 113 | + mv.visitLabel(myStartGeneratedCodeLabel); |
| 114 | + } |
| 115 | + for (int p = 0; p < myNotNullParams.size(); ++p) { |
| 116 | + int var = ((access & ACC_STATIC) == 0) ? 1 : 0; |
| 117 | + int param = ((Integer) myNotNullParams.get(p)).intValue(); |
| 118 | + for (int i = 0; i < param; ++i) { |
| 119 | + var += args[i].getSize(); |
| 120 | + } |
| 121 | + mv.visitVarInsn(ALOAD, var); |
| 122 | + |
| 123 | + Label end = new Label(); |
| 124 | + mv.visitJumpInsn(IFNONNULL, end); |
| 125 | + |
| 126 | + generateThrow(IAE_CLASS_NAME, |
| 127 | + "Argument " + (param - mySyntheticCount) + " for @NotNull parameter of " + myClassName + "." + name + " must not be null", end); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public void visitLocalVariable(final String name, final String desc, final String signature, final Label start, final Label end, |
| 132 | + final int index) { |
| 133 | + final boolean isStatic = (access & ACC_STATIC) != 0; |
| 134 | + final boolean isParameter = isStatic ? index < args.length : index <= args.length; |
| 135 | + mv.visitLocalVariable(name, desc, signature, (isParameter && myStartGeneratedCodeLabel != null) ? myStartGeneratedCodeLabel : start, end, index); |
| 136 | + } |
| 137 | + |
| 138 | + public void visitInsn(int opcode) { |
| 139 | + if (opcode == ARETURN) { |
| 140 | + if (myIsNotNull) { |
| 141 | + mv.visitInsn(DUP); |
| 142 | + /*generateConditionalThrow("@NotNull method " + myClassName + "." + name + " must not return null", |
| 143 | + "java/lang/IllegalStateException");*/ |
| 144 | + if (myThrowLabel == null) { |
| 145 | + Label skipLabel = new Label(); |
| 146 | + mv.visitJumpInsn(IFNONNULL, skipLabel); |
| 147 | + myThrowLabel = new Label(); |
| 148 | + mv.visitLabel(myThrowLabel); |
| 149 | + generateThrow(ISE_CLASS_NAME, "@NotNull method " + myClassName + "." + name + " must not return null", |
| 150 | + skipLabel); |
| 151 | + } else { |
| 152 | + mv.visitJumpInsn(IFNULL, myThrowLabel); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + mv.visitInsn(opcode); |
| 158 | + } |
| 159 | + |
| 160 | + private void generateThrow(final String exceptionClass, final String descr, final Label end) { |
| 161 | + String exceptionParamClass = "(Ljava/lang/String;)V"; |
| 162 | + mv.visitTypeInsn(NEW, exceptionClass); |
| 163 | + mv.visitInsn(DUP); |
| 164 | + mv.visitLdcInsn(descr); |
| 165 | + mv.visitMethodInsn(INVOKESPECIAL, |
| 166 | + exceptionClass, |
| 167 | + CONSTRUCTOR_NAME, |
| 168 | + exceptionParamClass); |
| 169 | + mv.visitInsn(ATHROW); |
| 170 | + mv.visitLabel(end); |
| 171 | + |
| 172 | + myIsModification = true; |
| 173 | + } |
| 174 | + |
| 175 | + public void visitMaxs(final int maxStack, final int maxLocals) { |
| 176 | + try { |
| 177 | + super.visitMaxs(maxStack, maxLocals); |
| 178 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 179 | + throw new ArrayIndexOutOfBoundsException("maxs processing failed for method " + name + ": " + e.getMessage()); |
| 180 | + } |
| 181 | + } |
| 182 | + }; |
| 183 | + } |
| 184 | + |
| 185 | + private static boolean isReferenceType(final Type type) { |
| 186 | + return type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY; |
| 187 | + } |
| 188 | +} |
| 189 | + |
0 commit comments