Method for establishing mapping relation of program class sets of different versions
Technical Field
The invention belongs to the technical field of computer software, and particularly relates to a method for establishing class set mapping relations of programs of different versions, particularly Java programs.
Background
Object-oriented technology has been a focus of research since its introduction. To date, object-oriented approaches have gradually penetrated various levels and aspects of software development, emerging as object-oriented approaches including object-oriented analysis, design, and programming. Software evolution refers to the process by which software is ramped and brought to a desired morphology. Software evolution plays an important role in prolonging the service life of software, the efficiency of software programs is improved after the software programs are evolved, and defects are corrected. Software maintenance and evolution are inevitable, but sometimes project personnel do not only want to obtain the evolved programs, and more, they desire to obtain the differences of the programs of different versions after the evolution. The evolution analysis of the software can help to comprehensively understand the program differences of different versions, the comprehensive understanding of the programs of the versions and the comprehensive understanding of the program differences of the different versions are the basis of program maintenance, however, the evolved programs have huge code quantity due to complex and linked structures, and the mode of analyzing the program evolution information by reading the source codes is a difficult and unwise behavior.
Although object-oriented technology has significantly improved the maintainability of programs, since the evolution analysis of different versions of programs at present needs to resort to development documents or change records of different versions of programs, the lack, incompleteness and inconsistency of normalized documents are common problems. Therefore, how to extract the high-level structure information of each version from the software programs of different versions under the condition of lacking of such documents is achieved, the mapping relation between the software programs of different versions is established according to the high-level structure information, and the difference between the software programs of different versions is obtained according to the mapping relation, so that the development of the high-level evolution information of the software programs becomes a troublesome problem in the software evolution analysis process.
Disclosure of Invention
In view of the deficiencies of the prior art, the technical problem to be solved by the present invention is to provide a method for establishing class set mapping relationships of different versions of programs, especially Java programs, wherein given two versions of source codes before and after the evolution of a Java program, the mapping relationships of "one-to-one", "one-to-many", "many-to-one" of classes in class sets of Java programs of old and new versions and the "one-to-one" mapping relationships of methods in method sets can be obtained.
In the scheme of the invention, firstly, judging factors of class similarity and method similarity are determined; secondly, establishing a class diagram from the reverse engineering of the source code, for example, analyzing the Java source code by a code analysis tool root to obtain an analyzed product, establishing models of classes in class sets of Java programs of different versions, and generating the class diagram; and finally, establishing the mapping relation of one-to-one, one-to-many and many-to-one of the classes in the class set of the Java program between different versions and the one-to-one mapping of the methods in the classes through a series of algorithm analysis (including calculating the initial class similarity, the method similarity and iteratively establishing the final similarity of the methods and the classes).
The method comprises the following specific steps:
a method for establishing a mapping relation of program class sets of different versions is characterized by comprising the following steps:
step S1: acquiring source codes of two different versions of the same program;
step S2: obtaining data structure information and the relation between classes and calling relation between methods in the classes according to the source codes of the two different versions in the step 1 through code analysis, and generating a class diagram;
step S3: calculating the primary class similarity: establishing model information of two version program class sets through the information obtained in the step S2, taking the class set of one version program as a class set A, calculating the similarity through a predefined class similarity evaluation factor, and determining the similarity of each class of the class set A under the class similarity evaluation factor of each class in a mapped version program class set B;
step S4: calculating the similarity of the method: establishing model information of two version method sets through the information obtained in the step S2, taking the method set of one version program as a method set A, calculating the similarity through a predefined method similarity evaluation factor, and determining the similarity of each method in a mapped version program method set B to each method in the method set A under the method similarity evaluation factor;
step S5: establishing a mapping relation: taking the method pair with the highest similarity, and establishing 'one-to-one' mapping of the methods; and according to the mapping relation of the method, adjusting the similarity of the classes in a step-by-step iteration mode, and finally determining the similarity between the classes.
Preferably, the program is a Java program.
Preferably, the class similarity evaluation factor includes: class name, attribute number, method number, similar attribute ratio and similar method ratio; the similarity evaluation factors of the method comprise: similarity between two classes to which the methods belong, frequency of calling the methods, frequency of calling other methods by the methods, method occupation ratio of mapping relations existing in a method set calling the two methods, and method occupation ratio and method signature of mapping relations existing in a method set called by the two methods.
Preferably, step S2 specifically includes:
step S21: definition of
Definition 1: CLASS set CLASS = { CLASS for a given program1, Class2, ..., Classn };
Wherein, ClassiIs the ith class of the program; represented as a triplet: classi = < className, FIELD, METHOD >(ii) a className represents the name of the class, FIELD represents all attribute sets of the current class, and METHOD represents all METHOD sets of the current class;
definition 2: attribute set FIELD = { FIELD of any one class1, Field2, ..., Fieldn };
Wherein, the FilediIs the ith attribute of the current class; represented as a triplet: fieldi =< fieldModifier, fieldType, fieldName >The fieldModifier is the authority of the attribute, the fieldType is the type of the attribute, and the fieldName is the name of the attribute;
definition 3: METHOD set of any one class = { METHOD = { METHOD }1, Method2, ..., Methodn };
Wherein, MethodiIs the ith method of the current class; represented as a quadruple: method for producing a metal oxide layeri = < methodModifier, methodReturnType, methodName, METHODPARAM>Method modifier indicates method authority, method Return type indicates return value type of method, method name indicates name of method, METHODPARAM = { methodParam =1, methodParam2, ..., methodParamnDenotes a parameter list set of the method, any one method paramjJ-th form showing the methodGinseng;
definition 4: similarity simClass = { sc) of new and old versions11, sc21, ..., scmn };
Wherein, for any scijStored in key-value pair format, with key format classi .className<-> classjclassName, corresponding to a class with a new versioniClass with old versionjThe similarity of (2);
definition 5: a method call Tree = (M, R) representing a call relationship of a method when a program runs, where M = { M = (M, R) }1, m2, ..., mnR is the set of method nodes, and R is the set of method invocation edges; each edge rije.R represents a number from miTo mjOf which the weight represents miCalling mjThe frequency of (d); for arbitrary miIn presence of mi.calli = { calli1, calli2, ..., callinDenotes calling miThe method set of (1); exist mi.becalli = { becalli1, becalli2, ..., becallimDenotes miA set of methods to invoke;
definition 6: similarity simMETHOD = { sm) of new and old version methods11, cm21, ..., smmn };
Wherein, for any smijStoring in the form of key value pairs with a key format of mi<-> mjMethod m with a new version corresponding to the valueiMethod m with old versionjThe similarity of (2);
step S22: handling class names
I/define class Linked List
Chain<SootClass> classes = null;
V/load all classes, put class linked list classes = scene.v ().getapplicationclasses ();
// define class iterator
Iterator<SootClass> iterator = classes.iterator();
while(iterator.hasNext())
do{
SootClass = iterator
Class ci= new ClassStructure (),/store in ClassStructure class form
String classname = SootClass
If the class is not the class of the analyzed Java program, then no record is made
if(!classname.contains(packagename)) continue;
ci.className=classname;
}
setList(CLASS,ci);
Step S23: handling attributes
Extracting attribute information for each class of a class set
for(ci:CLASS){
V/define Attribute information iterator, load class Attribute information and traverse
Iterator<SootField> fieldIteratorerator = ci.getFields().iterator();
while(fieldIteratorerator.hasNext())
do{
Sootfields = field
Stringname = s.getName (), attribute name type = s.getType (), toString (), attribute type int modifiers = s.getModifiers (), attribute rights ()
// return value 0: defaulting; returning the value 1: public; return value 2: a private; return value 4: protected
// store in the form of a FieldStructure class
Field fij = new FieldStructure();
fij.fieldModifiers=String.valueOf(modifiers); fij.fieldType=type; fij.fieldName=name; ci.FIELD.add(fij) Storage ofiAttribute information of class }
}
Step S24: processing method
Extracting method information of each of class set/interface set
for(ci:classlist||ci:interfacelist){
I/define method information iterator, load class method information and traverse
Iterator<SootMethod> methodIterator = ci.getMethods().iterator();
while(methodIterator.hasNext())
do{
Sootmethods = methoditerator. next (),/method information data structure
int modifiers = s.getModifiers (),/method permission Type = s.getTreturnType (),/Return Type
String name = s.getName (),/method name List < Type > paramTypes = s.getParameterTypes (),/parameter List
// store in the form of a method Structure class
Method mij = new MethodStructure(); mij.methodModifiers=String.valueOf(modifiers); mij.methodReturnType=String.valueOf(returntype); mij.methodName=name;
// parameters for the process should be stored separately
for(Type ttemp:paramTypes)
mij.METHODPARAM.add(String.valueOf(ttemp));
ci.METHOD.add(mij) Storage ofiClass method information }
}
Step S25: handling inheritance relationships
// defining inheritance relationship storage Structure
List<String> entenders=new ArrayList<>();
// obtaining a parent class of a class
SootClass superclass = ci.getSuperclass();
// Format: father-subclass
extenders.add(superclass.getName()+"<|--" + ci.getName());
Step S26: processing implementation relationships
// define implementation relationship storage Structure
List<String> implements=new ArrayList<>();
Interface for obtaining certain class (class can realize multiple interfaces in Java program, Chain is adopted for storage)
Chain<SootClass> iChain = ci.getInterfaces();
// generating iterator
Iterator<SootClass> iIterator = iChain.iterator();
while(iIterator .hasNext())
do{
SootClass interfaceclass = iIterator.next();
implememts.add(interfaceclass.getName()+"<|.." + ci.getName());
}
Step S27: handling associative relations
// define association store Structure
List<SootClass> r11s= new ArrayList<>();
List<SootClass> r1ns =new ArrayList<>();
V/determining whether there is an association
if (ci.Fieldik.fieldType.contains(packagename)) then
{
// determining whether or not it is 1 to many
if (ci.Fieldik.fieldType.contains("[]"))
r1ns.add( ci.getName() + "1--1..*" + fieldType);
else
r11s.add( ci.getName() + "1--1" + fieldType);
}
Step S28: handling dependencies
// definition dependency storage architecture
List<String> relys=new ArrayList<>();
// obtaining dependencies
for(String s:method_invoke){
String uml= s.split("->")[0].split(":")[0]+" ..>"+s.split("->")[1].split(":")[0];
if(!relys.contain(uml))then
relys.add(uml);
}
Step S29: handling call relation trees
The first step is as follows:
UL ij←Uijgetvalue (),/DeUijThe left half of the ValueBox
UR ij←UijGetvalue (),/DeUijThe right half of the ValueBox
The second step is that:
if (UijgetClass () = = JAssignStmt) the// if an assignment statement
{
// Presence method Call
if ( UR ij.toString() == JSpecialInvokeExpr.class ||
UR ij.toString() == JVirturalInvokeExpr.class ||
UR ij.toString() == JInterfaceInvokeExpr.class ||
UR ij.toString() == JStaticInvokeExpr.class) then
{
// obtaining called method
invokeMethod<—Uij.getMethod().getSignature();
invokek<—" Mi ->invokeMethod;/record call information
INVOKE.add(invokek);
}
}
else if (UijgetClass () = = JInvokeStmt) the// if call statement
{
if( Uij.toString().contains(“specialinvoke”)||
Uij.toString().contains(“virtualinvoke”)||
Uij.toString().contains(“interfaceinvoke”)||
Uij.toString().contains(“staticinvoke”))then
{
// obtaining called method
invokeMethod<—Uij.getMethod().getSignature();
invokek<—" Mi ->invokeMethod;/record call information
INVOKE.add(invokek);
}
}
The third step:
// construction InvokeTree
for(invokek:INVOKE)
1. Generate(invokek)。
Preferably, the step of calculating the similarity according to the predefined class similarity evaluation factor in step S3 specifically includes the following steps:
classiAnd classjClass name, attribute number, method number, similar attribute ratio, similar method ratio 5 judging factors for calculating classiAnd classjSimilarity of (2):
wherein, fnumiIs classiNumber of middle attributes, fnumjIs classjNumber of middle attributes, mnumiIs classiNumber of methods in (1), mnumjIs classjNumber of methods (1), fnumijIs classiAnd classjNumber of equal attributes in the set, mnumijIs classiAnd classjNumber of same methods in the same way.
Preferably, the step of calculating the similarity through the predefined method similarity evaluation factor in the step S4 specifically includes the following steps:
by the method miAnd mjSimilarity between the two classes to which the method belongs, number of times the method is called, number of times the method calls other methods, call miM of the method setiCall and call mjM of the method setjMethod contrast ratio, m, with a mapping relationship in calliSet of called methods miBecall and mjSet of called methods mjMethod-to-contrast ratio and method signature 6 judgment factor calculation method m with mapping relationship in becalliAnd mjSimilarity of (2):
wherein becallmijIs miBecall and mjThe number of pairs of methods having a mapping relationship in becall, becallnumijIs miBecall or mjNumber of elements of bell, callmijIs miCall and mjThe method for the existence of the mapping relation in the call is to the number, callnumijIs miCall or mjThe number of elements of call.
Preferably, step S5 specifically includes:
the method pair with the highest similarity is selected, one-to-one mapping of the method is established, the similarity of the classes is adjusted step by step through the mapping relation of the method, the similarity between the classes is updated in a step-by-step iteration mode, when the mapping relation is determined by the last method, the similarity of the classes is finally determined, a threshold value is determined, the mapping relation of the classes is established, and the one-to-one mapping relation or the one-to-many mapping relation or the many-to-one mapping relation of the classes is finally obtained.
Preferably, step S5 is specifically realized by the following algorithm:
Data: number of methods in the new version:Nnum,number of methods in the old version:Onum, simCLASS, simMETHOD
Result: method mapping set methodMap, class mapping set classMap
while--Nnum>= 0 do
smij = max{simMETHOD);
methodMap.put(methodi <—> methodj,smij);
scij=(scij+ 1)/2;
for p in Nnum do
for q in Onum do
if methodi ∈methodp.call and methodj∈methodq,call then
callmpq++
end
if methodi ∈methodp.becall and methodj∈methodq,becall then
becallmpq++
end
end
end
update(simMETHOD)
end
if scij>=0.8 then
classMap.put(classi <—> classj,scij);
end。
the invention and its preferred embodiments have the following advantageous properties:
1. effectiveness: for different versions of source codes of a given Java program, the mapping relation, the similarity of classes, the mapping relation of methods and the mapping relation of class sets of the same class can be obtained.
2. The accuracy is as follows: for the calculated class similarity, obtaining class similarity by using class names, attribute numbers, method number attribute information and method information, wherein the similarity of classes with a certain mapping relation before and after evolution should have higher similarity, and the similarity of classes without the mapping relation should not be higher than the similarity between classes with the mapping relation; for the obtained method mapping relation, the found method one-to-one mapping relation is consistent with the mapping relation obtained by directly observing the source code; and for the obtained class set mapping relation, the found mapping relation is ensured to be consistent with the mapping relation obtained by directly observing the source code.
3. Universality: for given different Java programs, the mapping relation of the method and the mapping relation of the class set can be obtained.
The method comprises the steps of analyzing Java source codes through a code analysis tool root, obtaining analyzed products, establishing models of different types of classes in class sets of Java programs of different versions, then establishing one-to-one, one-to-many and many-to-one mapping relations of the classes in the class sets of the Java programs of the different versions through a series of operations, solving the problem that finding the mapping relations among the class sets of the systems of the different versions in a mode of manually reading the source codes and the like is difficult to implement, visually reflecting the system structure of the system, increasing the readability and maintainability of the codes, reducing the difficulty of software developers and maintainers in understanding the systems of the different versions, and providing a method for observing the systems of the different versions at a higher level.
Drawings
The invention is described in further detail below with reference to the following figures and detailed description:
FIG. 1 is a schematic overall flow diagram of an embodiment of the present invention;
FIG. 2 is a schematic diagram of a mapping algorithm according to an embodiment of the present invention;
FIG. 3 is a schematic view of a user input interface of an embodiment of the present invention;
FIG. 4 is a schematic diagram of an output of a version A class diagram of a car rental system for version evaluation according to an embodiment of the invention;
FIG. 5 is a schematic diagram of a version B class diagram output of a car rental system for version evaluation according to an embodiment of the present invention;
FIG. 6 is a diagram illustrating class mapping results according to an embodiment of the present invention
FIG. 7 is a diagram illustrating a mapping result according to an embodiment of the present invention.
Detailed Description
In order to make the features and advantages of the present invention comprehensible, embodiments accompanied with figures are described in detail as follows:
firstly, the implementation of the present embodiment takes the definition of the evaluation factor as the basis, including: and defining evaluation factors of class similarity and method similarity, such as class name, attribute number, attribute information, method number, method information, calling relationship, similarity between classes to which the methods belong, calling times of the methods, called times of the methods and the like.
As shown in fig. 1, the scheme flow provided by this embodiment includes the following steps:
step S1: acquiring source codes of two different versions of the same Java program; for example, given a typical object-oriented Java program, the Java program source code can be analyzed by the code analysis tool root;
step S2: analyzing data structure information in the root and calling logic information between classes corresponding to the Java program classes, wherein the data structure information comprises a class name list, a member variable table, a member method table, a parent class and an interface list. Wherein the member variable information includes: attribute name, type, permission information, whether it is declared in a class, etc.; the information of the member method includes: method name, parameter type list, return value type, authority information, and type for declaring the method; and meanwhile, acquiring static relations (including implementation relations, inheritance relations and association relations) between classes, calling relations (dependency relations) between methods and methods in the classes, and the like, and generating a class diagram through the acquired information.
Step S3: calculating the primary class similarity: establishing model information of two versions of Java program class sets through the information obtained in the step S2, taking the class set of one version of Java program as a class set A, calculating the similarity through predefined class similarity evaluation factors, and determining the similarity of a certain class B in the mapped version of Java program class set B to each class in the class set A under the class similarity evaluation factors;
step S4: calculating the similarity of the method: establishing model information of two version method sets through the information obtained in the step S2, taking a method set of one version Java program as a method set A, calculating similarity through a predefined method similarity evaluation factor, and determining the similarity of a certain method B in a method set B of the mapped version Java program to each method of the method set A under the method similarity evaluation factor;
step S5: the method pair with the highest similarity is taken, and the one-to-one mapping of the methods is established. The similarity of the classes is gradually adjusted through the mapping relation of the method, the similarity between the classes is finally determined through a gradual iteration mode, and finally three kinds of mapping relations of the classes are obtained.
Specifically, the specific implementation thereof comprises:
1 detailed construction of the protocol of this example
1.1 defining class similarity evaluation factor and method similarity evaluation factor
Definition class similarity evaluation factor
In this embodiment, 5 classes of static features are selected as the factors for class similarity evaluation, which are:
1. a class name; 2. the number of attributes; 3. the number of the methods; 4. similar attribute ratio; 5. similar methods are used.
(II) defining method similarity evaluation factor
In this embodiment, 4 pieces of information obtained by simulating system operation, static information (method signature) of the method, and similarity of the method category are selected as factors for judging similarity of the method. (We believe that the method should have only a "one-to-one" mapping)
1. Similarity between the two classes to which the method belongs; 2. the frequency at which the method is invoked; 3. the frequency with which the method calls other methods; 4. calling method contrast of mapping relation in method set of the two methods; 5. the method comparison of the mapping relation exists in the method set called by the two methods; 6. method signature (authority of method, return value type, method name, parameter list type of method).
1.2 obtaining information and generating class diagrams
1.2.1 Definitions
Definition 1: CLASS set CLASS = { CLASS for a given program1, Class2, ..., Classn }。
Wherein, ClassiIs the ith class of programs, which can be represented as a triple: classi = < className, FIELD, METHOD >. className represents the name of the class, FIELD represents the set of all the attributes of the current class, and METHOD represents the set of all the METHODs of the current class.
Definition 2: attribute set FIELD = { FIELD of any one class1, Field2, ..., Fieldn }。
Wherein, the FilediIs the ith attribute of the current class, which can be represented as a triple: fieldi =< fieldModifier, fieldType, fieldName >The fieldModifier is the authority of the attribute, the fieldType is the type of the attribute, and the fieldName is the name of the attribute.
Definition 3: METHOD set of any one class = { METHOD = { METHOD }1, Method2, ..., Methodn }。
Wherein, MethodiThe ith method, which is a current class, can be represented as a quadruple: method for producing a metal oxide layeri = < methodModifier, methodReturnType, methodName, METHODPARAM>Method modifier indicates method authority, method Return type indicates return value type of method, method name indicates name of method, METHODPARAM = { methodParam =1, methodParam2, ..., methodParamnDenotes a parameter list set of the method, any one method paramjThe jth argument of the method is shown.
Definition 4: similarity simClass = { sc) of new and old versions11, sc21, ..., scmn }。
Wherein, for any scijStoring in key-value pair format (key format class)i .className<-> classjclassName, corresponding to a class with a new versioniClass with old versionjSimilarity of (d).
Definition 5: method call Tree = (M, R), which represents the call relationship of the method when the system runs, where M = { M = (M, R) }1, m2, ..., mnIs the set of method nodes, and R is the set of method call edges. Each edge rije.R represents a number from miTo mjOf which the weight represents miCalling mjOf (c) is detected. For arbitrary miIn presence of mi.calli = { calli1, calli2, ..., callinDenotes calling miA method set of (1). Exist mi.becalli = { becalli1, becalli2, ..., becallimDenotes miA set of methods to invoke.
Definition 6: similarity simMETHOD = { sm) of new and old version methods11, cm21, ..., smmn }。
Wherein, for any smijStoring in key value pair form (key format is m)i<-> mjMethod m with a new version corresponding to the valueiMethod m with old versionjSimilarity of (d).
1.2.2 flow Steps
Class (I) name
I/define class Linked List
Chain<SootClass> classes = null;
V/load all classes, put class linked list classes = scene.v ().getapplicationclasses ();
// define class iterator
Iterator<SootClass> iterator = classes.iterator();
while(iterator.hasNext())
do{
SootClass = iterator
Class ci= new ClassStructure (),/store in ClassStructure class form
String classname = SootClass
If the class is not the class of the analyzed Java program, then no record is made
if(!classname.contains(packagename)) continue;
ci.className=classname;
}
setList(CLASS,ci);
(II) Properties
Extracting attribute information for each class of a class set
for(ci:CLASS){
V/define Attribute information iterator, load class Attribute information and traverse
Iterator<SootField> fieldIteratorerator = ci.getFields().iterator();
while(fieldIteratorerator.hasNext())
do{
Sootfields = field
Stringname = s.getName (), attribute name type = s.getType (), toString (), attribute type int modifiers = s.getModifiers (), attribute rights ()
// return value 0: defaulting; returning the value 1: public; return value 2: a private; return value 4: protected
// store in the form of a FieldStructure class
Field fij = new FieldStructure();
fij.fieldModifiers=String.valueOf(modifiers); fij.fieldType=type; fij.fieldName=name; ci.FIELD.add(fij) Storage ofiAttribute information of class }
}
(III) method
Extracting method information of each of class set/interface set
for(ci:classlist||ci:interfacelist){
I/define method information iterator, load class method information and traverse
Iterator<SootMethod> methodIterator = ci.getMethods().iterator();
while(methodIterator.hasNext())
do{
Sootmethods = methoditerator. next (),/method information data structure
int modifiers = s.getModifiers (),/method permission Type = s.getTreturnType (),/Return Type
String name = s.getName (),/method name List < Type > paramTypes = s.getParameterTypes (),/parameter List
// store in the form of a method Structure class
Method mij = new MethodStructure(); mij.methodModifiers=String.valueOf(modifiers); mij.methodReturnType=String.valueOf(returntype); mij.methodName=name;
// parameters for the process should be stored separately
for(Type ttemp:paramTypes)
mij.METHODPARAM.add(String.valueOf(ttemp));
ci.METHOD.add(mij) Storage ofiClass method information }
}
(IV) inheritance relationship
// defining inheritance relationship storage Structure
List<String> entenders=new ArrayList<>();
// obtaining a parent class of a class
SootClass superclass = ci.getSuperclass();
// Format: father-subclass
extenders.add(superclass.getName()+"<|--" + ci.getName());
(V) implementation relationship
// define implementation relationship storage Structure
List<String> implements=new ArrayList<>();
Interface for obtaining certain class (class can realize multiple interfaces in Java program, Chain is adopted for storage)
Chain<SootClass> iChain = ci.getInterfaces();
// generating iterator
Iterator<SootClass> iIterator = iChain.iterator();
while(iIterator .hasNext())
do{
SootClass interfaceclass = iIterator.next();
implememts.add(interfaceclass.getName()+"<|.." + ci.getName());
}
(VI) Association relationship
// define association store Structure
List<SootClass> r11s= new ArrayList<>();
List<SootClass> r1ns =new ArrayList<>();
V/determining whether there is an association
if (ci.Fieldik.fieldType.contains(packagename)) then
{
// determining whether or not it is 1 to many
if (ci.Fieldik.fieldType.contains("[]"))
r1ns.add( ci.getName() + "1--1..*" + fieldType);
else
r11s.add( ci.getName() + "1--1" + fieldType);
}
(VII) dependency relationship
// definition dependency storage architecture
List<String> relys=new ArrayList<>();
// obtaining dependencies
for(String s:method_invoke){
String uml= s.split("->")[0].split(":")[0]+" ..>"+s.split("->")[1].split(":")[0];
if(!relys.contain(uml))then
relys.add(uml);
}
(eighth) calling relationship Tree
The first step is as follows:
UL ij←Uijgetvalue (),/DeUijThe left half of the ValueBox
UR ij←UijGetvalue (),/DeUijThe right half of the ValueBox
The second step is that:
if (UijgetClass () = = JAssignStmt) the// if an assignment statement
{
// Presence method Call
if ( UR ij.toString() == JSpecialInvokeExpr.class ||
UR ij.toString() == JVirturalInvokeExpr.class ||
UR ij.toString() == JInterfaceInvokeExpr.class ||
UR ij.toString() == JStaticInvokeExpr.class) then
{
// obtaining called method
invokeMethod<—Uij.getMethod().getSignature();
invokek<—" Mi ->invokeMethod;/record call information
INVOKE.add(invokek);
}
}
else if (UijgetClass () = = JInvokeStmt) the// if call statement
{
if( Uij.toString().contains(“specialinvoke”)||
Uij.toString().contains(“virtualinvoke”)||
Uij.toString().contains(“interfaceinvoke”)||
Uij.toString().contains(“staticinvoke”))then
{
// obtaining called method
invokeMethod<—Uij.getMethod().getSignature();
invokek<—" Mi ->invokeMethod;/record call information
INVOKE.add(invokek);
}
}
The third step:
// construction InvokeTree
for(invokek:INVOKE)
Generate(invokek)
1.3 calculate class similarity
ClassiAnd classjClass name, attribute number, method number, similar attribute ratio, similar method ratio 5 judging factors for calculating classiAnd classjSimilarity of (2):
wherein, fnumiIs classiNumber of middle attributes, fnumjIs classjNumber of middle attributes, mnumiIs classiNumber of methods in (1), mnumjIs classjNumber of methods (1), fnumijIs classiAnd classjNumber of equal attributes in the set, mnumijIs classiAnd classjNumber of same methods in the same way.
1.4 method of calculating similarity
By the method miAnd mjSimilarity between the two classes to which the method belongs, number of times the method is called, number of times the method calls other methods, call miM of the method setiCall and call mjM of the method setjMethod contrast ratio, m, with a mapping relationship in calliSet of called methods miBecall and mjSet of called methods mjMethod-to-contrast ratio and method signature 6 judgment factor calculation method m with mapping relationship in becalliAnd mjSimilarity of (2):
wherein becallmijIs miBecall and mjThe number of pairs of methods having a mapping relationship in becall, becallnumijIs miBecall or mjNumber of elements of bell, callmijIs miCall and mjThe method for the existence of the mapping relation in the call is to the number, callnumijIs miCall or mjThe number of elements of call.
1.5 mapping Algorithm
The method pair with the highest similarity is taken, and the one-to-one mapping of the methods is established. The similarity of classes is gradually adjusted through the mapping relation of the methods, the similarity between the classes is updated through a gradual iteration mode, when the mapping relation is determined by the last method, the similarity of the classes is finally determined, a threshold value is determined, the mapping relation of the classes is established, and the one-to-one mapping relation, the one-to-many mapping relation and the one-to-many mapping relation of the classes are finally obtained. The specific mapping algorithm is shown in fig. 2.
2 mode of use
The scheme of the embodiment uses an integrated development tool IntelliJ IDEA, and is developed based on Java language.
(1) Before using the method of the embodiment, firstly, the method code is imported into the working interval: placing the two versions of system source code into the src directory as prompted by the user input interface in fig. 3, and modifying PACKAGENAME1 and PACKAGENAME2 of the user input interface according to the package name;
(2) and running the program under the condition of ensuring that no error exists in the first step, and automatically acquiring all relevant information by the method of the embodiment and generating the class diagrams of two versions. And simultaneously displaying the class set mapping relation and the method set mapping relation of the two versions.
3 test example
3.1 class diagram evaluation
Completeness: for a given Java program source code, information such as a class name, attribute authority, attribute type, attribute name, method authority, method return type, method name, method parameter list, class relationship (inheritance, implementation, association, dependency) and the like can be acquired.
Universality: the relevant information can be obtained for given different Java program source codes.
Two versions of a car rental system were used for evaluation, and the class diagram output is shown in fig. 4 and 5:
3.2 class Collection and method Collection mapping evaluation
Effectiveness: for different versions of source codes of a given Java program, the mapping relation, the similarity of classes, the mapping relation of methods and the mapping relation of class sets of the same class can be obtained.
The accuracy is as follows: for the calculated class similarity, obtaining class similarity by using class names, attribute numbers, method number attribute information and method information, wherein the similarity of classes with a certain mapping relation before and after evolution should have higher similarity, and the similarity of classes without the mapping relation should not be higher than the similarity between classes with the mapping relation; for the obtained method mapping relationship, it should be ensured that the found method one-to-one mapping relationship is consistent with the mapping relationship obtained by directly observing the source code; for the obtained class set mapping relationship, it should be ensured that the found mapping relationship is consistent with the mapping relationship obtained by directly observing the source code.
Universality: for given different Java programs, the mapping relation of the method and the mapping relation of the class set can be obtained.
The evaluation was performed using the two versions of the system described above, and the results are shown in fig. 6 and 7.
The obtained method mapping relation and the class set mapping relation are applied to the class diagram, the fact that the class diagram of the version 1 can be converted into the class diagram of the version 2 under the action of the mapping relation can be observed, meanwhile, the result of manually observing the source code is consistent with the result obtained by the method, and the limitation, the accuracy and the universality of the method can be met.
The present invention is not limited to the above preferred embodiments, and various other methods for creating the mapping relationship between different versions of program sets can be derived by anyone based on the teaching of the present invention.