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 020package org.apache.isis.core.progmodel.facets.object.ignore.javalang; 021 022import java.lang.reflect.Method; 023import java.util.List; 024 025import com.google.common.collect.Lists; 026 027import org.apache.isis.core.commons.factory.InstanceUtil; 028import org.apache.isis.core.metamodel.facetapi.FeatureType; 029import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract; 030import org.apache.isis.core.metamodel.methodutils.MethodScope; 031 032/** 033 * Removes all methods inherited specified class. 034 */ 035public abstract class AbstractRemoveMethodsFacetFactory extends FacetFactoryAbstract { 036 037 private static class MethodAndParameterTypes { 038 private final String methodName; 039 private final Class<?>[] methodParameters; 040 041 public MethodAndParameterTypes(final String methodName, final Class<?>[] methodParameters) { 042 this.methodName = methodName; 043 this.methodParameters = methodParameters; 044 } 045 } 046 047 private final List<MethodAndParameterTypes> methodsToIgnore = Lists.newArrayList(); 048 049 public AbstractRemoveMethodsFacetFactory(final Class<?> typeToIgnore) { 050 super(FeatureType.OBJECTS_ONLY); 051 final Method[] methods = typeToIgnore.getMethods(); 052 for (final Method method : methods) { 053 methodsToIgnore.add(new MethodAndParameterTypes(method.getName(), method.getParameterTypes())); 054 } 055 } 056 057 public AbstractRemoveMethodsFacetFactory(final String typeToIgnoreIfOnClasspath) { 058 super(FeatureType.OBJECTS_ONLY); 059 try { 060 Class<?> typeToIgnore = InstanceUtil.loadClass(typeToIgnoreIfOnClasspath); 061 addMethodsToBeIgnored(typeToIgnore); 062 } catch(Exception ex) { 063 // ignore 064 } 065 } 066 067 private void addMethodsToBeIgnored(Class<?> typeToIgnore) { 068 final Method[] methods = typeToIgnore.getMethods(); 069 for (final Method method : methods) { 070 methodsToIgnore.add(new MethodAndParameterTypes(method.getName(), method.getParameterTypes())); 071 } 072 } 073 074 @Override 075 public void process(final ProcessClassContext processClassContext) { 076 for (final MethodAndParameterTypes mapt : methodsToIgnore) { 077 processClassContext.removeMethod(MethodScope.OBJECT, mapt.methodName, null, mapt.methodParameters); 078 } 079 } 080 081}