3
I have the following class hierarchy
(POSTRequestHTTP extends ReqeustHTTP) 
(RequestHTTP extends ComunicationObjectHTTP )
Assuming that each object can have variables corresponding to an item in a header, and I have to set its values by passing a String and recovering its values, receiving a String at runtime.
In order not to have to implement in all Classes, I implemented the following methods in the highest hierarchical level class (Comunicatonobjecthttp):
1st Method to search for field in the current class and in the parent classes, by their name.
protected Field getHeaderField(String fieldName)
    {
        Class current = this.getClass();
        boolean keepSearch = true;
    while(keepSearch)
    {
        try{
            return current.getDeclaredField(fieldName);
        }catch(Exception ex){}
        if((current = current.getSuperclass())== null)
        {
            keepSearch = false;
        }
    }
    return null;
    }
2º Method to generate a String containing all values of fields separated by lines.
public String generateHeaders()
{
    String returnValue = "";
    for(String name : constList.getConstValues())
    {
        try{
        Field field = this.getHeaderField(this.getFieldNameByHeader(name));
        field.setAccessible(true);
        Object value =  field.get(this);
            if(value != null)
            {
                String stringvalue = ""+value;
                if(stringvalue != "")
                {
                    returnValue = returnValue + value + lineSeparator;
                }
            }
        }catch(Exception ex){System.out.println(ex);}
    }
    if(returnValue != "")
    {
        returnValue = returnValue.substring(0,returnValue.length()-   lineSeparator.length());
    }
    return returnValue;
}
3º Receive a String set the value of each field per line of a String passed as parameter.
public void loadHeaders(String protocol)
{
    if(protocol != null)
    {
        if(protocol != "")
        {
            this.headers = protocol;
            String[] lines = protocol.split(lineSeparator);
            for(String line : lines)
            {
                if(line == null)
                {
                    break;
                }
                String[] nameAndValue = line.split(nameAndValueSeparator);
                if(nameAndValue.length >= 2)
                {
                    try{
                        Field field = this.getHeaderField(this.getFieldNameByHeader(nameAndValue[0]));
                        field.setAccessible(true);
                        field.set(this,nameAndValue[1]);
                    }catch(Exception ex){}
                }
            }
        }
    }
}
However I tested and even the names of the variables passed as parameter are correct, an exception occurs stating that these were not found. Could someone please give a hint about some mistake I’m making, or indicate a topic regarding this subject.
Hi thanks for the help ,then getConstValues returns a list of item names of headers and getFieldNameByHeader takes a header name and turns into the name of a class variable
– user5020
@user5020 You have to ensure that the name is exactly the same as the attributes. For example, if there is a header
nome, the class attribute has to be something likeprivate String nome.– utluiz
yes that’s right, I’ll show you some examples of search, Requesthttp has the following fields
protected String headerAccept;
 protected String headerAcceptCharset;
 protected String headerAcceptEncoding;
 protected String headerAcceptLanguage;and put a print on the name of the searched field each time it performs a search
headerAccept 
headerAcceptCharset 
headerAcceptEncoding 
headerAcceptEncoding 
headerAcceptEncoding 
headerAcceptEncoding 
headerAcceptLanguage 
– user5020
Guy discovered was the getFieldNameByHeader in it I use the replace("-","") method; and ended up getting a space at the end, so I added the Trim() method; , Fight for the tips.
– user5020