lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sanjay Madnani (JIRA)" <j...@apache.org>
Subject [jira] [Created] (SOLR-9112) Null Pointer Exception if @Field (child = true) is annotated at getter which takes collection of child docs
Date Sun, 15 May 2016 20:47:12 GMT
Sanjay Madnani created SOLR-9112:
------------------------------------

             Summary: Null Pointer Exception if @Field (child = true) is annotated at getter
which takes collection of child docs
                 Key: SOLR-9112
                 URL: https://issues.apache.org/jira/browse/SOLR-9112
             Project: Solr
          Issue Type: Bug
          Components: clients - java
    Affects Versions: 6.0
            Reporter: Sanjay Madnani
            Priority: Minor


See: http://stackoverflow.com/questions/37241489/solrj-6-0-0-insertion-of-a-bean-object-which-associate-list-of-bean-object-is-g
for defect description.

Defect is caused by below piece of code if annotation is present at setter for list of child:

public DocField(AccessibleObject member) {
      if (member instanceof java.lang.reflect.Field) {
        field = (java.lang.reflect.Field) member; // It is null here
      } else {
        setter = (Method) member; // initialized as annotation is at setter
      }
      annotation = member.getAnnotation(Field.class);
      storeName(annotation);
      storeType(); // giving null pointer exception as field is null see below

      // Look for a matching getter
      if (setter != null) {
        String gname = setter.getName();
        if (gname.startsWith("set")) {
          gname = "get" + gname.substring(3);
          try {
            getter = setter.getDeclaringClass().getMethod(gname, (Class[]) null);
          } catch (Exception ex) {
            // no getter -- don't worry about it...
            if (type == Boolean.class) {
              gname = "is" + setter.getName().substring(3);
              try {
                getter = setter.getDeclaringClass().getMethod(gname, (Class[]) null);
              } catch(Exception ex2) {
                // no getter -- don't worry about it...
              }
            }
          }
        }
      }
    }

private void storeType() {
      if (field != null) {
        type = field.getType();
      } else {
        Class[] params = setter.getParameterTypes();
        if (params.length != 1) {
          throw new BindingException("Invalid setter method. Must have one and only one parameter");
        }
        type = params[0];
      }

      if (type == Collection.class || type == List.class || type == ArrayList.class) {
        isList = true;
        if (annotation.child()) {
          populateChild(field.getGenericType()); //giving null pointer exception //as field
is null here
        } else {
          type = Object.class;
        }
      } else if (type == byte[].class) {
        //no op
      } else if (type.isArray()) {
        isArray = true;
        if (annotation.child()) {
          populateChild(type.getComponentType());
        } else {
          type = type.getComponentType();
        }
      } else if (type == Map.class || type == HashMap.class) { //corresponding to the support
for dynamicFields
        if (annotation.child()) throw new BindingException("Map should is not a valid type
for a child document");
        isContainedInMap = true;
        //assigned a default type
        type = Object.class;
        if (field != null) {
          if (field.getGenericType() instanceof ParameterizedType) {
            //check what are the generic values
            ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
            Type[] types = parameterizedType.getActualTypeArguments();
            if (types != null && types.length == 2 && types[0] == String.class)
{
              //the key should always be String
              //Raw and primitive types
              if (types[1] instanceof Class) {
                //the value could be multivalued then it is a List, Collection, ArrayList
                if (types[1] == Collection.class || types[1] == List.class || types[1] ==
ArrayList.class) {
                  type = Object.class;
                  isList = true;
                } else {
                  //else assume it is a primitive and put in the source type itself
                  type = (Class) types[1];
                }
              } else if (types[1] instanceof ParameterizedType) { //Of all the Parameterized
types, only List is supported
                Type rawType = ((ParameterizedType) types[1]).getRawType();
                if (rawType == Collection.class || rawType == List.class || rawType == ArrayList.class)
{
                  type = Object.class;
                  isList = true;
                }
              } else if (types[1] instanceof GenericArrayType) { //Array types
                type = (Class) ((GenericArrayType) types[1]).getGenericComponentType();
                isArray = true;
              } else { //Throw an Exception if types are not known
                throw new BindingException("Allowed type for values of mapping a dynamicField
are : " +
                    "Object, Object[] and List");
              }
            }
          }
        }
      } else {
        if (annotation.child()) {
          populateChild(type);
        }
      }
    }




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org


Mime
View raw message