[go: up one dir, main page]

CN108062273A - A kind of optimization method and device of program - Google Patents

A kind of optimization method and device of program Download PDF

Info

Publication number
CN108062273A
CN108062273A CN201610983777.3A CN201610983777A CN108062273A CN 108062273 A CN108062273 A CN 108062273A CN 201610983777 A CN201610983777 A CN 201610983777A CN 108062273 A CN108062273 A CN 108062273A
Authority
CN
China
Prior art keywords
sentence
object reference
function unit
nothing operation
source code
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201610983777.3A
Other languages
Chinese (zh)
Inventor
张海东
程哲
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Alibaba Group Holding Ltd
Original Assignee
Alibaba Group Holding Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Alibaba Group Holding Ltd filed Critical Alibaba Group Holding Ltd
Priority to CN201610983777.3A priority Critical patent/CN108062273A/en
Publication of CN108062273A publication Critical patent/CN108062273A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Prevention of errors by analysis, debugging or testing of software
    • G06F11/362Debugging of software
    • G06F11/3628Debugging of software of optimised code

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

This application involves field of software engineering more particularly to a kind of optimization method and devices of program, and in order to solve can not to realize the defensive programming of automation to program source code in the prior art, this method is:Program source code is divided several function units by optimization device, and a function unit can filter out an object reference sentence set, wherein, several object reference sentences are contained in one object reference sentence set again, by using the mode of nesting Xun Huan, automation defensive programming is carried out to each object reference sentence of each function unit in program source code, so, all objects are before called in program source code, it will perform and sentence do-nothing operation accordingly, so as to avoid when program is run, cause program crashing because there is null pointer in program source code, it also avoids repeating cumbersome manual work simultaneously, effectively increase operating efficiency, reduce O&M cost.

Description

