adithyan .p
adithyan .p

Reputation: 121

How does below method work in Java?

i am go through the below method but not getting how my seniors designed.

public LinkedHashMap<String,IPDFField> getFields() {
        LinkedHashMap<String, IPDFField> fields = new LinkedHashMap<String, IPDFField>();

        //^field1c^lastName^nameSuffix
        // Line One
        addField(fields,"1_1", new PDFField(27+X_OFF, 718+Y_OFF, new FieldWidthValidation(134F, "^field1_1^firstName^middleName^lastName^nameSuffix")) { //PI tab
            @Override
            public String getPrintableText(Object o) {
                Disposition d = (Disposition) o;
                return dataFormattingService.NormalizedPersonName(
                        d.getFirstName(), d.getMiddleName(), d.getLastName(), d.getNameSuffix()
                );
            }
        });
} //getFileds method ends

from above method they have called below addField Method but what is getPrintableText inside AddField Method

private void addField(HashMap<String, IPDFField> fields, String fieldKey, IPDFField field) {

        field.setFieldKey(fieldKey);
        if (field.getValidation() != null) {
            field.getValidation().setField(field);
        }

        fields.put(fieldKey, field);

    }

above is not full code , the main functionality is we are trying to write content into pdf but i don't want to paste my full code i just need explanation for above logic

Upvotes: 0

Views: 192

Answers (1)

Timothy Truckle
Timothy Truckle

Reputation: 15622

      addField(/**/, new PDFField(/**/) { 
            @Override
            public String getPrintableText(Object o) {
              // ...
            }
        });

What happens here is the creation of an anonymous inner class.

This anonymous inner class extends the class PDFField and redefines the behavior of the method getPrintableText which is defined in class PDFField.


sorry one more doubt addFiled we have two methods in above , one is anonymous and another is private method,may i know if they are using annonymous class then they could have declared different method name for addField which is private above? – adithyan .p

I'm not sure if I understand that comment...

The anonymous class is

   new PDFField(/**/) { 
        @Override
        public String getPrintableText(Object o) {
          // ...
        }
    }

And this is passed as a parameter to the method addField().

There is no restriction on the visibility of the method that gets the anonymous class instance as a parameter.

Upvotes: 1

Related Questions