Wednesday, 26 April 2017

Tag(Marker) Interface in ABAP and Java

“Specific predefined global interface. By integrating the tag interface, classes or other interfaces stand out against the ABAP runtime environment. A tag interface generally does not contain its own interface components, but instead assigns a particular task to the integrating classes or interfaces and changes the way they are handled by the ABAP Compiler.“
And in fact this is not a specific concept of ABAP, but exists in many other language as well.

ABAP tag interface


One of the most famous tag interface in ABAP is if_serializable_object.
SAP ABAP Tutorials and Materials, SAP ABAP Certifications, SAP ABAP Guide

An object instance of class which implements this tag interface could be serialized to an XML String.
The actual serialization and deserialization is done in the kernel.

Another tag interface IF_BADI_INTERFACE


In you BAdI definition, it is impossible to use an interface without including this tag interface.

SAP ABAP Tutorials and Materials, SAP ABAP Certifications, SAP ABAP Guide

Even if you bypass this check via tricks like debugging in ABAP layer, there is still kernel check in the runtime as last defense.

Tag interface in Java


As I mentioned the tag interface is a generic concept which is available in many other language like Java. See more generic definition in Wikipedia.

“The tag/ marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata.“

Java Serialization


Still use serialization for example. Java has its own tag interface for serialization, Serializable. ( just exactly the same logic as if_serializable_object in ABAP ).

SAP ABAP Tutorials and Materials, SAP ABAP Certifications, SAP ABAP Guide

When the serialization is performed, JDK will check whether the instance has implemented this interface by instanceof.

SAP ABAP Tutorials and Materials, SAP ABAP Certifications, SAP ABAP Guide

In ABAP we have similar keyword to achieve the same.

SAP ABAP Tutorials and Materials, SAP ABAP Certifications, SAP ABAP Guide

Cloneable tag interface


Consider the following source code:
public class Employee implements Cloneable {
        private String name;
        private List<String> skill = new ArrayList<String>();
        public Employee(String name){
                 this.name = name;
        }
        public Employee addSkill(String name){
                 this.skill.add(name);
                 return this;
        }
        public static void main(String[] arg){
                 Employee jerry = new Employee("Jerry");
                 jerry.addSkill("ABAP");
                 jerry.addSkill("Java");
                 jerry.addSkill("JavaScript");
                
                 Employee ji = null;
                 try {
                         ji = (Employee) jerry.clone();
                 } catch (CloneNotSupportedException e) {
                         e.printStackTrace();
                 }
                 System.out.println("Clone done");
        }
}

When executed, it will raise the following exception:

SAP ABAP Tutorials and Materials, SAP ABAP Certifications, SAP ABAP Guide

Simply add the tag interface Cloneable:

SAP ABAP Tutorials and Materials, SAP ABAP Certifications, SAP ABAP Guide

And the error disappears:

SAP ABAP Tutorials and Materials, SAP ABAP Certifications, SAP ABAP Guide

This test proves that there must be some check in the native implementation of clone method in JDK.

SAP ABAP Tutorials and Materials, SAP ABAP Certifications, SAP ABAP Guide

No comments:

Post a Comment