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.validperspec;
021
022 import java.util.ArrayList;
023 import java.util.List;
024
025 import org.apache.isis.applib.annotation.MustSatisfy;
026 import org.apache.isis.applib.spec.Specification;
027 import org.apache.isis.core.metamodel.facetapi.Facet;
028 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
029 import org.apache.isis.core.metamodel.facetapi.FacetUtil;
030 import org.apache.isis.core.metamodel.facetapi.FeatureType;
031 import org.apache.isis.core.metamodel.facets.AnnotationBasedFacetFactoryAbstract;
032
033 public class MustSatisfySpecificationOnTypeFacetFactory extends AnnotationBasedFacetFactoryAbstract {
034
035 public MustSatisfySpecificationOnTypeFacetFactory() {
036 super(FeatureType.OBJECTS_ONLY);
037 }
038
039 @Override
040 public void process(final ProcessClassContext processClassContaxt) {
041 FacetUtil.addFacet(create(processClassContaxt.getCls(), processClassContaxt.getFacetHolder()));
042 }
043
044 private Facet create(final Class<?> clazz, final FacetHolder holder) {
045 return create(getAnnotation(clazz, MustSatisfy.class), holder);
046 }
047
048 private static Facet create(final MustSatisfy annotation, final FacetHolder holder) {
049 if (annotation == null) {
050 return null;
051 }
052 final Class<?>[] values = annotation.value();
053 final List<Specification> specifications = new ArrayList<Specification>();
054 for (final Class<?> value : values) {
055 final Specification specification = newSpecificationElseNull(value);
056 if (specification != null) {
057 specifications.add(specification);
058 }
059 }
060 return specifications.size() > 0 ? new MustSatisfySpecificationOnTypeFacet(specifications, holder) : null;
061 }
062
063 private static Specification newSpecificationElseNull(final Class<?> value) {
064 if (!(Specification.class.isAssignableFrom(value))) {
065 return null;
066 }
067 try {
068 return (Specification) value.newInstance();
069 } catch (final InstantiationException e) {
070 return null;
071 } catch (final IllegalAccessException e) {
072 return null;
073 }
074 }
075
076 }