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     *        http://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     */
019    
020    package org.apache.isis.core.progmodel.facets.object.ignore.javalang;
021    
022    import java.lang.reflect.Method;
023    import java.util.ArrayList;
024    
025    import org.apache.isis.core.commons.exceptions.IsisException;
026    import org.apache.isis.core.metamodel.facetapi.Facet;
027    import org.apache.isis.core.metamodel.facetapi.FeatureType;
028    import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
029    import org.apache.isis.core.metamodel.facets.MethodFilteringFacetFactory;
030    
031    /**
032     * Designed to simply filter out any synthetic methods.
033     * 
034     * <p>
035     * Does not add any {@link Facet}s.
036     */
037    public class SyntheticMethodFilteringFacetFactory extends FacetFactoryAbstract implements MethodFilteringFacetFactory {
038    
039        public SyntheticMethodFilteringFacetFactory() {
040            super(new ArrayList<FeatureType>());
041        }
042    
043        @Override
044        public boolean recognizes(final Method method) {
045            return isSynthetic(method);
046        }
047    
048        private boolean isSynthetic(final Method method) {
049            try {
050                final Class<?> type = method.getClass();
051                try {
052                    return ((Boolean) type.getMethod("isSynthetic", (Class[]) null).invoke(method, (Object[]) null))
053                        .booleanValue();
054                } catch (final NoSuchMethodException nsm) {
055                    // pre java 5
056                    return false;
057                }
058            } catch (final Exception e) {
059                throw new IsisException(e);
060            }
061        }
062    
063    }