1、1Object-oriented Program Tailoring Based on ModelAbstract: For the redundant codes caused by software reuse and rapid development?in object-oriented?program, the paper proposes a cutting model and designs a cutting scheme based on this model. Firstly, a cutting model is established. Then, static ana
2、lysis is made for object-oriented codes and the cutting information base of objects to be clipped is established. Lastly, the cutting information base and cutting model is matched to cut the program. A test on a practical java project indicates that the cutting scheme can make the volume of the proj
3、ect reduce by about 21%. Key words: object-oriented, redundant code, cutting model, static analysis 1 Introduction Object-oriented?programming 1 uses objects as primary unit and encapsulates the program and data, which not only improves reusability, flexibility and extensibility of software, and red
4、uces lots of repetitive work in software?development?activities, but also improves 2production?efficiency, reduces development cost and development cycle. Object-oriented program has become the most mainstream software development model, and has been widely applied to large-scale project design. How
5、ever, software reuse inevitably increases redundancy of codes, for example, the introduction of external class libraries will take along the useless packages and classes, and the imported classes may have the unused variables?or methods, which increases the redundancy of codes, the system resources
6、for software operation and the software operation cost. Especially in the?embedded domain, with the rapid development of smart phone, tablet?personal computer and internet of things, the number of using object-oriented program of language development including Java, Objective-C, C# and C+ increases
7、rapidly, but the embedded hardware resources are deficient, the provided system resources are limited, and the software has the characteristics of small volume and low resource consumption. Therefore, the object-oriented program should be clipped to remove the useless libraries, packages, classes an
8、d the variables and methods of class, which not only reduces the redundancy of program, the volume of software and occupancy of system resources, but also improves the operation 3efficiency of software and reduces operation cost. However, it is infeasible to cut the codes manually because of the hig
9、h complexity of the large object-oriented programs2. So, it is import to find an approach to cut program efficiently. The present code tailoring researches aim at the embedded Linux system, including compiling custom based on configured tools 3, kernel tailoring methods based on call-graph 4 and tai
10、loring methods based on source code analysis. For embedded software, Cai Hong and Shen Lei proposed an automatic tailoring for embedded software based on coverage test 5. Through the observation on software tailoring technique, the paper proposes an automatic tailoring method based on model for obje
11、ct-oriented software, and takes java language as the object for research. Firstly, the inter-class relationships and right control of java language is analyzed to establish cutting model. Then, a code analysis method of program is designed. Lastly, the automatic cutting method based on model is desi
12、gned. 2 Related Technologies 2.1 Inter-class and interface relationship In UML6, the relationship includes generalization, dependency, association and realization. The association can be further divided into aggregation and combination. 4(1)Generalizations Generalization is that a class (called subc
13、lass or sub-interface) inherits the structure of another class (called parent class or parent interface), in which new structure and behavior can be added and the original behavior can be rewritten. In java language, generalization corresponds to the inheritance relationship between parent class and
14、 sub-class, and between parent interface and sub-interface, the key word extends is used to specify the identification 7. The fig.1 (a) means the inheritance relationship between classes. The classes of java have no multiple-inheritance concept, but the interfaces have it, for example, the fig.1 (b)
15、 shows the multiple-inheritance of the interface. (2)Dependencies Dependency is a using relationship, in which the change of specific things may affect the things using the thing. It is the connection between classes, and it represents that a class depends on the definition of another class. Depende
16、ncy is unidirectional, which means that a class uses another class. The using relationship is occasional, temporary and weak. But the change of the uses class may affect the using class. In java language, the dependence includes local variable, 5parameters in methods and calling static methods. In f
17、ig.2, teacher class uses the object of student class as parameter for teach method, so teacher class depends on student class. (3) Associations Association is a structural?relationship, which indicates that the object of a thing associate with that of another thing. If two associated classes are giv
18、en, the objects of a class can be achieved from that of another class. Association includes dual relationship and multiple relationships. Dual relationship means one-to-one relationship, and multiple relationships mean that one-to-many or many-to-one relationship. The simple association between two
19、classes means the structural relationship with equal status. The association in Java is realized by member variables of classes. Aggregation and composition are special association. Aggregation is the relationship between entirety?and?individual, which means has-a relationship, in which the entirety
20、?and?individual is separable. Combination is contains-a relationship, in which the lifecycle of entirety?and?individual is consistent and inseparable. Entirety?and?individual cant be differentiated for grammar, and only can be divided for semantics. In Fig.3, there are one or more students in a scho
21、ol, and a student may be the registered student of many other 6schools. The relationship between school and student class is the aggregation of entirety and part. The school consists of many departments, and the school is inseparable from departments, so they are composition relationship. (4)Realiza
22、tion Realization means a contract between two entities, which means that an entity defines a contract, and the other entity ensures to fulfill the contract. In java, a class realizes one or more interfaces, and the key word implement is used to clear the identification8. A simple realization is show
23、n in Fig 4. 2.2 Access right of java Java has four access rights, private, default, protected and public, in which default has no any modifiers in codes. (1) Private: the narrowest modifier of access right in Java language. The modified class, variable?and method can only be accessed by the object o
24、f the class. Access from its sub-class and across-package is prohibited. (2) Default: it means that there is no any access modifier, which is generally called default access mode. In this mode, the access is only allowed in the same package. (3) Protect: it is an access modifier between public and p
25、rivate, which is generally called protect. The modified class, 7variable?and method can be only accessed by the method and sub-class of the class, including the sub-class in different packages. (4)Public: it is the widest modifier of access right in Java language, which is generally called public. T
26、he modified class, variable?and method not only can across-class access, but also allow across-package access. 2.3 Static code analysis Static code analysis means that there is no need to operate the tested codes. The correctness of program is only be examined by analyzing or checking the grammar, s
27、tructure, process and interface of source program, which can find out the faults of the code 9 such as unmatched parameters, ambiguous nested statements, wrong recursion, illegal computation, null pointer dereferences and redundant codes. The technology for static code analysis includes automatic ab
28、stract interpretation 10, theorem proving 11, model checking 12, symbolic execution?13 and code inspection based on defect mode 14. The common java static analysis tools are CheckStyle, FindBugs15, PMD and Jtest , etc. The cutting method of the paper starts with making scanning analysis on codes to
29、achieve the cutting information, which is the clipping basis. There is the following information. 8(1)Type information. It includes Library, Package, Class, Interface, Variable and Method. (2)Rights information, It includes private, default, protected and public. (3)Relation and state class informat
30、ion. If the class is inherited, inherits the other classes, associates the other classes and is associated by the other classes belongs to relation class information. State class information means that if it is used in code. And the state of java class means it is new in codes. After achieving the a
31、bove information, the cutting information database of all kinds of objects is established and to match with the cutting model. 3 Cutting Model Cutting model is used to match the parts of program that can be clipped. For the characteristics of the object-oriented program, according to cutting particl
32、e size, the cutting models are divided into class library cutting, package cutting, class and interface cutting, method and variable?cutting. The definition of cutting model is as follows. M=T:Cc1,c2, In the formula, T is Type, which means the type of the 9model. And it includes library,package, cla
33、ss, interface, variable and method. C means the cutting conditions which mean authority information, relations, and state information of cutting information. The authority information means four access modifiers, which can be represented by binary, as shown in Table 2. Relation and state class infor
34、mation is represented by 0 and 1. 0 means there is no the relation or the relation is not used. 1 means that the relation exists or is used. Table 3 shows the table description of cutting conditions, and each record in the table means elements in C set. If there is an object A and the type is type1,
35、 the cutting condition information is c1, and the cutting information is m= type1:c1, judging if it matches with the cutting model M=T:C needs to satisfy the following conditions. (1) type1=T;(2)cC 3.1Class library cutting model Class library cutting means to cut the unused class libraries of the pr
36、ogram. The class libraries imported in java include system class library and external class library. The system class library means the class library in JRE, and the framework class library of web projects such as struts and spring, etc, which are necessary for application programs. The 10external c
37、lass library is imported class library except for system class library, which include component, plug-in and API completing specific functions. In project files, class library exists with the form of jar file. The imported class libraries in codes need to use import statement. Using class library me
38、ans using the classes in it. The paper is focused the cutting of the external class library. The library cutting model is M=Library:C, and the cutting condition C is shown in Table 4. 3.2 Package cutting model Package cutting aims at the useless class packages of program, which includes the imported
39、 external class library. In the program files, class package exists with the form of document and corresponds to catalogue of file system. The imported class packages in codes also use import statement. The class cutting model is M=Package:C, and the cutting condition C is shown in Table 5. 3.3 Class and interface cutting model (1)Class cutting model Class cutting means to delete the classes which have not been used. Judging if a class is used is utterly dependent on if it is instantiated and if it is new in java. Java class includes external class and internal class. The paper mainly