A kind of optimization method and device of program
Technical field
This application involves field of software engineering more particularly to a kind of optimization method and devices of program.
Background technology
Under the prior art, in programming process, usually can because of some it is small the problem of, and it is existing that program is caused collapse occur As.Such as, if using JAVA language as program development language, program source code cannot there are null pointer (null pointer), Once there are null pointer, program is run at null pointer, and will paralyse collapse.
And the appearance of problems in order to prevent, developer can usually be detected source code.Due in source code It usually needs to call different objects, therefore, developer needs whether to judge the corresponding source code portion of each object successively There are null pointers.But merely the problem is singly sentenced in program source code using manual type It is disconnected, debug time can be greatly prolonged, adds O&M cost.
Meanwhile using manual type in a large amount of red tape source codes to it is small the problem of judge, it is easy to It omits, once causing program crashing because of omitting, developer may require a great deal of time and search problem, Sometimes where can not even finding problem, very big hidden danger has been buried to program, may result in the appearance of catastrophic effect.
Therefore, it is necessary to design a kind of optimization method of new program to overcome drawbacks described above.
The content of the invention
The embodiment of the present application provides a kind of optimization method and device of program, is automatically found and micro- in settlement procedure source code The problem of small.
Specific technical solution provided by the embodiments of the present application is as follows:
A kind of optimization method of program, including:
According to default extracting rule, function unit set is extracted in program source code, wherein, it is recorded in function unit Methodological function corresponding program source code;
For the function unit in the function unit set, following operation is performed:
In the function unit, according to default screening rule, object reference sentence set is filtered out;
For the object involved by the object reference sentence in the object reference sentence set, judge whether in object tune With being provided in the context of sentence and the object is corresponding sentences do-nothing operation sentence;
For being not provided with the object for sentencing do-nothing operation sentence, in the context of the object reference sentence of the object, Addition with the object is corresponding sentences do-nothing operation sentence.
Optionally, function unit set is extracted in program source code according to default extracting rule, including:
The program source code part that parameter is specified comprising the first kind is divided into a function unit, wherein, described first Class specify parameter including but not limited to:Access attribute, function name, parameter list and function body;
All function unit component function unit sets obtained will be divided.
Optionally, according to default screening rule, object reference sentence set is filtered out, including:
The program source code part that parameter is specified comprising the second class is identified as an object reference sentence, wherein, it is described Second class specify parameter including but not limited to:Object oriented, call operator and calling body;
All object reference sentences composition object reference sentence set that will identify that.
Optionally, for the object reference sentence involved by object, judge whether object reference sentence up and down It is provided in text and the object is corresponding sentences do-nothing operation sentence, including:
The object reference sentence is marked;
In program source code, since being scanned the function unit belonged to the object reference sentence the mark Beginning position;
Judge to whether there is the sentencing comprising " if " for the object involved by the object reference sentence in scanning area Do-nothing operation sentence, if so, determining to sentence do-nothing operation sentence there is provided corresponding for the object, otherwise, it determines for institute Object is stated to be not provided with corresponding sentencing do-nothing operation sentence.
Optionally, for being not provided with the object for sentencing do-nothing operation sentence, the object object reference sentence it is upper Hereinafter, addition with the object is corresponding sentences do-nothing operation sentence, including:
A null is added in the top of the object reference sentence of the object;
Addition sentences do-nothing operation sentence comprising " if " in the null, and in the context for sentencing do-nothing operation sentence The starting character of the sphere of action of do-nothing operation sentence is further sentenced described in addition expression;
The function list that the object reference sentence of scanning to the object belongs to since the object reference sentence of the object The end position of member;
It is determined in scanning area for last set by the object involved by the object reference sentence of the object A object reference sentence;
Below the last one described object reference sentence, the knot of the sphere of action of do-nothing operation sentence is sentenced described in addition expression Beam accords with.
A kind of optimization device of program, including:
First execution unit, for according to default extracting rule, function unit set to be extracted in program source code, In, the corresponding program source code of methodological function is had recorded in function unit;
Second execution unit for the function unit being directed in the function unit set, performs following operation:
In the function unit, according to default screening rule, object reference sentence set is filtered out;
For the object involved by the object reference sentence in the object reference sentence set, judge whether in object tune With being provided in the context of sentence and the object is corresponding sentences do-nothing operation sentence;
For being not provided with the object for sentencing do-nothing operation sentence, in the context of the object reference sentence of the object, Addition with the object is corresponding sentences do-nothing operation sentence.
Optionally, when extracting function unit set in program source code according to default extracting rule, described first holds Row unit is used for:
The program source code part that parameter is specified comprising the first kind is divided into a function unit, wherein, described first Class specify parameter including but not limited to:Access attribute, function name, parameter list and function body;
All function unit component function unit sets obtained will be divided.
Optionally, according to default screening rule, when filtering out object reference sentence set, second execution unit is used In:
The program source code part that parameter is specified comprising the second class is identified as an object reference sentence, wherein, it is described Second class specify parameter including but not limited to:Object oriented, call operator and calling body;
All object reference sentences composition object reference sentence set that will identify that.
Optionally, for the object reference sentence involved by object, judge whether object reference sentence up and down In text be provided with the object is corresponding sentence do-nothing operation sentence when, second execution unit is used for:
The object reference sentence is marked;
In program source code, since being scanned the function unit belonged to the object reference sentence the mark Beginning position;
Judge to whether there is the sentencing comprising " if " for the object involved by the object reference sentence in scanning area Do-nothing operation sentence, if so, determining to sentence do-nothing operation sentence there is provided corresponding for the object, otherwise, it determines for institute Object is stated to be not provided with corresponding sentencing do-nothing operation sentence.
Optionally, for being not provided with the object for sentencing do-nothing operation sentence, the object object reference sentence it is upper Hereinafter, addition with the object is corresponding sentence do-nothing operation sentence when, second execution unit is used for:
A null is added in the top of the object reference sentence of the object;
Addition sentences do-nothing operation sentence comprising " if " in the null, and in the context for sentencing do-nothing operation sentence The starting character of the sphere of action of do-nothing operation sentence is further sentenced described in addition expression;
The function list that the object reference sentence of scanning to the object belongs to since the object reference sentence of the object The end position of member;
It is determined in scanning area for last set by the object involved by the object reference sentence of the object A object reference sentence;
Below the last one described object reference sentence, the knot of the sphere of action of do-nothing operation sentence is sentenced described in addition expression Beam accords with.
The application has the beneficial effect that:
In the embodiment of the present application, program source code is divided into several function units by optimization device, and a function list Member can filter out an object reference sentence set, wherein, it is right to contain several again in an object reference sentence set As call statement, by using the mode of nesting Xun Huan, to each object of each function unit in program source code Call statement all carries out automation defensive programming, in this way, all objects can all perform phase before called in program source code That answers sentences do-nothing operation, so as to avoid when program is run, because causing program crashing there are null pointer in program source code The problem of, however, if it is desired to by manually in substantial amounts of program source code, to it is this small the problem of arrange line by line It looks into, is not only susceptible to situation about ignoring so that ignored mistake causes program crashing, and also adds manual work and bear Load and O&M cost, correspondingly, automatically completing defensive programming, are not only avoided that the cumbersome manual work of repetition, but also Operating efficiency and accuracy rate are effectively increased, reduces O&M cost.
Description of the drawings
Fig. 1 is the optimization method flow chart of the embodiment of the present application Program;
Fig. 2 a are the program source code sectional drawing that automation defensive programming is not carried out in the embodiment of the present application;
Fig. 2 b are the program source code sectional drawing that automation defensive programming is performed in the embodiment of the present application;
Fig. 3 is the program source code section for performing automation defensive programming in the embodiment of the present application in application scenarios one Figure;
Fig. 4 is the program source code section for performing automation defensive programming in the embodiment of the present application in application scenarios two Figure;
Fig. 5 is the apparatus structure schematic diagram of the optimization of the embodiment of the present application Program.
Specific embodiment
In order to solve can not to realize the defensive programming of automation to program source code in the prior art, the program is:It adopts All object reference sentences in program source code are judged with the mode of nesting Xun Huan, judge each object call statement Context in the presence or absence of sentencing do-nothing operation sentence, and to sentence sky for there is no the object reference sentence additions for sentencing do-nothing operation sentence Action statement.
In the embodiment of the present application, first have to class in clear and definite JAVA language, object, attribute, method and object member it is general It reads, JAVA language is the programming language of an object-oriented, and all things in daily life are all objects, and with same kind A kind of things of property is referred to as class.
An abstract concept specifically, class is some aggregates with same alike result or type, e.g., people or Person, automobile, i.e. people or automobile are exactly a class, and object is a specific example of class, e.g., a people for being Zhang San, Alternatively, the automobile that a licence plate is XXXXXX, i.e. Zhang San is a specific example of this class of people, alternatively, licence plate is The automobile of XXXXXX is a specific example of this class of automobile.
Attribute is the feature for description object, and e.g., the height of Zhang San is 1.8 meters, and weight is 70 kilograms, i.e. height and Weight is all the attribute of this object of Zhang San.
Method is for operation object, and method can be an action, alternatively, can be a behavior, e.g., Zhang San sits Under, alternatively, Zhang San has a meal, i.e. sit down and have a meal be exactly this object of Zhang San method.
Attribute belongs to the one side of object static state, and for the feature of description object, and method is to belong to the dynamic one side of object, For operation object, i.e. attribute and method constitute the major part of an object, are the members of object, wherein, object is being grasped When making attribute and method, object reference member property is properly termed as, alternatively, object reference member method (is referred to as object Member function is called, is to be illustrated with function instead of method in following embodiment).
Defensive programming is a kind of programming custom, and developer adds defensive code, Auto-Sensing in programming process To it is small the problem of, so that developer has found the problem of small as soon as possible, but under the prior art, developer passes through addition Defensive code can only detect the problem of small, and can not be to detecting the problem of solves automatically.The application is implemented It in example, is illustrated by taking JAVA language as an example, cannot there are problems that null pointer for JAVA language Program source code, defendd Property programming above-mentioned object can be carried out to sentence do-nothing operation in object reference member function, alternatively, before object reference member property, That is, it is judged that it whether there is null pointer in the context of the object behavior sentence.
For example, it is assumed that object is p there are one in this class of people, there are one member function, above-mentioned member functions for this object of p To name " getName () ", p calls getName () this member function to can be regarded as naming to p.So, when p is called During getName (), if not carrying out sentencing do-nothing operation in the context of call statement, then, once the calling of getName () There are null pointer in journey, then may be collapsed when program is gone to herein;Conversely, when p is when calling getName (), if calling Exist in the context of sentence and sentence do-nothing operation sentence, then, once it is determined that the calling process of getName () is there are null pointer, then Can being on the defensive property programming, make up this defect in time so that when program is gone to herein, subsequent operation can be smoothed out.
In order to realize said program, in the embodiment of the present application, a kind of optimization method of new program is devised, it can be by pin The problem of to being detected in defensive programming, is handled automatically.
The embodiment preferential to the application is described in detail below in conjunction with the accompanying drawings.
As shown in fig.1, in the embodiment of the present application, the detailed process for carrying out program optimization is as follows:
Step 100:Optimization device extracts function unit set according to default extracting rule in program source code.
Specifically, optimization device meeting scanning imaging system source code, and extracted in program source code and included based on extracting rule Function unit set, wherein, the corresponding program source code of methodological function is had recorded in function unit.
More specifically, said extracted rule represents:The program source code part that parameter is specified comprising the first kind is divided For a function unit, and all function unit component function unit sets obtained will be divided, wherein, the first kind is specified Parameter including but not limited to:Access attribute, function name, parameter list and function body.
It is possible to further perform said extracted rule by the way of function, the form of above-mentioned function is:Extraction rule Title then<Access attribute, function name, parameter list, function body>(SetFunction<Access, Name, Vars, FunctionBody>), wherein, round bracket is generally used in the access qualifier of Access representative functions, Vars expression parameter lists () is wrapped up, and FunctionBody representative function bodies are generally wrapped up with braces { }.
Specifically, the access qualifier of function can have but be not limited to following several forms:
Publicly-owned (public), when a member is declared as public, it is pair to represent the member property, member function What all objects opened, i.e. no matter which bag above-mentioned object belongs to, and can access the member.
Privately owned (private), when a member is declared into private, represent above-mentioned member can only by it is above-mentioned into Object accesses where member within class, and cannot be accessed by any class outside class where above-mentioned member.
(protect) of protection when a member is declared as protect, represents above-mentioned member only to above-mentioned member Object accesses within the bag of place, and the subclass for having inheritance outside bag where above-mentioned member can also be visited in a manner of succession Ask the member.
(default) of acquiescence, when a member is not using any of the above described access qualifier, i.e. default is represented It is default access, the member is only visible to the class in bag where above-mentioned member, i.e. appointing in bag where representing above-mentioned member What object can access above-mentioned member.
For example, it is assumed that the format setting of the corresponding function of extracting rule is SetFunction<Access, Name, Vars, FunctionBody>If function unit is " public void test (String arg, int there are one in program source code i){};", then, based on the form of above-mentioned function, " public " corresponds to " Access ", and " test " corresponds to " Name ", " (String arg, int i) " corresponds to " parameter list ", and " { } " corresponds to " FunctionBody ", and above-mentioned function unit is just It can be extracted.
Step 101:Optimization device chooses a function unit from the function unit set currently extracted.
After optimizing device acquisition function unit set, a function unit can be chosen from current function unit set.
Specifically, a function unit is included at least in above-mentioned function unit set, if in above-mentioned function unit set Contain only a function unit, then, after the operation that optimization device is determined in current function unit is finished, directly tie Shu Xunhuan;If a function unit is contained in above-mentioned function unit set incessantly, then, when optimization device determines current function After operation in unit is finished, it may proceed to perform corresponding operation in next function unit, until determining current function After all function units in unit set complete corresponding operation, end loop.
Step 102:Optimization device filters out object tune in the function unit currently chosen according to default screening rule With sentence set.
After optimizing device Selection of Function unit, it can be carried out based on default screening rule in the function unit currently chosen Scanning, filters out object reference sentence set present in above-mentioned function unit.
Specifically, above-mentioned screening rule represents:The program source code part that parameter is specified comprising the second class is identified as One object reference sentence, and all object reference sentences composition object reference sentence set that will identify that, wherein, described the Two classes specify parameter including but not limited to:Object oriented, call operator and calling body.
It is possible to further perform above-mentioned screening rule by the way of function, the form of above-mentioned function is:Screening rule Title then<Object, call operator call body>(DoFun<Obj, Dot, FunctionObj>), wherein, Obj represents object Title, Dot represents call operator, and in JAVA language, call operator is dot operator, and FunctionObj represents to call Body can be the member function of object or the member property of object.
For example, it is assumed that the format setting of the corresponding function of screening rule is DoFun<Obj, Dot, FunctionObj>If Object reference sentence is " String name=p.getName there are one in the function unit of selection;", then, based on above-mentioned letter Several forms, " p " correspond to Obj, and " " corresponds to call operator, and " getName ", which corresponds to, calls body, you can filters out State object reference sentence " String name=p.getName existing for function unit;”.
Further, the example above is simply illustrated by taking an object reference sentence as an example, if from current function unit Filter out existing object reference sentence more than one, then, above-mentioned multiple object reference sentences can form an object reference Sentence set.
Step 103:Optimization device chooses an object reference sentence in the object reference sentence set currently filtered out, And corresponding mark is done at above-mentioned object reference sentence, it is scanned upwards since mark.
Optimization device chooses an object reference sentence in the object reference sentence set currently filtered out, and above-mentioned A mark is done at object reference sentence, wherein, above-mentioned mark can be a label or a label, complete pair After the mark for the object reference sentence currently chosen, optimization device can be since mark, in the function unit bag currently chosen It is scanned upwards in the program source code contained, until scanning rising for the program source code included to the function unit currently chosen Until beginning position, judge sentence empty behaviour for whether the object in the object reference sentence currently chosen performs in scanning area Make.
Step 104:Whether optimization device judges the object being directed in scanning area in the object reference sentence currently chosen It has carried out sentencing do-nothing operation, if so, performing step 106;Otherwise, step 105 is performed.
Specifically, optimization device start to be scanned upwards in the mark for the object reference sentence currently chosen, to During upper scanning, if there is the judgement sentence for including " if " in optimization device discovery, to the above-mentioned judgement for including " if " Semantic search is carried out in the expression formula of sentence, and is determined whether there is according to search result and sentences do-nothing operation sentence accordingly, wherein, Semantic search refers to the surface meaning of word for being not limited to sentence, but is accurately captured really containing for sentence expression by semantic analysis Justice.
For example, if the object of above-mentioned object reference sentence is p, do-nothing operation sentence is sentenced for " if (p!=null) ", then, To above-mentioned object carry out semantic search refer to based on above-mentioned object sentence do-nothing operation sentence content carry out, i.e. need to looking for To the expression formula for judging sentence comprising " if " analyzed, whether the content for analyzing above-mentioned expression formula is " if (p!= Null) " rather than only above-mentioned expression formula is judged with the word of if or null.
Further, according to analysis result, if the content of above-mentioned expression formula is not object in above-mentioned object reference sentence Sentence do-nothing operation sentence, then continue up carry out semantic search, the initial position until searching function unit determines object reference It is not present in the context of sentence and object is corresponding sentences do-nothing operation sentence involved by the object reference sentence, then performs step 105, if the content of above-mentioned expression formula sentences do-nothing operation sentence for object in above-mentioned object reference sentence, illustrate above-mentioned object It has carried out sentencing do-nothing operation, has then performed step 106.
It is to describe to sentence do-nothing operation with JAVA language, under JAVA environment, sentencing do-nothing operation sentence can in the embodiment of the present application Think " if (p!=null) ", but in practical application, application scenarios can also be C++ environment etc., and may be used also under C++ environment By using carrying out sentencing do-nothing operation in the form of pointer, corresponding to sentence do-nothing operation sentence can be " if (p) ", due to the application of the application Scene is JAVA environment, and therefore, the embodiment of the present application is to be directed to JAVA language, and with " if (p!=null) " it is used as and sentences empty behaviour It is illustrated as sentence, details are not described herein.
Step 105:Optimization device is added for the object reference sentence currently chosen and above-mentioned object is corresponding sentences sky Action statement.
Specifically, at step 104, if optimization device determine to be not present in the context of object reference sentence with it is above-mentioned right Sentence do-nothing operation sentence as corresponding, then optimizing device can add and above-mentioned object in the context of above-mentioned object reference sentence It is corresponding to sentence do-nothing operation sentence.
For example, optimization device first adds a line null in the top of above-mentioned object reference sentence, then bag is added at null Sentence do-nothing operation sentence containing " if ", and in the context for sentencing do-nothing operation sentence comprising " if ", further addition represents to sentence sky The starting character of the sphere of action of action statement after the completion of addition, starts to scan for downwards, until the function unit currently chosen Terminate, whether judge for the object in above-mentioned object reference sentence also there are other object reference sentences, if being not present, directly It is connected on after above-mentioned object reference sentence, addition represents to sentence the end mark of the sphere of action of do-nothing operation sentence, if in the presence of in pin Corresponding object reference sentence when being called to the above-mentioned object that the last time of the object in above-mentioned object reference sentence occurs Lower section, addition represents to sentence the end mark of the sphere of action of do-nothing operation sentence, this process, which is considered as, to be completed for currently choosing Do-nothing operation sentence is sentenced in object reference sentence increase.
For example, it is assumed that the object of object reference sentence 1 is p, if optimization device is determined in the context of object reference sentence 1 There is no do-nothing operation sentence is sentenced for object P, then a line null is added in the top of object reference sentence 1, and at null Add sentence " if (p!=null) " and in " if (p!=null) " sphere of action for representing to sentence do-nothing operation sentence is added below Starting character " { ", if during search, Finding Object p is called by object reference sentence 2 again, then continue downwards carry out Search, it is assumed that terminate also to be called by other object reference sentences without Finding Object p until searching function unit, then, it is excellent The lower section for the object reference sentence 2 that can occur in last time is put in makeup, and addition represents to sentence the sphere of action of do-nothing operation sentence End mark " } ".
Step 106:Also whether there are untreated right in the object reference sentence set that optimization device judgement is currently chosen As call statement, if so, return to step 103;Otherwise, step 107 is performed.
Step 104 and step 105 are accepted, since multiple object reference languages can be included in an object reference sentence set Therefore sentence, after optimization device completes the automation defensive programming to the object reference sentence currently chosen, can judge to work as Whether also there are untreated object reference sentence in the object reference sentence set of preceding selection, if the object reference currently chosen Untreated object reference sentence is still remained in sentence set, then back to step 103, in the object reference language of current selected An object reference sentence is chosen again in sentence set, and does corresponding mark at above-mentioned object reference sentence, is opened from mark Beginning is scanned upwards, and subsequent step is identical with step 104, step 105, and which is not described herein again.
If it conversely, is performed in the object reference sentence set currently chosen there is no untreated object reference sentence Step 107.
Step 107:Optimize device to judge whether also there are untreated function unit in current function unit set, if It is, then return to step 101;Otherwise, current process is terminated.
Specifically, when performing step 106, if there is no untreated right in the object reference sentence set currently chosen As call statement, then it represents that optimization device is had been completed to the object reference sentence set in the function unit currently chosen Defensive programming is automated, since multiple function units can be included in a function unit set, when optimization device is true After fixed completion is to the automation defensive programming for the function unit currently chosen, it is also necessary to the function unit set currently extracted Judged, confirm whether also there are untreated function units in above-mentioned function unit set.
Further, if also performing step 101 there are untreated function unit in above-mentioned function unit set, from Choose a function unit in the function unit set currently extracted again, subsequent step and step 102, step 103, step 104, Step 105 and step 106 are identical, and which is not described herein again.
If conversely, optimizing device determining that untreated function unit is not present in the function unit set currently extracted, Represent that optimization device has completed the automation defensive programming of the function unit set to currently extracting, i.e. program source code In all object reference sentences context in exist sentence do-nothing operation sentence.
By above-mentioned flow, you can complete the defensive volume of the automation to object reference sentence all in program source code Journey, all objects in program source code have been carried out sentencing do-nothing operation, in this way, program is kept away in operational process before called The problem of having exempted to cause program crashing because there is null pointer.
However, in the embodiment of the present application, preferred embodiment can be completed to each in function unit set The automation defensive programming of each object reference sentence in each object reference sentence set in function unit, into One step, it can also be only completed in the partial objects call statement set in the partial function unit in function unit set The automation defensive programming of partial objects call statement, does not limit herein.
For example, refering to shown in Fig. 2 a and Fig. 2 b, in fig. 2 a, before object " p " call function, it is not carried out sentencing empty behaviour Make, cause, in practice, can directly collapse when program source code is run to herein, and in figure 2b, due at object " p " Before call function, it is with the addition of and sentences do-nothing operation sentence " if (p!=null) ", the operation of null pointer is avoided, so as to optimize program Source code.
Automation defensive programming is carried out to program source code below by two specific application scenarios to illustrate.
Application scenarios one:Polygamma function single object, refering to shown in Fig. 3.
Specifically, in scene one, a function unit x1 is first extracted, specifically as shown in the program source code in Fig. 3, " people (Person) " this class has been given a definition four functions, and aforementioned four function is respectively " having a meal (eat) ", " has a bath (wash) ", " work (work) " and " walking (walk) ", and, the object for defining " Person " is " p ".
Further, when program source code is run, according to default screening rule in function unit x1, filter out pair As call statement set y1, i.e. each object that " public void test (Person p) { } " and " { } " in Fig. 3 is included Call statement.
When detecting first object reference sentence " p.eat ();" when, it can be in " p.eat ();" at start to search for upwards, It determines to search function unit x1 initial positions, also finds no and exist on the do-nothing operation sentence of sentencing of object " p ", then exist “p.eat();" top add a null, and at above-mentioned null addition sentence do-nothing operation sentence " if (p!=null) ", then In " p.eat ();" top addition represent sphere of action starting character " { " starts afterwards in above-mentioned object reference sentence set It is scanned, in the last one object reference sentence " p.walk () on object " p ";" lower section addition expression effect model The end mark " } enclosed " so far, is completed to first object reference sentence " p.eat ();" sentence the addition of do-nothing operation sentence.
Then, start for second object reference sentence " p.wash ();" judge whether to need to add to sentence do-nothing operation language Sentence, when program source code runs to " p.wash ();" at when, from " p.wash ();" at start to scan up, judge “p.wash();" at before whether be with the addition of and sentence do-nothing operation sentence " if (p on object " p "!=null) ", it is based on Semantic search is determined in " p.wash ();" be with the addition of sentence do-nothing operation sentence " if (p on object " p " before!= Null) ", then terminate for second object reference sentence " p.wash ();" correlated judgment operation.
Then start for the 3rd object reference sentence " p.work ();" judge whether to need to add to sentence do-nothing operation language Sentence, concrete operations are with being directed to second object reference sentence " p.wash ();" judge whether to need to add to sentence do-nothing operation sentence Operating procedure is identical, and details are not described herein.
Finally, for the 4th object reference sentence " p.walk ();" judge whether to need to add to sentence do-nothing operation sentence, With for second object reference sentence " p.wash ();" judge whether to need to add the operating procedure phase for sentencing do-nothing operation sentence Together, details are not described herein.
So far, complete to carry out program source code in polygamma function single object application scenarios one the complete of automation defensive programming Process.
Application scenarios two:Polygamma function is multipair as refering to shown in Fig. 4.
Specifically, in scene two, a function unit x2 is first extracted, specifically as shown in the program source code in Fig. 4, " cat (Cat) " this class has been given a definition two functions, and above-mentioned two function is respectively " climbing tree (climbTree) " and " catches mouse (catchMouse) ", and, the object of " Cat " is defined as " cat ", given a definition a function in " dog (Dog) " this class, on Function is stated as " swimming (swimming) ", and, the object for defining " Dog " is " dog ", it can be seen that, " climbTree " and The object of " catchMouse " the two functions is " cat ", and the object of " swimming " this function is " dog ".
Further, when program source code is run, according to default screening rule in function unit x2, filter out pair As call statement set y2, i.e. " public void test (Cat cat, Dog dog, Person person) { } " in Fig. 4 The each object call statement that " { } " includes.
When detecting first object reference sentence " cat.catchMouse ();" when, it can be in " cat.catchMouse ();" at start to search for upwards, determine to search function unit x2 initial positions, also find no sentencing on object " cat " Do-nothing operation sentence exists, then in " cat.catchMouse ();" top add a null, and add and sentence at above-mentioned null Do-nothing operation sentence " if (cat!=null) ", then in " cat.catchMouse ();" top addition represent sphere of action rise Beginning, " { " starts to be scanned in object reference sentence set y2 afterwards, in the last one object on object " cat " symbol Call statement " cat.climbTree ();" lower section addition represent sphere of action end mark " } ", so far, complete to first A object reference sentence " cat.catchMouse ();" the addition for sentencing do-nothing operation sentence.
Then, start for second object reference sentence " cat.climbTree ();" judge whether to need to add to sentence sky Action statement, when program source code runs to " cat.climbTree ();" at when, from " cat.climbTree ();" at start It scans up, judges in " cat.climbTree ();" at before whether be with the addition of and sentence do-nothing operation on object " cat " Sentence " if (cat!=null) ", based on semantic search, determine in " cat.climbTree ();" before be with the addition of on Object " cat " sentences do-nothing operation sentence " if (cat!=null) ", terminate to second object reference sentence “cat.climbTree();" sentence the addition of do-nothing operation sentence.
Again then, for the 3rd object reference sentence " dog.swimming ();" judge whether to need to add to sentence empty behaviour Make sentence, when program source code runs to " dog.swimming ();" at when, from " dog.swimming ();" at start upwards Scanning determines that function unit x2 initial positions are arrived in scanning, also finds no and exists on the do-nothing operation sentence of sentencing of object " dog ", Then in " dog.swimming ();" top add a null, and at above-mentioned null addition sentence do-nothing operation sentence " if (dog! =null) ", then in " dog.swimming ();" top addition represent sphere of action starting character " { " starts afterwards upper It states and is scanned in object reference sentence set y2, in the last one object reference sentence on object " p " “dog.swimming();" lower section addition represent sphere of action end mark " } ", so far, complete to the 3rd object reference Sentence " dog.swimming ();" sentence the addition of do-nothing operation sentence.
Correlated judgment operation in down-stream source code is identical with scene one, is not repeating herein.
So far, it is multipair as carrying out the complete of automation defensive programming in application scenarios two to program source code to complete polygamma function Process.
As shown in fig.5, in the embodiment of the present application, optimization device performs list including at least the first execution unit 51 and second Member 52, wherein,
First execution unit 51, for according to default extracting rule, function unit set to be extracted in program source code, Wherein, the corresponding program source code of methodological function is had recorded in function unit;
Second execution unit 52 for the function unit being directed in the function unit set, performs following operation:
In the function unit, according to default screening rule, object reference sentence set is filtered out;
For the object involved by the object reference sentence in the object reference sentence set, judge whether in object tune With being provided in the context of sentence and the object is corresponding sentences do-nothing operation sentence;
For being not provided with the object for sentencing do-nothing operation sentence, in the context of the object reference sentence of the object, Addition with the object is corresponding sentences do-nothing operation sentence.
Optionally, when extracting function unit set in program source code according to default extracting rule, described second holds Row unit 52 is used for:
The program source code part that parameter is specified comprising the first kind is divided into a function unit, wherein, described first Class specify parameter including but not limited to:Access attribute, function name, parameter list and function body;
All function unit component function unit sets obtained will be divided.
Optionally, according to default screening rule, when filtering out object reference sentence set, second execution unit 52 For:
The program source code part that parameter is specified comprising the second class is identified as an object reference sentence, wherein, it is described Second class specify parameter including but not limited to:Object oriented, call operator and calling body;
All object reference sentences composition object reference sentence set that will identify that.
Optionally, for the object reference sentence involved by object, judge whether object reference sentence up and down In text be provided with the object is corresponding sentence do-nothing operation sentence when, second execution unit 52 is used for:
The object reference sentence is marked;
In program source code, since being scanned the function unit belonged to the object reference sentence the mark Beginning position;
Judge to whether there is the sentencing comprising " if " for the object involved by the object reference sentence in scanning area Do-nothing operation sentence, if so, determining to sentence do-nothing operation sentence there is provided corresponding for the object, otherwise, it determines for institute Object is stated to be not provided with corresponding sentencing do-nothing operation sentence.
Optionally, for being not provided with the object for sentencing do-nothing operation sentence, the object object reference sentence it is upper Hereinafter, addition with the object is corresponding sentence do-nothing operation sentence when, second execution unit 52 is used for:
A null is added in the top of the object reference sentence of the object;
Addition sentences do-nothing operation sentence comprising " if " in the null, and in the context for sentencing do-nothing operation sentence The starting character of the sphere of action of do-nothing operation sentence is further sentenced described in addition expression;
The function list that the object reference sentence of scanning to the object belongs to since the object reference sentence of the object The end position of member;
It is determined in scanning area for last set by the object involved by the object reference sentence of the object A object reference sentence;
Below the last one described object reference sentence, the knot of the sphere of action of do-nothing operation sentence is sentenced described in addition expression Beam accords with.
In the embodiment of the present application, program source code is divided into several function units by optimization device, and a function list Member can filter out an object reference sentence set, wherein, it is right to contain several again in an object reference sentence set As call statement, by using the mode of nesting Xun Huan, to each object of each function unit in program source code Call statement all carries out automation defensive programming, in this way, all objects can all perform phase before called in program source code That answers sentences do-nothing operation, so as to avoid when program is run, because causing program crashing there are null pointer in program source code The problem of, however, if it is desired to by manually in substantial amounts of program source code, to it is this small the problem of arrange line by line It looks into, is not only susceptible to situation about ignoring so that ignored mistake causes program crashing, and also adds manual work and bear Load and O&M cost, correspondingly, automatically completing defensive programming, are not only avoided that the cumbersome manual work of repetition, but also Operating efficiency and accuracy rate are effectively increased, reduces O&M cost.
It should be understood by those skilled in the art that, embodiments herein can be provided as method, system or computer program Product.Therefore, the reality in terms of complete hardware embodiment, complete software embodiment or combination software and hardware can be used in the application Apply the form of example.Moreover, the computer for wherein including computer usable program code in one or more can be used in the application The computer program production that usable storage medium is implemented on (including but not limited to magnetic disk storage, CD-ROM, optical memory etc.) The form of product.
The application is with reference to the flow according to the method for the embodiment of the present application, equipment (system) and computer program product Figure and/or block diagram describe.It should be understood that it can be realized by computer program instructions every first-class in flowchart and/or the block diagram The combination of flow and/or box in journey and/or box and flowchart and/or the block diagram.These computer programs can be provided The processor of all-purpose computer, special purpose computer, Embedded Processor or other programmable data processing devices is instructed to produce A raw machine so that the instruction performed by computer or the processor of other programmable data processing devices is generated for real The device for the function of being specified in present one flow of flow chart or one box of multiple flows and/or block diagram or multiple boxes.
These computer program instructions, which may also be stored in, can guide computer or other programmable data processing devices with spy Determine in the computer-readable memory that mode works so that the instruction generation being stored in the computer-readable memory includes referring to Make the manufacture of device, the command device realize in one flow of flow chart or multiple flows and/or one box of block diagram or The function of being specified in multiple boxes.
These computer program instructions can be also loaded into computer or other programmable data processing devices so that counted Series of operation steps is performed on calculation machine or other programmable devices to generate computer implemented processing, so as in computer or The instruction offer performed on other programmable devices is used to implement in one flow of flow chart or multiple flows and/or block diagram one The step of function of being specified in a box or multiple boxes.
Although the preferred embodiment of the application has been described, those skilled in the art once know basic creation Property concept, then can make these embodiments other change and modification.So appended claims be intended to be construed to include it is excellent It selects embodiment and falls into all change and modification of the application scope.
Obviously, those skilled in the art can carry out the embodiment of the present application various modification and variations without departing from this Shen Please embodiment spirit and scope.In this way, if these modifications and variations of the embodiment of the present application belong to the application claim And its within the scope of equivalent technologies, then the application is also intended to comprising including these modification and variations.

