1 /*
2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package vm.share.transform;
24 
25 import jdk.internal.org.objectweb.asm.AnnotationVisitor;
26 
27 public abstract class AnnotationAppender {
28     private final String desc;
29     private final boolean visible;
30     private boolean annotationPresent;
31 
AnnotationAppender(String desc, boolean visible)32     public AnnotationAppender(String desc, boolean visible) {
33         this.desc = desc;
34         this.visible = visible;
35     }
36 
checkAnnotation(String desc, boolean isVisible)37     public void checkAnnotation(String desc, boolean isVisible) {
38         annotationPresent |= visible == isVisible && this.desc.equals(desc);
39     }
40 
addAnnotation(VisitAnnotation func)41     public void addAnnotation(VisitAnnotation func) {
42         if (shouldAdd()) {
43             AnnotationVisitor av = func.visit(desc, true);
44             if (av != null) {
45                 postCreate(av);
46                 av.visitEnd();
47                 annotationPresent = true;
48             }
49         }
50     }
51 
shouldAdd()52     protected boolean shouldAdd() {
53         return !annotationPresent;
54     }
55 
postCreate(AnnotationVisitor av)56     protected abstract void postCreate(AnnotationVisitor av);
57 
58     @FunctionalInterface
59     public static interface VisitAnnotation {
visit(String desc, boolean visible)60         AnnotationVisitor visit(String desc, boolean visible);
61     }
62 }
63