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