Claims (10)

1. a kind of optimization method of program, which is characterized in that including:
According to default extracting rule, function unit set is extracted in program source code, wherein, the side of having recorded in function unit The corresponding program source code of method function;
For the function unit in the function unit set, following operation is performed:
In the function unit, according to default screening rule, object reference sentence set is filtered out;
For the object involved by the object reference sentence in the object reference sentence set, judge whether in object reference language It is provided in the context of sentence and the object is corresponding sentences do-nothing operation sentence;
For the object for sentencing do-nothing operation sentence is not provided with, in the context of the object reference sentence of the object, add With the object is corresponding sentences do-nothing operation sentence.
2. the method as described in claim 1, which is characterized in that letter is extracted in program source code according to default extracting rule Counting unit set, including:
The program source code part that parameter is specified comprising the first kind is divided into a function unit, wherein, the first kind refers to Determine parameter including but not limited to:Access attribute, function name, parameter list and function body;
All function unit component function unit sets obtained will be divided.
3. the method as described in claim 1, which is characterized in that according to default screening rule, filter out object reference sentence Set, including:
The program source code part that parameter is specified comprising the second class is identified as an object reference sentence, wherein, described second Class specify parameter including but not limited to:Object oriented, call operator and calling body;
All object reference sentences composition object reference sentence set that will identify that.
4. the method as described in claim 1,2 or 3, which is characterized in that for the object involved by the object reference sentence, Judge whether to be provided in the context of object reference sentence and the object is corresponding sentences do-nothing operation sentence, including:
The object reference sentence is marked;
In program source code, the start bit for the function unit that scanning to the object reference sentence belongs to since the mark It puts;
Judge to whether there is in scanning area and sentence empty behaviour comprising " if " for the object involved by the object reference sentence Make sentence, if so, determining to sentence do-nothing operation sentence there is provided corresponding for the object, otherwise, it determines for described right As being not provided with corresponding sentencing do-nothing operation sentence.
5. the method as described in claim 1,2 or 3, which is characterized in that for being not provided with the object for sentencing do-nothing operation sentence, In the context of the object reference sentence of the object, addition with the object is corresponding sentences do-nothing operation sentence, including:
A null is added in the top of the object reference sentence of the object;
Addition sentences do-nothing operation sentence comprising " if " in the null, and into one in the context for sentencing do-nothing operation sentence The starting character of the sphere of action of do-nothing operation sentence is sentenced described in step addition expression;
The function unit that the object reference sentence of scanning to the object belongs to since the object reference sentence of the object End position;
It is right for the last one set by the object involved by the object reference sentence of the object to be determined in scanning area As call statement;
Below the last one described object reference sentence, the end of the sphere of action of do-nothing operation sentence is sentenced described in addition expression Symbol.
6. a kind of optimization device of program, which is characterized in that including:
First execution unit, for according to default extracting rule, function unit set to be extracted in program source code, wherein, The corresponding program source code of methodological function is had recorded in function unit;
Second execution unit for the function unit being directed in the function unit set, performs following operation:
In the function unit, according to default screening rule, object reference sentence set is filtered out;
For the object involved by the object reference sentence in the object reference sentence set, judge whether in object reference language It is provided in the context of sentence and the object is corresponding sentences do-nothing operation sentence;
For the object for sentencing do-nothing operation sentence is not provided with, in the context of the object reference sentence of the object, add With the object is corresponding sentences do-nothing operation sentence.
7. device as claimed in claim 6, which is characterized in that letter is extracted in program source code according to default extracting rule During counting unit set, first execution unit is used for:
The program source code part that parameter is specified comprising the first kind is divided into a function unit, wherein, the first kind refers to Determine parameter including but not limited to:Access attribute, function name, parameter list and function body;
All function unit component function unit sets obtained will be divided.
8. device as claimed in claim 6, which is characterized in that according to default screening rule, filter out object reference sentence During set, second execution unit is used for:
The program source code part that parameter is specified comprising the second class is identified as an object reference sentence, wherein, described second Class specify parameter including but not limited to:Object oriented, call operator and calling body;
All object reference sentences composition object reference sentence set that will identify that.
9. the device as described in claim 6,7 or 8, which is characterized in that for the object involved by the object reference sentence, Judge whether to be provided in the context of object reference sentence with the object is corresponding sentence do-nothing operation sentence when, described the Two execution units are used for:
The object reference sentence is marked;
In program source code, the start bit for the function unit that scanning to the object reference sentence belongs to since the mark It puts;
Judge to whether there is in scanning area and sentence empty behaviour comprising " if " for the object involved by the object reference sentence Make sentence, if so, determining to sentence do-nothing operation sentence there is provided corresponding for the object, otherwise, it determines for described right As being not provided with corresponding sentencing do-nothing operation sentence.
10. the device as described in claim 6,7 or 8, which is characterized in that for being not provided with pair for sentencing do-nothing operation sentence As, in the context of the object reference sentence of the object, addition with the object is corresponding sentence do-nothing operation sentence when, institute The second execution unit is stated to be used for:
A null is added in the top of the object reference sentence of the object;
Addition sentences do-nothing operation sentence comprising " if " in the null, and into one in the context for sentencing do-nothing operation sentence The starting character of the sphere of action of do-nothing operation sentence is sentenced described in step addition expression;
The function unit that the object reference sentence of scanning to the object belongs to since the object reference sentence of the object End position;
It is right for the last one set by the object involved by the object reference sentence of the object to be determined in scanning area As call statement;
Below the last one described object reference sentence, the end of the sphere of action of do-nothing operation sentence is sentenced described in addition expression Symbol.
CN201610983777.3A 2016-11-08 2016-11-08 A kind of optimization method and device of program Pending CN108062273A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201610983777.3A CN108062273A (en) 2016-11-08 2016-11-08 A kind of optimization method and device of program

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201610983777.3A CN108062273A (en) 2016-11-08 2016-11-08 A kind of optimization method and device of program

