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 */ 017package org.apache.bcel.verifier; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.apache.commons.lang3.ArrayUtils; 023 024/** 025 * A PassVerifier actually verifies a class file; it is instantiated by a Verifier. The verification should conform with 026 * a certain pass as described in The Java Virtual Machine Specification, 2nd edition. This book describes four passes. 027 * Pass one means loading the class and verifying a few static constraints. Pass two actually verifies some other 028 * constraints that could enforce loading in referenced class files. Pass three is the first pass that actually checks 029 * constraints in the code array of a method in the class file; it has two parts with the first verifying static 030 * constraints and the second part verifying structural constraints (where a data flow analysis is used for). The fourth 031 * pass, finally, performs checks that can only be done at run-time. JustIce does not have a run-time pass, but certain 032 * constraints that are usually delayed until run-time for performance reasons are also checked during the second part 033 * of pass three. PassVerifier instances perform caching. That means, if you really want a new verification run of a 034 * certain pass you must use a new instance of a given PassVerifier. 035 * 036 * @see Verifier 037 * @see #verify() 038 */ 039public abstract class PassVerifier { 040 041 /** The (warning) messages. */ 042 private final List<String> messages = new ArrayList<>(); 043 /** The VerificationResult cache. */ 044 private VerificationResult verificationResult; 045 046 /** 047 * This method adds a (warning) message to the message pool of this PassVerifier. This method is normally only 048 * internally used by BCEL's class file verifier "JustIce" and should not be used from the outside. 049 * 050 * @param message message to be appended to the message list. 051 * @see #getMessages() 052 */ 053 public void addMessage(final String message) { 054 messages.add(message); 055 } 056 057 /** 058 * Verifies, not cached. 059 * 060 * @return The VerificationResult 061 */ 062 public abstract VerificationResult do_verify(); 063 064 /** 065 * Returns the (warning) messages that this PassVerifier accumulated during its do_verify()ing work. 066 * 067 * @return the (warning) messages. 068 * @see #addMessage(String) 069 * @see #do_verify() 070 */ 071 public String[] getMessages() { 072 return getMessagesList().toArray(ArrayUtils.EMPTY_STRING_ARRAY); 073 } 074 075 /** 076 * Returns the (warning) messages that this PassVerifier accumulated during its do_verify()ing work. 077 * 078 * @see #addMessage(String) 079 * @see #do_verify() 080 */ 081 public List<String> getMessagesList() { 082 verify(); // create messages if not already done (cached!) 083 return messages; 084 } 085 086 /** 087 * This method runs a verification pass conforming to the Java Virtual Machine Specification, 2nd edition, on a class 088 * file. PassVerifier instances perform caching; i.e. if the verify() method once determined a VerificationResult, then 089 * this result may be returned after every invocation of this method instead of running the verification pass anew; 090 * likewise with the result of getMessages(). 091 * 092 * @return a VerificationResult. 093 * @see #getMessages() 094 * @see #addMessage(String) 095 */ 096 public VerificationResult verify() { 097 if (verificationResult == null) { 098 verificationResult = do_verify(); 099 } 100 return verificationResult; 101 } 102}