001    // Copyright 2010 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    // http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.internal.services;
016    
017    import org.apache.tapestry5.services.StackTraceElementAnalyzer;
018    import org.apache.tapestry5.services.StackTraceElementClassConstants;
019    
020    /**
021     * Encapsulates a number of tests for identifying stack frames that are a side-effect
022     * of various Tapestry Aspect Oriented Programming and other code generation behaviors.
023     * 
024     * @since 5.2.0
025     */
026    public class TapestryAOPStackFrameAnalyzer implements StackTraceElementAnalyzer
027    {
028        private static final String[] SYNTHETIC_METHOD_PREFIXES = new String[]
029        { "_$get_", "_$set_", "_$readaccess_", "_$writeaccess_" };
030    
031        public String classForFrame(StackTraceElement frame)
032        {
033            if (omit(frame))
034                return StackTraceElementClassConstants.OMITTED;
035    
036            return null;
037        }
038    
039        private boolean omit(StackTraceElement frame)
040        {
041            // $FieldAccess class in root package is generated
042    
043            if (frame.getClassName().startsWith("$FieldAccess_"))
044                return true;
045    
046            if (frame.getMethodName().equals("invoke") && frame.getClassName().contains("$MethodAccess_"))
047                return true;
048    
049            if (frame.getMethodName().equals("invokeAdvisedMethod") && frame.getClassName().contains("$invocation_"))
050                return true;
051    
052            for (String prefix : SYNTHETIC_METHOD_PREFIXES)
053            {
054                if (frame.getMethodName().startsWith(prefix))
055                    return true;
056            }
057    
058            return false;
059        }
060    
061    }