2 Replies Latest reply on Jul 4, 2014 5:27 AM by danielcunha

    [forge-dev] [Roaster] Type<?> instanceof

    danielcunha

      Hi folks,

       

      I want do that:

       

      type instanceof Number.

       

      That case, my type can be AtomicInteger, AtomicLong, BigDecimal,

      BigInteger, Byte, Double, Float, Integer, Long and Short

       

      Eg:.

       

      public static Type<?> getIdType(JavaClass<?> entity)

         {

            for (Member<?> member : entity.getMembers())

            {

               if (member.hasAnnotation(Id.class))

               {

                  if (member instanceof Method)

                  {

                     return ((Method<?, ?>) member).getReturnType();

                  }

                  if (member instanceof Field)

                  {

                     return ((Field<?>) member).getType();

                  }

               }

            }

            return null;

         }

       

          public static boolean isNumberIdType(JavaClass<?> entity)

         {

            return getIdType(entity) instanceof Number;

         }

       

      I see the code and I don’t see a simple form to do it.

      How I do that with Roaster Type<?>?

       

      I thought to do that in Types (roaster util):

       

         public static boolean isNumber(final String type)

         {

            if (isBasicType(type))

            {

               if (isPrimitive(type))

               {

                  return Arrays.asList("int", "long", "float", "double",

      "short").contains(type);

               }

               return Arrays.asList("Boolean", "Byte", "Double", "Float",

      "Integer", "Long", "Short").contains(

                        type);

            }

            return false;

         }

       

      But, it isn’t good.

      --

      Daniel Cunha (soro) <http://www.cejug.net>

      Blog: http://www.danielsoro.com.br

      Twitter: https://twitter.com/dvlc_

      GitHub: https://github.com/danielsoro

      LinkedIn:  http://www.linkedin.com/in/danielvlcunha

       

        • 1. Re: [forge-dev] [Roaster] Type<?> instanceof
          gastaldi

          Hi Daniel,

           

          You could try to load the class using: Class.forName and then attempt to check if the class isAssignableFrom(Number.class)

           

          This will not work if the class does not belong to the current classloader, but I am not sure if you're going to run into this issue specifically.

           

          Em 03/07/2014, às 21:46, Daniel Cunha <danielsoro@gmail.com> escreveu:

           

          Hi folks,

           

          I want do that:

           

          type instanceof Number.

          That case, my type can be AtomicInteger, AtomicLong, BigDecimal, BigInteger, Byte, Double, Float, Integer, Long and Short

           

          Eg:.

           

          public static Type<?> getIdType(JavaClass<?> entity)

             {

                for (Member<?> member : entity.getMembers())

                {

                   if (member.hasAnnotation(Id.class))

                   {

                      if (member instanceof Method)

                      {

                         return ((Method<?, ?>) member).getReturnType();

                      }

                      if (member instanceof Field)

                      {

                         return ((Field<?>) member).getType();

                      }

                   }

                }

                return null;

             }

              public static boolean isNumberIdType(JavaClass<?> entity)

             {

                return getIdType(entity) instanceof Number;

             }

          I see the code and I don’t see a simple form to do it.

          How I do that with Roaster Type<?>?

           

          I thought to do that in Types (roaster util):

           

             public static boolean isNumber(final String type)

             {

                if (isBasicType(type))

                {

                   if (isPrimitive(type))

                   {

                      return Arrays.asList("int", "long", "float", "double", "short").contains(type);

                   }

                   return Arrays.asList("Boolean", "Byte", "Double", "Float", "Integer", "Long", "Short").contains(

                            type);

                }

                return false;

             }

          But, it isn’t good.

           

          --

          Daniel Cunha (soro) <http://www.cejug.net>

          Blog: http://www.danielsoro.com.br

          Twitter: https://twitter.com/dvlc_

          GitHub: https://github.com/danielsoro

          LinkedIn:  http://www.linkedin.com/in/danielvlcunha

          _______________________________________________

          forge-dev mailing list

          forge-dev@lists.jboss.org

          https://lists.jboss.org/mailman/listinfo/forge-dev

           

          • 2. Re: [forge-dev] [Roaster] Type<?> instanceof
            danielcunha

            Hmmm,

             

            Ok. Thank you very much George.

             

             

            On Fri, Jul 4, 2014 at 12:06 AM, George Gastaldi <ggastald@redhat.com>

            wrote:

             

            Hi Daniel,

             

            You could try to load the class using: Class.forName and then attempt to

            check if the class isAssignableFrom(Number.class)

             

            This will not work if the class does not belong to the current

            classloader, but I am not sure if you're going to run into this issue

            specifically.

             

            Em 03/07/2014, às 21:46, Daniel Cunha <danielsoro@gmail.com> escreveu:

             

            Hi folks,

             

            I want do that:

             

            type instanceof Number.

             

            That case, my type can be AtomicInteger, AtomicLong, BigDecimal,

            BigInteger, Byte, Double, Float, Integer, Long and Short

             

            Eg:.

             

            public static Type<?> getIdType(JavaClass<?> entity)

               {

                  for (Member<?> member : entity.getMembers())

                  {

                     if (member.hasAnnotation(Id.class))

                     {

                        if (member instanceof Method)

                        {

                           return ((Method<?, ?>) member).getReturnType();

                        }

                        if (member instanceof Field)

                        {

                           return ((Field<?>) member).getType();

                        }

                     }

                  }

                  return null;

               }

             

                public static boolean isNumberIdType(JavaClass<?> entity)

               {

                  return getIdType(entity) instanceof Number;

               }

             

            I see the code and I don’t see a simple form to do it.

            How I do that with Roaster Type<?>?

             

            I thought to do that in Types (roaster util):

             

               public static boolean isNumber(final String type)

               {

                  if (isBasicType(type))

                  {

                     if (isPrimitive(type))

                     {

                        return Arrays.asList("int", "long", "float", "double", "short").contains(type);

                     }

                     return Arrays.asList("Boolean", "Byte", "Double", "Float", "Integer", "Long", "Short").contains(

                              type);

                  }

                  return false;

               }

             

            But, it isn’t good.

            --

            Daniel Cunha (soro) <http://www.cejug.net>

            Blog: http://www.danielsoro.com.br

            Twitter: https://twitter.com/dvlc_

            GitHub: https://github.com/danielsoro

            LinkedIn:  http://www.linkedin.com/in/danielvlcunha

             

            _______________________________________________

            forge-dev mailing list

            forge-dev@lists.jboss.org

            https://lists.jboss.org/mailman/listinfo/forge-dev

             

            >

            _______________________________________________

            forge-dev mailing list

            forge-dev@lists.jboss.org

            https://lists.jboss.org/mailman/listinfo/forge-dev

             

             

             

             

            --

            Daniel Cunha (soro) <http://www.cejug.net>

            Blog: http://www.danielsoro.com.br

            Twitter: https://twitter.com/dvlc_

            GitHub: https://github.com/danielsoro

            LinkedIn:  http://www.linkedin.com/in/danielvlcunha