API for testing whether a class is model or not
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 03 May 2013 09:26:03 +0200
changeset 597fae5a91c9c7
parent 58 1aed561982c4
child 60 939453361b2d
API for testing whether a class is model or not
json/src/main/java/net/java/html/json/Models.java
json/src/main/java/org/apidesign/html/json/impl/JSON.java
json/src/test/java/net/java/html/json/ModelsTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/json/src/main/java/net/java/html/json/Models.java	Fri May 03 09:26:03 2013 +0200
     1.3 @@ -0,0 +1,46 @@
     1.4 +/**
     1.5 + * HTML via Java(tm) Language Bindings
     1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details. apidesign.org
    1.16 + * designates this particular file as subject to the
    1.17 + * "Classpath" exception as provided by apidesign.org
    1.18 + * in the License file that accompanied this code.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License
    1.21 + * along with this program. Look for COPYING file in the top folder.
    1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    1.23 + */
    1.24 +package net.java.html.json;
    1.25 +
    1.26 +import java.lang.reflect.Method;
    1.27 +import org.apidesign.html.json.impl.JSON;
    1.28 +
    1.29 +/** Information about and 
    1.30 + * operations for classes generated by the {@link Model @Model}
    1.31 + * annotation.
    1.32 + *
    1.33 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    1.34 + */
    1.35 +public final class Models {
    1.36 +    private Models() {
    1.37 +    }
    1.38 +   
    1.39 +    /** Finds out whether given class is a model class - e.g. has been
    1.40 +     * generated by {@link Model @Model} annotation.
    1.41 +     * 
    1.42 +     * @param clazz the class 
    1.43 +     * @return 
    1.44 +     * @since 0.2
    1.45 +     */
    1.46 +    public static boolean isModel(Class<?> clazz) {
    1.47 +        return JSON.isModel(clazz);
    1.48 +    }
    1.49 +}
     2.1 --- a/json/src/main/java/org/apidesign/html/json/impl/JSON.java	Thu May 02 21:22:44 2013 +0200
     2.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/JSON.java	Fri May 03 09:26:03 2013 +0200
     2.3 @@ -106,6 +106,18 @@
     2.4          froms.put(from.factoryFor(), from);
     2.5      }
     2.6      
     2.7 +    public static boolean isModel(Class<?> clazz) {
     2.8 +        for (int i = 0; i < 2; i++) {
     2.9 +            FromJSON<?> from = froms.get(clazz);
    2.10 +            if (from == null) {
    2.11 +                initClass(clazz);
    2.12 +            } else {
    2.13 +                return true;
    2.14 +            }
    2.15 +        }
    2.16 +        return false;
    2.17 +    }
    2.18 +    
    2.19      public static <T> T read(Context c, Class<T> modelClazz, Object data) {
    2.20          for (int i = 0; i < 2; i++) {
    2.21              FromJSON<?> from = froms.get(modelClazz);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/json/src/test/java/net/java/html/json/ModelsTest.java	Fri May 03 09:26:03 2013 +0200
     3.3 @@ -0,0 +1,50 @@
     3.4 +/**
     3.5 + * HTML via Java(tm) Language Bindings
     3.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details. apidesign.org
    3.16 + * designates this particular file as subject to the
    3.17 + * "Classpath" exception as provided by apidesign.org
    3.18 + * in the License file that accompanied this code.
    3.19 + *
    3.20 + * You should have received a copy of the GNU General Public License
    3.21 + * along with this program. Look for COPYING file in the top folder.
    3.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    3.23 + */
    3.24 +package net.java.html.json;
    3.25 +
    3.26 +import static org.testng.Assert.*;
    3.27 +import org.testng.annotations.Test;
    3.28 +
    3.29 +/**
    3.30 + *
    3.31 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    3.32 + */
    3.33 +public class ModelsTest {
    3.34 +    
    3.35 +    public ModelsTest() {
    3.36 +    }
    3.37 +
    3.38 +    @Test public void peopleAreModel() {
    3.39 +        assertTrue(Models.isModel(People.class), "People are generated class");
    3.40 +    }
    3.41 +    
    3.42 +    @Test public void personIsModel() {
    3.43 +        assertTrue(Models.isModel(Person.class), "Person is generated class");
    3.44 +    }
    3.45 +
    3.46 +    @Test public void implClassIsNotModel() {
    3.47 +        assertFalse(Models.isModel(PersonImpl.class), "Impl is not model");
    3.48 +    }
    3.49 +
    3.50 +    @Test public void randomClassIsNotModel() {
    3.51 +        assertFalse(Models.isModel(StringBuilder.class), "JDK classes are not model");
    3.52 +    }
    3.53 +}
    3.54 \ No newline at end of file