Publications (1)

Publication Number Publication Date
CN108062273A true CN108062273A (en) 2018-05-22

Family

ID=62137474

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201610983777.3A Pending CN108062273A (en) 2016-11-08 2016-11-08 A kind of optimization method and device of program

Country Status (1)

Country Link
CN (1) CN108062273A (en)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101566968A (en) * 2009-05-20 2009-10-28 阿里巴巴集团控股有限公司 Method for checking null pointer in source code and computer system
US8307435B1 (en) * 2010-02-18 2012-11-06 Symantec Corporation Software object corruption detection
CN103955426A (en) * 2014-04-21 2014-07-30 中国科学院计算技术研究所 Method and device for detecting code C null-pointer reference
CN105607990A (en) * 2014-11-19 2016-05-25 腾讯科技(成都)有限公司 Null pointer crash mining method and device

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101566968A (en) * 2009-05-20 2009-10-28 阿里巴巴集团控股有限公司 Method for checking null pointer in source code and computer system
US8307435B1 (en) * 2010-02-18 2012-11-06 Symantec Corporation Software object corruption detection
CN103955426A (en) * 2014-04-21 2014-07-30 中国科学院计算技术研究所 Method and device for detecting code C null-pointer reference
CN105607990A (en) * 2014-11-19 2016-05-25 腾讯科技(成都)有限公司 Null pointer crash mining method and device

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
RUBENYU: "List对象的空指针问题", 《HTTPS://WEB.ARCHIVE.ORG/WEB/20110928171226/HTTPS://BLOG.CSDN.NET/RUBENYU/ARTICLE/DETAILS/6599098》 *
易昕 等: "面向程序自动修复的缺陷分类方法研究", 《计算机应用研究》 *

