You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@isis.apache.org by Mohammad Nour El-Din <mn...@apache.org> on 2011/08/10 15:15:14 UTC

Re: svn commit: r1156165 - in /incubator/isis/trunk/framework/core/progmodel/src: main/java/org/apache/isis/core/progmodel/facets/ main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/ test/java/org/apache/isis/core/progmodel/facet

Woohoo my 1st commit in Isis :D, I am so happy about that :D.

On Wed, Aug 10, 2011 at 3:13 PM,  <mn...@apache.org> wrote:
> Author: mnour
> Date: Wed Aug 10 13:13:25 2011
> New Revision: 1156165
>
> URL: http://svn.apache.org/viewvc?rev=1156165&view=rev
> Log:
> [ISIS-111] - Title Facet Via Method Annotation: * Implemented code. * Implemented FacetFactory unit tests.
>
> Added:
>    incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/
>    incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/Title.java
>    incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/TitleAnnotationFacetFactory.java
>    incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/TitleFacetViaTitleAnnotation.java
>    incubator/isis/trunk/framework/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/ident/title/annotation/
>    incubator/isis/trunk/framework/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/ident/title/annotation/TitleAnnotationFacetFactoryTest.java
> Modified:
>    incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/MethodFinderUtils.java
>
> Modified: incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/MethodFinderUtils.java
> URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/MethodFinderUtils.java?rev=1156165&r1=1156164&r2=1156165&view=diff
> ==============================================================================
> --- incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/MethodFinderUtils.java (original)
> +++ incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/MethodFinderUtils.java Wed Aug 10 13:13:25 2011
> @@ -16,8 +16,11 @@
>  */
>  package org.apache.isis.core.progmodel.facets;
>
> +import java.lang.annotation.Annotation;
>  import java.lang.reflect.Method;
>  import java.lang.reflect.Modifier;
> +import java.util.ArrayList;
> +import java.util.List;
>
>  import org.apache.isis.core.metamodel.facetapi.MethodRemover;
>  import org.apache.isis.core.metamodel.methodutils.MethodScope;
> @@ -134,6 +137,30 @@ public final class MethodFinderUtils {
>         return null;
>     }
>
> +    public static List<Method> findMethodsWithAnnotation(final Class<?> type, final MethodScope methodScope,
> +               Class<? extends Annotation> annotationClass) {
> +
> +       List<Method> methods = new ArrayList<Method>();
> +
> +       // Validate arguments
> +       if ( (type == null) || (methodScope == null) || (annotationClass == null)) {
> +               throw new IllegalArgumentException("One or more arguments are 'null' valued");
> +       }
> +
> +       // Find methods annotated with the specified annotation
> +       for (Method method : type.getMethods()) {
> +            if (!methodScope.matchesScopeOf(method)) {
> +                continue;
> +            }
> +
> +               if (method.isAnnotationPresent(annotationClass)) {
> +                       methods.add(method);
> +               }
> +       }
> +
> +       return methods;
> +    }
> +
>     public static void removeMethod(final MethodRemover methodRemover, final Method method) {
>         if (methodRemover != null && method != null) {
>             methodRemover.removeMethod(method);
>
> Added: incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/Title.java
> URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/Title.java?rev=1156165&view=auto
> ==============================================================================
> --- incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/Title.java (added)
> +++ incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/Title.java Wed Aug 10 13:13:25 2011
> @@ -0,0 +1,33 @@
> +/*
> + *  Licensed to the Apache Software Foundation (ASF) under one
> + *  or more contributor license agreements.  See the NOTICE file
> + *  distributed with this work for additional information
> + *  regarding copyright ownership.  The ASF licenses this file
> + *  to you under the Apache License, Version 2.0 (the
> + *  "License"); you may not use this file except in compliance
> + *  with the License.  You may obtain a copy of the License at
> + *
> + *        http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing,
> + *  software distributed under the License is distributed on an
> + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> + *  KIND, either express or implied.  See the License for the
> + *  specific language governing permissions and limitations
> + *  under the License.
> + */
> +
> +package org.apache.isis.core.progmodel.facets.object.title.annotation;
> +
> +import java.lang.annotation.ElementType;
> +import java.lang.annotation.Retention;
> +import java.lang.annotation.RetentionPolicy;
> +import java.lang.annotation.Target;
> +
> +/**
> + * A title annotation used to annotate methods used to construct the title of a domain object instance.
> + * It is used as a marker.
> + */
> +@Retention(RetentionPolicy.RUNTIME)
> +@Target(ElementType.METHOD)
> +public @interface Title {}
>
> Added: incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/TitleAnnotationFacetFactory.java
> URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/TitleAnnotationFacetFactory.java?rev=1156165&view=auto
> ==============================================================================
> --- incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/TitleAnnotationFacetFactory.java (added)
> +++ incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/TitleAnnotationFacetFactory.java Wed Aug 10 13:13:25 2011
> @@ -0,0 +1,58 @@
> +/*
> + *  Licensed to the Apache Software Foundation (ASF) under one
> + *  or more contributor license agreements.  See the NOTICE file
> + *  distributed with this work for additional information
> + *  regarding copyright ownership.  The ASF licenses this file
> + *  to you under the Apache License, Version 2.0 (the
> + *  "License"); you may not use this file except in compliance
> + *  with the License.  You may obtain a copy of the License at
> + *
> + *        http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing,
> + *  software distributed under the License is distributed on an
> + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> + *  KIND, either express or implied.  See the License for the
> + *  specific language governing permissions and limitations
> + *  under the License.
> + */
> +
> +package org.apache.isis.core.progmodel.facets.object.title.annotation;
> +
> +import java.lang.reflect.Method;
> +import java.util.List;
> +
> +import org.apache.isis.core.metamodel.facetapi.FacetHolder;
> +import org.apache.isis.core.metamodel.facetapi.FacetUtil;
> +import org.apache.isis.core.metamodel.facetapi.FeatureType;
> +import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
> +import org.apache.isis.core.metamodel.methodutils.MethodScope;
> +import org.apache.isis.core.progmodel.facets.MethodFinderUtils;
> +import org.apache.isis.core.progmodel.facets.fallback.FallbackFacetFactory;
> +
> +public class TitleAnnotationFacetFactory extends FacetFactoryAbstract {
> +
> +    public TitleAnnotationFacetFactory() {
> +        super(FeatureType.OBJECTS_ONLY);
> +    }
> +
> +    /**
> +     * If no method tagged with {@link Title} annotation then will use Facets provided by {@link FallbackFacetFactory} instead.
> +     */
> +    @Override
> +    public void process(final ProcessClassContext processClassContext) {
> +        final Class<?> cls = processClassContext.getCls();
> +        final FacetHolder facetHolder = processClassContext.getFacetHolder();
> +
> +        // TODO - MNour: Add method in MethodFinderUtils to find methods with a specified annotation.
> +        List<Method> methods = MethodFinderUtils.findMethodsWithAnnotation(cls, MethodScope.OBJECT, Title.class);
> +
> +        if (!methods.isEmpty()) {
> +               for (Method method : methods) {
> +                   processClassContext.removeMethod(method);
> +               }
> +               FacetUtil.addFacet(new TitleFacetViaTitleAnnotation(methods, facetHolder));
> +               return;
> +        }
> +    }
> +}
>
> Added: incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/TitleFacetViaTitleAnnotation.java
> URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/TitleFacetViaTitleAnnotation.java?rev=1156165&view=auto
> ==============================================================================
> --- incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/TitleFacetViaTitleAnnotation.java (added)
> +++ incubator/isis/trunk/framework/core/progmodel/src/main/java/org/apache/isis/core/progmodel/facets/object/title/annotation/TitleFacetViaTitleAnnotation.java Wed Aug 10 13:13:25 2011
> @@ -0,0 +1,78 @@
> +/*
> + *  Licensed to the Apache Software Foundation (ASF) under one
> + *  or more contributor license agreements.  See the NOTICE file
> + *  distributed with this work for additional information
> + *  regarding copyright ownership.  The ASF licenses this file
> + *  to you under the Apache License, Version 2.0 (the
> + *  "License"); you may not use this file except in compliance
> + *  with the License.  You may obtain a copy of the License at
> + *
> + *        http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing,
> + *  software distributed under the License is distributed on an
> + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> + *  KIND, either express or implied.  See the License for the
> + *  specific language governing permissions and limitations
> + *  under the License.
> + */
> +
> +package org.apache.isis.core.progmodel.facets.object.title.annotation;
> +
> +import java.lang.reflect.Method;
> +import java.util.Collections;
> +import java.util.List;
> +
> +import org.apache.isis.applib.profiles.Localization;
> +import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
> +import org.apache.isis.core.metamodel.adapter.util.AdapterInvokeUtils;
> +import org.apache.isis.core.metamodel.facetapi.FacetHolder;
> +import org.apache.isis.core.metamodel.facets.ImperativeFacet;
> +import org.apache.isis.core.metamodel.facets.object.title.TitleFacetAbstract;
> +import org.apache.log4j.Logger;
> +
> +public class TitleFacetViaTitleAnnotation extends TitleFacetAbstract implements ImperativeFacet {
> +
> +    private static final Logger LOG = Logger.getLogger(TitleFacetViaTitleAnnotation.class);
> +    private final List<Method> methods;
> +
> +    public TitleFacetViaTitleAnnotation(final List<Method> methods, final FacetHolder holder) {
> +        super(holder);
> +        this.methods = methods;
> +    }
> +
> +    /**
> +     * Returns a singleton list of the {@link Method}(s) provided in the constructor.
> +     */
> +    @Override
> +    public List<Method> getMethods() {
> +        return Collections.unmodifiableList(methods);
> +    }
> +
> +    @Override
> +    public boolean impliesResolve() {
> +        return true;
> +    }
> +
> +    @Override
> +    public boolean impliesObjectChanged() {
> +        return false;
> +    }
> +
> +    @Override
> +    public String title(final ObjectAdapter owningAdapter, final Localization localization) {
> +       StringBuilder stringBuilder = new StringBuilder();
> +
> +        try {
> +               for (Method method : this.methods) {
> +                       stringBuilder.append((String) AdapterInvokeUtils.invoke(method, owningAdapter)).append(' ');
> +               }
> +
> +               return stringBuilder.toString().trim();
> +        } catch (final RuntimeException ex) {
> +            LOG.warn("Title failure", ex);
> +            return "Failed Title";
> +        }
> +    }
> +
> +}
>
> Added: incubator/isis/trunk/framework/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/ident/title/annotation/TitleAnnotationFacetFactoryTest.java
> URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/ident/title/annotation/TitleAnnotationFacetFactoryTest.java?rev=1156165&view=auto
> ==============================================================================
> --- incubator/isis/trunk/framework/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/ident/title/annotation/TitleAnnotationFacetFactoryTest.java (added)
> +++ incubator/isis/trunk/framework/core/progmodel/src/test/java/org/apache/isis/core/progmodel/facets/object/ident/title/annotation/TitleAnnotationFacetFactoryTest.java Wed Aug 10 13:13:25 2011
> @@ -0,0 +1,120 @@
> +/*
> + *  Licensed to the Apache Software Foundation (ASF) under one
> + *  or more contributor license agreements.  See the NOTICE file
> + *  distributed with this work for additional information
> + *  regarding copyright ownership.  The ASF licenses this file
> + *  to you under the Apache License, Version 2.0 (the
> + *  "License"); you may not use this file except in compliance
> + *  with the License.  You may obtain a copy of the License at
> + *
> + *        http://www.apache.org/licenses/LICENSE-2.0
> + *
> + *  Unless required by applicable law or agreed to in writing,
> + *  software distributed under the License is distributed on an
> + *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
> + *  KIND, either express or implied.  See the License for the
> + *  specific language governing permissions and limitations
> + *  under the License.
> + */
> +
> +
> +package org.apache.isis.core.progmodel.facets.object.ident.title.annotation;
> +
> +import java.lang.reflect.Method;
> +import java.util.List;
> +
> +import org.apache.isis.core.metamodel.facetapi.Facet;
> +import org.apache.isis.core.metamodel.facets.FacetFactory.ProcessClassContext;
> +import org.apache.isis.core.metamodel.facets.object.title.TitleFacet;
> +import org.apache.isis.core.metamodel.methodutils.MethodScope;
> +import org.apache.isis.core.progmodel.facets.AbstractFacetFactoryTest;
> +import org.apache.isis.core.progmodel.facets.MethodFinderUtils;
> +import org.apache.isis.core.progmodel.facets.object.title.annotation.Title;
> +import org.apache.isis.core.progmodel.facets.object.title.annotation.TitleAnnotationFacetFactory;
> +import org.apache.isis.core.progmodel.facets.object.title.annotation.TitleFacetViaTitleAnnotation;
> +
> +
> +public class TitleAnnotationFacetFactoryTest extends AbstractFacetFactoryTest {
> +
> +    private TitleAnnotationFacetFactory facetFactory;
> +
> +    @Override
> +    protected void setUp() throws Exception {
> +        super.setUp();
> +
> +        facetFactory = new TitleAnnotationFacetFactory();
> +    }
> +
> +    @Override
> +    protected void tearDown() throws Exception {
> +        facetFactory = null;
> +        super.tearDown();
> +    }
> +
> +    public void testTitleAnnotatedMethodPickedUpOnClassAndMethodRemoved() {
> +        class Customer {
> +
> +            @SuppressWarnings("unused")
> +            @Title
> +            public String someTitle() {
> +                return "Some Title";
> +            }
> +
> +        }
> +
> +        titleAnnotatedMethodsPickedUpOnClassAndMethodsRemovedCommonTest(Customer.class);
> +    }
> +
> +    public void testTitleAnnotatedMethodsPickedUpOnClassAndMethodsRemoved() {
> +        class Customer {
> +
> +            @SuppressWarnings("unused")
> +            @Title
> +            public String titleElement1() {
> +                return "titleElement1";
> +            }
> +
> +            @SuppressWarnings("unused")
> +            @Title
> +            public String titleElement2() {
> +                return "titleElement2";
> +            }
> +
> +            @SuppressWarnings("unused")
> +            @Title
> +            public String titleElement3() {
> +                return "titleElement3";
> +            }
> +
> +        }
> +
> +        titleAnnotatedMethodsPickedUpOnClassAndMethodsRemovedCommonTest(Customer.class);
> +    }
> +
> +    public void testNoExplicitTitleAnnotations() {
> +        class Customer {}
> +
> +        facetFactory.process(new ProcessClassContext(Customer.class, methodRemover, facetedMethod));
> +
> +        assertNull(facetedMethod.getFacet(TitleFacet.class));
> +
> +        assertNoMethodsRemoved();
> +    }
> +
> +    protected void titleAnnotatedMethodsPickedUpOnClassAndMethodsRemovedCommonTest(Class<?> type) {
> +       assertNotNull("Type MUST not be 'null'", type);
> +
> +        final List<Method> titleMethods = MethodFinderUtils.findMethodsWithAnnotation(type, MethodScope.OBJECT, Title.class);
> +
> +        facetFactory.process(new ProcessClassContext(type, methodRemover, facetedMethod));
> +
> +        final Facet facet = facetedMethod.getFacet(TitleFacet.class);
> +        assertNotNull(facet);
> +        assertTrue(facet instanceof TitleFacetViaTitleAnnotation);
> +        final TitleFacetViaTitleAnnotation titleFacetViaTitleAnnotation = (TitleFacetViaTitleAnnotation) facet;
> +        assertEquals(titleMethods, titleFacetViaTitleAnnotation.getMethods());
> +
> +        assertTrue(methodRemover.getRemovedMethodMethodCalls().containsAll(titleMethods));
> +    }
> +
> +}
>
>
>



-- 
Thanks
- Mohammad Nour
  Author of (WebSphere Application Server Community Edition 2.0 User Guide)
  http://www.redbooks.ibm.com/abstracts/sg247585.html
- LinkedIn: http://www.linkedin.com/in/mnour
- Blog: http://tadabborat.blogspot.com
----
"Life is like riding a bicycle. To keep your balance you must keep moving"
- Albert Einstein

"Writing clean code is what you must do in order to call yourself a
professional. There is no reasonable excuse for doing anything less
than your best."
- Clean Code: A Handbook of Agile Software Craftsmanship

"Stay hungry, stay foolish."
- Steve Jobs