001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018package org.apache.bcel.verifier; 019 020import java.util.ArrayList; 021import java.util.List; 022 023import org.apache.commons.lang3.ArrayUtils; 024 025/** 026 * A PassVerifier actually verifies a class file; it is instantiated by a Verifier. The verification should conform with 027 * a certain pass as described in The Java Virtual Machine Specification, 2nd edition. This book describes four passes. 028 * Pass one means loading the class and verifying a few static constraints. Pass two actually verifies some other 029 * constraints that could enforce loading in referenced class files. Pass three is the first pass that actually checks 030 * constraints in the code array of a method in the class file; it has two parts with the first verifying static 031 * constraints and the second part verifying structural constraints (where a data flow analysis is used for). The fourth 032 * pass, finally, performs checks that can only be done at run-time. JustIce does not have a run-time pass, but certain 033 * constraints that are usually delayed until run-time for performance reasons are also checked during the second part 034 * of pass three. PassVerifier instances perform caching. That means, if you really want a new verification run of a 035 * certain pass you must use a new instance of a given PassVerifier. 036 * 037 * @see Verifier 038 * @see #verify() 039 */ 040public abstract class PassVerifier { 041 042 /** The (warning) messages. */ 043 private final List<String> messages = new ArrayList<>(); 044 /** The VerificationResult cache. */ 045 private VerificationResult verificationResult; 046 047 /** 048 * This method adds a (warning) message to the message pool of this PassVerifier. This method is normally only 049 * internally used by BCEL's class file verifier "JustIce" and should not be used from the outside. 050 * 051 * @see #getMessages() 052 */ 053 public void addMessage(final String message) { 054 messages.add(message); 055 } 056 057 /** Does the real verification work, uncached. */ 058 public abstract VerificationResult do_verify(); 059 060 /** 061 * Returns the (warning) messages that this PassVerifier accumulated during its do_verify()ing work. 062 * 063 * @see #addMessage(String) 064 * @see #do_verify() 065 */ 066 public String[] getMessages() { 067 verify(); // create messages if not already done (cached!) 068 return messages.toArray(ArrayUtils.EMPTY_STRING_ARRAY); 069 } 070 071 /** 072 * This method runs a verification pass conforming to the Java Virtual Machine Specification, 2nd edition, on a class 073 * file. PassVerifier instances perform caching; i.e. if the verify() method once determined a VerificationResult, then 074 * this result may be returned after every invocation of this method instead of running the verification pass anew; 075 * likewise with the result of getMessages(). 076 * 077 * @see #getMessages() 078 * @see #addMessage(String) 079 */ 080 public VerificationResult verify() { 081 if (verificationResult == null) { 082 verificationResult = do_verify(); 083 } 084 return verificationResult; 085 } 086}