Similar Documents

Publication Publication Date Title
US10191889B2 (en) Systems, apparatuses and methods for generating a user interface by performing computer vision and optical character recognition on a graphical representation
CN107423322B (en) Method and device for displaying label nesting hierarchy of webpage
KR101719278B1 (en) Deep learnig framework and image recognition method for content-based visual image recognition
CN108614707A (en) Static code inspection method, device, storage medium and computer equipment
Reiss Seeking the user interface
JP2009163742A (en) Object oriented method of structuring software step program
CN108108194B (en) User interface editing method and user interface editor
CN116070053B (en) Cross-platform RPA browser data acquisition method and system for information creation system
CN108874649B (en) Method and device for generating automatic test script and computer equipment thereof
CN109725896A (en) A kind of method and electronic equipment of online editing python code
CN106598615A (en) Recipe program code generation method and recipe compiling cloud platform system
Abdelhamid et al. Deep learning‐based prototyping of android GUI from hand‐drawn mockups
CN109753286A (en) A method of the code method based on functional label counts its call number
CN115562656A (en) Page generation method and device, storage medium and computer equipment
CN113704125A (en) Black box automatic testing method based on graphical user interface
CN113050935A (en) Method and device for generating mixed object, computing equipment and computer readable storage medium
CN119292953B (en) Software automatic testing method and system based on visual attention mechanism
CN119690807A (en) Large model assisted static code scanning result analysis method, device, electronic equipment and computer readable storage medium
CN118567999A (en) A test script generation method based on LLM
CN112487437B (en) Android counterfeit application detection method and device based on user interface characteristics
CN108062273A (en) A kind of optimization method and device of program
CN119066467A (en) An artificial intelligence-based building information model element classification method
CN116820996A (en) Automatic generation method and device for integrated test cases based on artificial intelligence
CN116775483A (en) Code review method and device
CN116610558A (en) Code detection method, device, electronic equipment and computer readable storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
RJ01 Rejection of invention patent application after publication

Application publication date: 20180522

RJ01 Rejection of invention patent application after publication