< prev index next >

src/java.base/share/classes/jdk/internal/classfile/impl/verifier/VerificationWrapper.java

Print this page

 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 package jdk.internal.classfile.impl.verifier;
 27 
 28 import java.lang.classfile.Attributes;
 29 import java.lang.classfile.ClassModel;

 30 import java.lang.classfile.MethodModel;
 31 import java.lang.classfile.attribute.LocalVariableInfo;
 32 import java.lang.classfile.constantpool.ClassEntry;
 33 import java.lang.classfile.constantpool.ConstantPool;
 34 import java.lang.classfile.constantpool.DynamicConstantPoolEntry;
 35 import java.lang.classfile.constantpool.MemberRefEntry;
 36 import java.lang.classfile.constantpool.NameAndTypeEntry;
 37 import java.lang.constant.ClassDesc;
 38 import java.lang.reflect.AccessFlag;
 39 import java.util.LinkedList;
 40 import java.util.List;
 41 import java.util.stream.Collectors;
 42 
 43 import jdk.internal.classfile.impl.BoundAttribute;
 44 import jdk.internal.classfile.impl.CodeImpl;
 45 import jdk.internal.classfile.impl.Util;
 46 
 47 public final class VerificationWrapper {
 48     private final ClassModel clm;
 49     private final ConstantPoolWrapper cp;
 50 
 51     public VerificationWrapper(ClassModel clm) {
 52         this.clm = clm;
 53         this.cp = new ConstantPoolWrapper(clm.constantPool());
 54      }
 55 
 56     String thisClassName() {
 57         return clm.thisClass().asInternalName();
 58     }
 59 
 60     int majorVersion() {
 61         return clm.majorVersion();
 62     }
 63 
 64     String superclassName() {
 65         return clm.superclass().map(ClassEntry::asInternalName).orElse(null);
 66     }
 67 
 68     Iterable<String> interfaceNames() {
 69         return Util.mappedList(clm.interfaces(), ClassEntry::asInternalName);
 70     }
 71 
 72     Iterable<MethodWrapper> methods() {
 73         return clm.methods().stream().map(m -> new MethodWrapper(m)).toList();
 74     }
 75 
 76     boolean findField(String name, String sig) {
 77         for (var f : clm.fields())
 78             if (f.fieldName().stringValue().equals(name) && f.fieldType().stringValue().equals(sig))
 79                 return true;
 80         return false;
 81     }
 82 
 83     class MethodWrapper {
 84 
 85         final MethodModel m;
 86         private final CodeImpl c;
 87         private final List<int[]> exc;
 88 
 89         MethodWrapper(MethodModel m) {
 90             this.m = m;
 91             this.c = (CodeImpl)m.code().orElse(null);
 92             exc = new LinkedList<>();
 93             if (c != null) c.iterateExceptionHandlers((start, end, handler, catchType) -> {
 94                 exc.add(new int[] {start, end, handler, catchType});
 95             });
 96         }
 97 
 98         ConstantPoolWrapper constantPool() {
 99             return cp;
100         }

144         }
145 
146         List<int[]> exceptionTable() {
147             return exc;
148         }
149 
150         List<LocalVariableInfo> localVariableTable() {
151             var attro = c.findAttribute(Attributes.localVariableTable());
152             return attro.map(lvta -> lvta.localVariables()).orElse(List.of());
153         }
154 
155         byte[] stackMapTableRawData() {
156             var attro = c.findAttribute(Attributes.stackMapTable());
157             return attro.map(attr -> ((BoundAttribute) attr).contents()).orElse(null);
158         }
159 
160     }
161 
162     static class ConstantPoolWrapper {
163 
164         private final ConstantPool cp;
165 
166         ConstantPoolWrapper(ConstantPool cp) {
167             this.cp = cp;
168         }
169 
170         int entryCount() {
171             return cp.size();
172         }
173 
174         String classNameAt(int index) {
175             return cp.entryByIndex(index, ClassEntry.class).asInternalName();
176         }
177 
178         String dynamicConstantSignatureAt(int index) {
179             return cp.entryByIndex(index, DynamicConstantPoolEntry.class).type().stringValue();
180         }
181 
182         int tagAt(int index) {
183             return cp.entryByIndex(index).tag();
184         }
185 
186         private NameAndTypeEntry _refNameType(int index) {
187             var e = cp.entryByIndex(index);
188             return (e instanceof DynamicConstantPoolEntry de) ? de.nameAndType() :
189                     e != null ? ((MemberRefEntry)e).nameAndType() : null;
190         }
191 
192         String refNameAt(int index) {
193             return _refNameType(index).name().stringValue();
194         }
195 
196         String refSignatureAt(int index) {
197             return _refNameType(index).type().stringValue();
198         }
199 
200         int refClassIndexAt(int index) {
201             return cp.entryByIndex(index, MemberRefEntry.class).owner().index();
202         }




203     }
204 }

 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  *
 25  */
 26 package jdk.internal.classfile.impl.verifier;
 27 
 28 import java.lang.classfile.Attributes;
 29 import java.lang.classfile.ClassModel;
 30 import java.lang.classfile.FieldModel;
 31 import java.lang.classfile.MethodModel;
 32 import java.lang.classfile.attribute.LocalVariableInfo;
 33 import java.lang.classfile.constantpool.ClassEntry;
 34 import java.lang.classfile.constantpool.ConstantPool;
 35 import java.lang.classfile.constantpool.DynamicConstantPoolEntry;
 36 import java.lang.classfile.constantpool.MemberRefEntry;
 37 import java.lang.classfile.constantpool.NameAndTypeEntry;
 38 import java.lang.constant.ClassDesc;
 39 import java.lang.reflect.AccessFlag;
 40 import java.util.LinkedList;
 41 import java.util.List;
 42 import java.util.stream.Collectors;
 43 
 44 import jdk.internal.classfile.impl.BoundAttribute;
 45 import jdk.internal.classfile.impl.CodeImpl;
 46 import jdk.internal.classfile.impl.Util;
 47 
 48 public final class VerificationWrapper {
 49     final ClassModel clm;
 50     private final ConstantPoolWrapper cp;
 51 
 52     public VerificationWrapper(ClassModel clm) {
 53         this.clm = clm;
 54         this.cp = new ConstantPoolWrapper(clm.constantPool());
 55      }
 56 
 57     String thisClassName() {
 58         return clm.thisClass().asInternalName();
 59     }
 60 
 61     int majorVersion() {
 62         return clm.majorVersion();
 63     }
 64 
 65     String superclassName() {
 66         return clm.superclass().map(ClassEntry::asInternalName).orElse(null);
 67     }
 68 
 69     Iterable<String> interfaceNames() {
 70         return Util.mappedList(clm.interfaces(), ClassEntry::asInternalName);
 71     }
 72 
 73     Iterable<MethodWrapper> methods() {
 74         return clm.methods().stream().map(m -> new MethodWrapper(m)).toList();
 75     }
 76 
 77     FieldModel findField(String name, String sig) {
 78         for (var f : clm.fields())
 79             if (f.fieldName().stringValue().equals(name) && f.fieldType().stringValue().equals(sig))
 80                 return f;
 81         return null;
 82     }
 83 
 84     class MethodWrapper {
 85 
 86         final MethodModel m;
 87         private final CodeImpl c;
 88         private final List<int[]> exc;
 89 
 90         MethodWrapper(MethodModel m) {
 91             this.m = m;
 92             this.c = (CodeImpl)m.code().orElse(null);
 93             exc = new LinkedList<>();
 94             if (c != null) c.iterateExceptionHandlers((start, end, handler, catchType) -> {
 95                 exc.add(new int[] {start, end, handler, catchType});
 96             });
 97         }
 98 
 99         ConstantPoolWrapper constantPool() {
100             return cp;
101         }

145         }
146 
147         List<int[]> exceptionTable() {
148             return exc;
149         }
150 
151         List<LocalVariableInfo> localVariableTable() {
152             var attro = c.findAttribute(Attributes.localVariableTable());
153             return attro.map(lvta -> lvta.localVariables()).orElse(List.of());
154         }
155 
156         byte[] stackMapTableRawData() {
157             var attro = c.findAttribute(Attributes.stackMapTable());
158             return attro.map(attr -> ((BoundAttribute) attr).contents()).orElse(null);
159         }
160 
161     }
162 
163     static class ConstantPoolWrapper {
164 
165         final ConstantPool cp;
166 
167         ConstantPoolWrapper(ConstantPool cp) {
168             this.cp = cp;
169         }
170 
171         int entryCount() {
172             return cp.size();
173         }
174 
175         String classNameAt(int index) {
176             return cp.entryByIndex(index, ClassEntry.class).asInternalName();
177         }
178 
179         String dynamicConstantSignatureAt(int index) {
180             return cp.entryByIndex(index, DynamicConstantPoolEntry.class).type().stringValue();
181         }
182 
183         int tagAt(int index) {
184             return cp.entryByIndex(index).tag();
185         }
186 
187         private NameAndTypeEntry _refNameType(int index) {
188             var e = cp.entryByIndex(index);
189             return (e instanceof DynamicConstantPoolEntry de) ? de.nameAndType() :
190                     e != null ? ((MemberRefEntry)e).nameAndType() : null;
191         }
192 
193         String refNameAt(int index) {
194             return _refNameType(index).name().stringValue();
195         }
196 
197         String refSignatureAt(int index) {
198             return _refNameType(index).type().stringValue();
199         }
200 
201         int refClassIndexAt(int index) {
202             return cp.entryByIndex(index, MemberRefEntry.class).owner().index();
203         }
204 
205         boolean is_within_bounds(int i) {
206             return i >= 1 && i <= cp.size();
207         }
208     }
209 }
< prev index next >