CN116931939A - Attribute access method and device - Google Patents

Attribute access method and device Download PDF

Info

Publication number
CN116931939A
CN116931939A CN202210344558.6A CN202210344558A CN116931939A CN 116931939 A CN116931939 A CN 116931939A CN 202210344558 A CN202210344558 A CN 202210344558A CN 116931939 A CN116931939 A CN 116931939A
Authority
CN
China
Prior art keywords
access method
type
access
attribute
parameter
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
CN202210344558.6A
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.)
Beijing Zitiao Network Technology Co Ltd
Original Assignee
Beijing Zitiao Network Technology Co 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 Beijing Zitiao Network Technology Co Ltd filed Critical Beijing Zitiao Network Technology Co Ltd
Priority to CN202210344558.6A priority Critical patent/CN116931939A/en
Publication of CN116931939A publication Critical patent/CN116931939A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/70Software maintenance or management

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The embodiment of the invention provides a method and a device for accessing attributes, and relates to the technical field of software design. The method comprises the following steps: dynamically modifying the first attribute when the first attribute of the target class is declared, wherein the dynamic modification is used for indicating that a first access method for accessing the first attribute is not generated when the target class is compiled; compiling the target class to obtain a compiling result corresponding to the target class; detecting a call to the first access method when the compiling result is operated; when the call to the first access method is detected, a second access method corresponding to the first access method is acquired; and accessing the first attribute by calling the second access method. The embodiment of the invention is used for reducing the data quantity of codes corresponding to the attributes in the compiling result while ensuring that the compiling result can normally run.

Description

Attribute access method and device
Technical Field
The present invention relates to the field of software design technologies, and in particular, to a method and an apparatus for accessing an attribute.
Background
On the IOS mobile operation system, after an attribute (@ property) of a class is declared and compiled by a compiler, an instance variable of the attribute, a value method (get method) corresponding to the instance variable and a value method (set method) corresponding to the instance variable are automatically generated in a compiling result. As described above, the example variable, the value method, and the assignment method of the attribute are automatically generated in the compiling result, so the data amount of the code corresponding to the attribute is generally large.
Disclosure of Invention
In view of this, the present invention provides a method and apparatus for accessing an attribute, which are used to reduce the data size of codes corresponding to the attribute in the compiling result while ensuring that the compiling result can operate normally.
In order to achieve the above object, the embodiment of the present invention provides the following technical solutions:
in a first aspect, an embodiment of the present invention provides a method for accessing an attribute, including:
dynamically modifying a first attribute of a target class when the first attribute is declared, wherein the dynamic modification is used for indicating that a first access method for accessing the first attribute is not generated when the target class is compiled;
compiling the target class to obtain a compiling result corresponding to the target class;
detecting a call to the first access method when the compiling result is operated;
when the call to the first access method is detected, a second access method corresponding to the first access method is acquired;
and accessing the first attribute by calling the second access method.
As an optional implementation manner of the embodiment of the present invention, the detecting the call to the first access method includes:
Detecting target message forwarding, wherein the target message forwarding is message forwarding of the first access method;
when the target message forwarding is detected, it is determined that a call for the first access method is detected.
As an optional implementation manner of the embodiment of the present invention, the detecting the forwarding of the target message includes:
configuring the target class to inherit a preset base class;
implementing a dynamic analysis method in the preset base class; the dynamic analysis method is used for forwarding a message to the first access method if the first access method is not found in the target class or the parent class of the target class when the first access method is called;
and detecting the forwarding of the target message in the dynamic analysis method.
As an optional implementation manner of the embodiment of the present invention, the obtaining a second access method corresponding to the first access method includes:
determining a method type of the first access method, the method type comprising: a value method and a value method;
and acquiring the second access method according to the method type of the first access method.
As an optional implementation manner of the embodiment of the present invention, the obtaining the second access method according to the method type of the first access method includes:
Acquiring the type of the parameter of the first access method;
and acquiring the second access method according to the method type of the first access method and the type of the parameter of the first access method.
As an optional implementation manner of the embodiment of the present invention, the obtaining a type of the parameter of the first access method includes:
acquiring the name of the parameter of the first access method;
resolving the name of the parameter of the first access method, and acquiring a type character string of the parameter of the first access method;
and determining the type of the parameter of the first access method according to the type character string of the parameter of the first access method.
As an optional implementation manner of the embodiment of the present invention, the obtaining the second access method according to the method type of the first access method and the type of the parameter of the first access method includes:
when the method type of the first access method is the value method, acquiring the second access method according to the type of the parameter of the first access method and a first corresponding relation;
the first corresponding relation comprises the corresponding relation of each parameter type and the corresponding value method.
As an optional implementation manner of the embodiment of the present invention, before the second access method is obtained according to the type of the parameter of the first access method and the first correspondence, the method further includes:
and configuring the first corresponding relation and realizing each parameter type and corresponding value method in the first corresponding relation.
As an optional implementation manner of the embodiment of the present invention, the obtaining the second access method according to the method type of the first access method and the type of the parameter of the first access method includes:
when the method type of the first access method is the assignment method, acquiring the second access method according to the type of the parameter of the first access method and a second corresponding relation;
the second correspondence includes correspondence of each parameter type and a corresponding assignment method.
As an optional implementation manner of the embodiment of the present invention, before obtaining the second access method according to the type of the parameter of the first access method and the second correspondence, the method further includes:
and configuring the second corresponding relation and realizing each parameter type and corresponding assignment method in the second corresponding relation.
As an alternative implementation manner of the embodiment of the present invention, the dynamic modification is further used to indicate that the instance variable of the first attribute is not generated when compiling the target class.
In a second aspect, an embodiment of the present invention provides an access apparatus for an attribute, including:
a modification unit, configured to dynamically modify a first attribute of a target class when the first attribute is declared, where the dynamic modification is used to indicate that a first access method for accessing the first attribute is not generated when compiling the target class;
the compiling module is used for compiling the target class and obtaining a compiling result corresponding to the target class;
the detection module is used for detecting the call to the first access method when the compiling result is operated;
the acquisition module is used for acquiring a second access method corresponding to the first access method when detecting the call to the first access method;
and the access module is used for accessing the first attribute by calling the second access method.
As an optional implementation manner of the embodiment of the present invention, the detection module is specifically configured to detect a target message forwarding, where the target message forwarding is a message forwarding of the first access method; when the target message forwarding is detected, it is determined that a call to the first-genus access method is detected.
As an optional implementation manner of the embodiment of the present invention, the detection module is specifically configured to configure the target class to inherit a preset base class; a dynamic analysis method is realized in the preset base class; and detecting the forwarding of the target message in the dynamic analysis method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module is specifically configured to determine a method type of the first access method, where the method type includes: a value method and a value method; and acquiring the second access method according to the method type of the first access method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module is specifically configured to obtain a type of a parameter of the first access method; and acquiring the second access method according to the method type of the first access method and the type of the parameter of the first access method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module is specifically configured to obtain a name of a parameter of the first access method; resolving the name of the parameter of the first access method, and acquiring a type character string of the parameter of the first access method; and determining the type of the parameter of the first access method according to the type character string of the parameter of the first access method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module is specifically configured to obtain, when a method type of the first access method is the value method, the second access method according to a type of a parameter of the first access method and a first correspondence;
the first corresponding relation comprises the corresponding relation of each parameter type and a corresponding value method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module is further configured to configure the first corresponding relationship and implement each parameter type and corresponding value method in the first corresponding relationship before obtaining the second access method according to the type and the first corresponding relationship of the parameter of the first access method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module is specifically configured to obtain, when a method type of the first access method is the assignment method, the second access method according to a type of a parameter of the first access method and a second correspondence;
the second corresponding relation comprises the corresponding relation between each parameter type and a corresponding assignment method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module is further configured to configure the second corresponding relationship and implement each parameter type and corresponding assignment method in the second corresponding relationship before obtaining the second access method according to the type of the parameter of the first access method and the second corresponding relationship.
As an alternative implementation manner of the embodiment of the present invention, the dynamic modification is further used to indicate that the instance variable of the first attribute is not generated when compiling the target class.
In a third aspect, an embodiment of the present invention provides an electronic device, including: a memory and a processor, the memory for storing a computer program; the processor is configured to, when the computer program is called, cause the electronic device to implement the method for accessing the attribute according to the first aspect or any optional implementation manner of the first aspect.
In a fourth aspect, embodiments of the present invention provide a computer readable storage medium, which when executed by a computing device, causes the computing device to implement a method for accessing an attribute according to the first aspect or any optional implementation manner of the first aspect.
In a fifth aspect, embodiments of the present invention provide a computer program product, which when run on a computer causes the computer to implement a method of accessing properties according to the first aspect or any of the alternative embodiments of the first aspect.
According to the method for accessing the attribute, which is provided by the embodiment of the invention, when the first attribute of the target class is declared, the first attribute is dynamically modified, then the target class is compiled, and the compiling result corresponding to the target class is obtained. On the other hand, when the compiling result is operated, the embodiment of the invention detects the call to the first access method, and when the call to the first access method is detected, the embodiment of the invention obtains the second access method corresponding to the first access method, and accesses the first attribute by calling the second access method, so that the embodiment of the invention can also ensure the normal operation of the compiling result, and avoid system breakdown caused by accessing the instance variable of the first attribute by a value method or an assignment method. In summary, the embodiment of the invention can reduce the data volume of the codes corresponding to the attributes in the compiling result while ensuring that the compiling result can normally run.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and together with the description, serve to explain the principles of the invention.
In order to more clearly illustrate the embodiments of the invention or the technical solutions of the prior art, the drawings which are used in the description of the embodiments or the prior art will be briefly described, and it is obvious to a person skilled in the art that other drawings can be obtained from these drawings without inventive effort.
FIG. 1 is one of the flow charts of the steps of a method for accessing attributes provided by an embodiment of the present invention;
FIG. 2 is a second flowchart illustrating a method for accessing attributes according to an embodiment of the present invention;
FIG. 3 is a schematic diagram of an access device with attributes according to an embodiment of the present invention;
fig. 4 is a schematic hardware structure of an electronic device according to an embodiment of the present invention.
Detailed Description
In order that the above objects, features and advantages of the invention will be more clearly understood, a further description of the invention will be made. It should be noted that, without conflict, the embodiments of the present invention and features in the embodiments may be combined with each other.
In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present invention, but the present invention may be practiced otherwise than as described herein; it will be apparent that the embodiments in the specification are only some, but not all, embodiments of the invention.
It should be noted that, in order to clearly describe the technical solutions of the embodiments of the present invention, in the embodiments of the present invention, the terms "first", "second", and the like are used to distinguish the same item or similar items having substantially the same function and effect, and those skilled in the art will understand that the terms "first", "second", and the like are not limited in number and execution order. For example: the first access method and the second access method are only for distinguishing between different access methods, and are not limited in the order of access methods or the like.
In embodiments of the invention, words such as "exemplary" or "such as" are used to mean serving as an example, instance, or illustration. Any embodiment or design described herein as "exemplary" or "e.g." in an embodiment of the present invention is not to be taken as preferred or advantageous over other embodiments or designs. Rather, the use of words such as "exemplary" or "such as" is intended to present related concepts in a concrete fashion. Furthermore, in the description of the embodiments of the present invention, unless otherwise indicated, the meaning of "plurality" means two or more.
Because the attribute (@ property) of a class is declared and compiled by a compiler, the instance variable, the value method and the value method of the attribute are automatically generated in the compiling result, so that the data size of the code corresponding to the attribute is generally larger.
In order to reduce the data volume of the code corresponding to the attribute, the attribute can be dynamically modified by adding @ dynamic when the attribute of the class is declared. Under the condition that the attribute is dynamically modified by adding @ dynamic, the instance variable, the value method and the assignment method of the attribute are not automatically generated in the compiling result of the class, and the data quantity of codes corresponding to the attribute is further reduced.
After a class attribute is declared on the IOS and compiled by the compiler, the location, description and byte count occupied by the code corresponding to the attribute in the compiled result are shown in table 1 below:
TABLE 1
As shown in table 1 above, the number of bytes occupied by the code corresponding to one attribute is 179 bytes, and after the attribute is dynamically modified, the code corresponding to the attribute only includes: the _cstring section of the _TEXT section has myString and t@ "NSstring", &, D, N symbols, occupy 37 bytes; and the _cstring section of the _text section has myString and t@ "NSstring", &, D, N symbols, occupy 37 bytes; a total of 53 bytes are occupied, and thus, adding dynamic modifiers, the code amount of one attribute can be reduced by 179-53=126 bytes.
However, although the dynamic modification of the attribute can reduce the data volume of the code corresponding to the attribute, because the compiling result does not have the instance variable, the value method and the assignment method of the attribute, the system crash can be caused when the instance variable of the attribute is accessed through the value method or the assignment method.
In view of the above problems, an embodiment of the present invention provides a method for accessing an attribute, and referring to a step flowchart shown in fig. 1, the method for accessing an attribute provided by the embodiment of the present invention includes the following steps:
s11, dynamically modifying the first attribute of the target class when the first attribute is declared.
Wherein the dynamic modification is to indicate that a first access method for accessing the first attribute is not generated when compiling the target class.
Optionally, the dynamic modification is further configured to indicate that an instance variable of the first attribute is not generated when compiling the target class.
The dynamic modification in the above embodiment is further used for indicating that the instance variable of the first attribute is not generated when compiling the target class, so that the above embodiment can further reduce the data amount of the code corresponding to the first attribute in the compiling result of the target class.
Specifically, the main role of declaring attributes is: encapsulating the data in the object, thereby enabling quick and convenient creation of instance variables and creation of accessors for instance variables, and allowing access to accessors by point syntax, the nature of the declaration attribute is: the get method and the set method of the attribute are declared, the instance variable of the attribute is declared, and the get method and the set method of the attribute are implemented. At compile time, the compiler will generate instance variables, get methods, and setter methods for the properties, and the instance variables, get methods, and setter methods are generated by way of automatic synthesis (autosythesize) and added to the class. Under the condition of dynamically modifying the attribute, the compiling period can not automatically generate the instance variable, the get method and the set method of the attribute, so that the embodiment of the invention can dynamically modify the first attribute by utilizing the mechanism, thereby avoiding the instance variable, the get method and the set method of the first attribute from being contained in the compiling result, and reducing the data quantity of the code corresponding to the first attribute in the compiling result.
S12, compiling the target class to obtain a compiling result corresponding to the target class.
As described above, because the first attribute is dynamically modified when the first attribute is declared, the compiling result corresponding to the target class does not contain the instance variable, the get method and the get method of the first attribute, thereby reducing the data volume of the code corresponding to the first attribute.
S13, detecting the call to the first access method when the compiling result is operated.
Since the access method for accessing the first attribute includes: the value method (get method) and the assignment method (set method) are two methods, so the step S13 is: and detecting a value method of the first attribute or a value method of the first attribute in the process of running the compiling result.
S14, when the call to the first access method is detected, acquiring a second access method corresponding to the first access method.
Specifically, since the compiling result corresponding to the target class does not include the first access method for accessing the first attribute, a second access method capable of replacing the first access method to access the first attribute needs to be implemented in advance, and when a call to the first access method is detected, the access to the first attribute is implemented through the second access method, so as to avoid system crash.
S15, accessing the first attribute by calling the second access method.
Optionally, the implementation of step S15 (accessing the first attribute by calling the second access method) may include:
and adding a second access method into the running environment of the compiling result corresponding to the target class through a class-adding method (class_addmethod), and setting the method name of the second access method to be the same as the method name of the first access method, so that when the first access method is called, the second access method can be found and called in the running environment through the method name, and the first attribute is accessed through the second access method.
According to the method for accessing the attribute, which is provided by the embodiment of the invention, when the first attribute of the target class is declared, the first attribute is dynamically modified, then the target class is compiled, and the compiling result corresponding to the target class is obtained. On the other hand, when the compiling result is operated, the embodiment of the invention detects the call to the first access method, and when the call to the first access method is detected, the embodiment of the invention obtains the second access method corresponding to the first access method, and accesses the first attribute by calling the second access method, so that the embodiment of the invention can also ensure the normal operation of the compiling result, and avoid system breakdown caused by accessing the instance variable of the first attribute by a value method or an assignment method. In summary, the embodiment of the invention can reduce the data volume of the codes corresponding to the attributes in the compiling result while ensuring that the compiling result can normally run.
As an extension and refinement of the above embodiment, an embodiment of the present invention provides another attribute accessing method, and referring to fig. 2, the attribute accessing method includes the following steps:
s201, dynamically modifying a first attribute of a target class when the first attribute is declared.
Wherein the dynamic modification is used for indicating that when compiling the target class, an instance variable corresponding to the first attribute and a first access method for accessing the first attribute are not generated
S202, compiling the target class to obtain a compiling result corresponding to the target class.
S203, detecting the forwarding of the target message.
Wherein the target message forwarding is a message forwarding of the first access method.
S204, when the target message forwarding is detected, determining that the calling of the first access method is detected.
In Objective-C, when a method is sent to an object, the object first searches for whether the method exists in the class of the object (whether the method is implemented in the class of the object), if the implementation of the method is not found in the class (the method is not implemented in the class of the object), the method is further found in the parent class of the object (whether the method is implemented in the parent class of the object), if the method is not found in the parent class (the method is not implemented in the parent class of the object), a message forwarding mechanism is triggered to forward the method, so that the message forwarding of the unrealized method can be detected, and when the message forwarding of a certain method is detected, the call to the method is determined to be detected. Specifically, in the embodiment of the present invention, since the first access method does not exist in both the target class and the parent class of the target class (the first access method is not implemented), the message forwarding of the first access method can be necessarily detected when the first access method is invoked, so that the embodiment of the present invention can implement the detection of the invocation of the first access method through the steps S202 and S203 described above.
As an optional implementation manner of the embodiment of the present invention, the step S203 (detecting the forwarding of the target message) includes the following steps a to c:
and a step a of configuring the target class to inherit a preset base class.
And b, implementing a dynamic analysis method in the preset base class.
And when the first access method is called, if the first access method is not found in the target class or the parent class of the target class, forwarding the message to the first access method.
And c, detecting the forwarding of the target message in the dynamic analysis method.
That is, it is possible to detect the message forwarding of the first access method in a dynamic parsing method by configuring the target class to inherit a base class and rewriting a dynamic parsing method (resolvelnstancemethod) in the base class.
S205, determining the method type of the first access method.
Wherein the method types include: a value method (a setter method) and a value method (a setter method).
That is, it is determined whether the first access method is a value method of the first attribute or an assignment method of the first attribute.
Specifically, the first access method may be judged by a selector (selector) as the value method of the first attribute or the value method of the first attribute.
S206, acquiring the type of the parameter of the first access method.
The types of the parameters of the first access method include: OC type (string), class type, C string type, C pointer type, boolean type, unsigned character type, double type, float type, unsigned int type, unsigned long type under 32 bits, unsigned long type under 64 bits, unsigned short type, short type.
As an optional manner of the embodiment of the present invention, the step S206 (the type of obtaining the parameter of the first access method) includes the following step 1 value step 3:
step 1, acquiring names of parameters of the first access method.
And 2, analyzing the name of the parameter of the first access method, and acquiring a type character string (Property Type String) of the parameter of the first access method.
And step 3, determining the type of the parameter of the first access method according to the type character string of the parameter of the first access method.
S207, acquiring the second access method according to the method type of the first access method and the type of the parameter of the first access method.
As an optional implementation manner of the embodiment of the present invention, the step S207 (obtaining the second access method according to the method type of the first access method and the type of the parameter of the first access method) includes:
when the method type of the first access method is the value method, acquiring the second access method according to the type of the parameter of the first access method and a first corresponding relation;
the first corresponding relation comprises the corresponding relation of each parameter type and a corresponding value method.
Illustratively, the type of parameter of the first access method may be: types such as OC type, class type, C string, C pointer, base data type, structure type, etc.
For example, when the first access method is the value method and the type of the parameter of the first access method is OC type, the second access method may be as follows:
for example, when the first access method is the value method and the type of the parameter of the first access method is a Class type or a C string or a C pointer, the Class type or the C string or the C pointer may be the same as the corresponding second method because the nature of the Class type is a pointer of the obj_class structure.
Further, before the second access method is obtained according to the type of the parameter and the first correspondence of the first access method, the access method of the attribute provided by the embodiment of the invention further includes:
and configuring the first corresponding relation and realizing each parameter type and corresponding value method in the first corresponding relation.
As an optional manner of the embodiment of the present invention, the step S207 (obtaining the second access method according to the method type of the first access method and the type of the parameter of the first access method) includes:
when the method type of the first access method is the assignment method, acquiring the second access method according to the type of the parameter of the first access method and a second corresponding relation;
the second corresponding relation comprises the corresponding relation between each parameter type and a corresponding assignment method.
Illustratively, the type of parameter of the first access method may be: types such as OC type, class type, C string, C pointer, base data type, structure type, etc.
For example, when the first access method is the assignment method and the type of the parameter of the first access method is OC type, the second access method may be as follows:
Further, before the second access method is obtained according to the type of the parameter of the first access method and the second correspondence, the access method of the attribute provided by the embodiment of the invention further includes:
and configuring the second corresponding relation and realizing each parameter type and corresponding assignment method in the second corresponding relation.
According to the method for accessing the attribute, which is provided by the embodiment of the invention, when the first attribute of the target class is declared, the first attribute is dynamically modified, then the target class is compiled, and the compiling result corresponding to the target class is obtained. On the other hand, when the compiling result is operated, the embodiment of the invention detects the call to the first access method, and when the call to the first access method is detected, the embodiment of the invention obtains the second access method corresponding to the first access method, and accesses the first attribute by calling the second access method, so that the embodiment of the invention can also ensure the normal operation of the compiling result, and avoid system breakdown caused by accessing the instance variable of the first attribute by a value method or an assignment method. In summary, the embodiment of the invention can reduce the data volume of the codes corresponding to the attributes in the compiling result while ensuring that the compiling result can normally run.
Because the first attribute is dynamically modified, the compiling result does not include the instance variable corresponding to the first attribute, and therefore, reducing the data size of the code corresponding to the attribute in the compiling result also causes the instance variable of the first attribute to be invisible. Aiming at the problem: a method for outputting the instance variable of the attribute can be implemented in the base class of the target class and the instance variable of the first attribute is output by the method when the instance variable of the first attribute needs to be checked. The method for outputting instance variables of the attribute may be a dynamic logallivars method, for example.
Furthermore, in terms of performance, by implementation we find: 100 sample classes, a total of 1000 attributes with dynamic modification, compared with the attributes without dynamic modification, the access time of the attributes is increased by 0.4666ms, the increase percentage is 2%, and the actual time increment is only 0.4666ms, so that the influence on the destructiveness is almost negligible.
Based on the same inventive concept, as an implementation of the above method, the embodiment of the present invention further provides an attribute accessing device, where the embodiment of the device corresponds to the embodiment of the foregoing method, and for convenience of reading, the embodiment of the present invention does not further describe details in the embodiment of the foregoing method one by one, but it should be clear that the attribute accessing device in the embodiment can correspondingly implement all the details in the embodiment of the foregoing method.
An embodiment of the present invention provides an attribute accessing device, fig. 3 is a schematic structural diagram of the attribute accessing device, and as shown in fig. 3, an attribute accessing device 300 includes:
a modification unit 31, configured to dynamically modify a first attribute of a target class when the first attribute is declared, where the dynamic modification is used to indicate that a first access method for accessing the first attribute is not generated when compiling the target class;
the compiling module 32 is configured to compile the target class, and obtain a compiling result corresponding to the target class;
a detection module 33, configured to detect a call to the first access method when the compiling result is executed;
an obtaining module 34, configured to obtain a second access method corresponding to the first access method when a call to the first access method is detected;
and an access module 35, configured to access the first attribute by calling the second access method.
As an optional implementation manner of the embodiment of the present invention, the detection module 33 is specifically configured to detect a target message forwarding, where the target message forwarding is a message forwarding of the first access method; when the target message forwarding is detected, it is determined that the call to the first access method is detected.
As an optional implementation manner of the embodiment of the present invention, the detection module 33 is specifically configured to configure the target class to inherit a preset base class; implementing a dynamic analysis method in the preset base class; detecting the forwarding of the target message in the dynamic analysis method;
and when the first access method is called, if the first access method is not found in the target class or the parent class of the target class, forwarding the message to the first access method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module 34 is specifically configured to determine a method type of the first access method, where the method type includes: a value method and a value method; and acquiring the second access method according to the method type of the first access method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module 34 is specifically configured to obtain a type of the parameter of the first access method; and acquiring the second access method according to the method type of the first access method and the type of the parameter of the first access method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module 34 is specifically configured to obtain a name of a parameter of the first access method; resolving the name of the parameter of the first access method, and acquiring a type character string of the parameter of the first access method; and determining the type of the parameter of the first access method according to the type character string of the parameter of the first access method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module 34 is specifically configured to obtain, when the method type of the first access method is the value method, the second access method according to the type of the parameter of the first access method and the first correspondence;
the first corresponding relation comprises the corresponding relation of each parameter type and a corresponding value method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module 34 is further configured to configure the first correspondence before obtaining the second access method according to the type of the parameter of the first access method and the first correspondence, and implement each parameter type and corresponding value method in the first correspondence.
As an optional implementation manner of the embodiment of the present invention, the obtaining module 34 is specifically configured to obtain, when the method type of the first access method is the assignment method, the second access method according to the type of the parameter of the first access method and a second correspondence;
the second corresponding relation comprises the corresponding relation between each parameter type and a corresponding assignment method.
As an optional implementation manner of the embodiment of the present invention, the obtaining module 34 is further configured to configure the second corresponding relationship and implement each parameter type and corresponding assignment method in the second corresponding relationship before obtaining the second access method according to the type of the parameter of the first access method and the second corresponding relationship.
The attribute accessing device provided in this embodiment may execute the attribute accessing method provided in the foregoing method embodiment, and its implementation principle is similar to that of the technical effect, and will not be described herein.
Based on the same inventive concept, the embodiment of the invention also provides electronic equipment. Fig. 4 is a schematic structural diagram of an electronic device according to an embodiment of the present invention, as shown in fig. 4, where the electronic device provided in this embodiment includes: a memory 401 and a processor 402, said memory 401 being for storing a computer program; the processor 402 is configured to execute the method for accessing the attribute provided in the above embodiment when the computer program is invoked.
Based on the same inventive concept, the embodiments of the present invention further provide a computer readable storage medium, on which a computer program is stored, which when executed by a processor, causes the computing device to implement the method for accessing the attribute provided in the above embodiments.
Based on the same inventive concept, embodiments of the present invention also provide a computer program product, which when run on a computer, causes the computing device to implement the method of accessing the attributes provided by the above embodiments.
It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the invention may take the form of a computer program product embodied on one or more computer-usable storage media having computer-usable program code embodied therein.
The processor may be a central processing unit (CentralProcessingUnit, CPU), but may also be other general purpose processors, digital signal processors (DigitalSignalProcessor, DSP), application specific integrated circuits (ApplicationSpecificIntegratedCircuit, ASIC), off-the-shelf programmable gate arrays (Field-ProgrammableGateArray, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or the like. The general purpose processor may be a micro processor or the processor may be any conventional processor or the like.
The memory may include volatile memory in a computer-readable medium, random Access Memory (RAM) and/or nonvolatile memory, etc., such as Read Only Memory (ROM) or flash memory (flashRAM). Memory is an example of a computer-readable medium.
Computer readable media include both non-transitory and non-transitory, removable and non-removable storage media. Storage media may embody any method or technology for storage of information, which may be computer readable instructions, data structures, program modules, or other data. Examples of storage media for a computer include, but are not limited to, phase change memory (PRAM), static Random Access Memory (SRAM), dynamic Random Access Memory (DRAM), other types of Random Access Memory (RAM), read Only Memory (ROM), electrically Erasable Programmable Read Only Memory (EEPROM), flash memory or other memory technology, compact disc read only memory (CD-ROM), digital Versatile Discs (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices, or any other non-transmission medium, which can be used to store information that can be accessed by the computing device. Computer-readable media, as defined herein, does not include transitory computer-readable media (transshipment) such as modulated data signals and carrier waves.
Finally, it should be noted that: the above embodiments are only for illustrating the technical solution of the present invention, and not for limiting the same; although the invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical scheme described in the foregoing embodiments can be modified or some or all of the technical features thereof can be replaced equivalently; such modifications and substitutions do not depart from the spirit of the invention.

Claims (15)

1. A method for accessing an attribute, comprising:
dynamically modifying a first attribute of a target class when the first attribute is declared, wherein the dynamic modification is used for indicating that a first access method for accessing the first attribute is not generated when the target class is compiled;
compiling the target class to obtain a compiling result corresponding to the target class;
detecting a call to the first access method when the compiling result is operated;
when the call to the first access method is detected, a second access method corresponding to the first access method is acquired;
And accessing the first attribute by calling the second access method.
2. The method of claim 1, wherein the detecting the call to the first access method comprises:
detecting target message forwarding, wherein the target message forwarding is the message forwarding of the first access method;
when the target message forwarding is detected, it is determined that the call to the first access method is detected.
3. The method of claim 2, wherein detecting the forwarding of the target message comprises:
configuring the target class to inherit a preset base class;
implementing a dynamic analysis method in the preset base class; the dynamic analysis method is used for forwarding a message to the first access method if the first access method is not found in the target class or the parent class of the target class when the first access method is called;
and detecting the forwarding of the target message in the dynamic analysis method.
4. The method of claim 1, wherein the obtaining a second access method corresponding to the first access method comprises:
determining a method type of the first access method, the method type comprising: a value method and a value method;
And acquiring the second access method according to the method type of the first access method.
5. The method of claim 4, wherein the obtaining the second access method according to the method type of the first access method comprises:
acquiring the type of the parameter of the first access method;
and acquiring the second access method according to the method type of the first access method and the type of the parameter of the first access method.
6. The method of claim 5, wherein the obtaining the type of parameter of the first access method comprises:
acquiring the name of the parameter of the first access method;
resolving the name of the parameter of the first access method, and acquiring a type character string of the parameter of the first access method;
and determining the type of the parameter of the first access method according to the type character string of the parameter of the first access method.
7. The method of claim 5, wherein the obtaining the second access method according to the method type of the first access method and the type of the parameter of the first access method comprises:
when the method type of the first access method is the value method, acquiring the second access method according to the type of the parameter of the first access method and a first corresponding relation;
The first corresponding relation comprises the corresponding relation of each parameter type and each value method.
8. The method of claim 7, wherein prior to obtaining the second access method based on the type of parameter and the first correspondence of the first access method, the method further comprises:
and configuring the first corresponding relation and realizing each parameter type and corresponding value method in the first corresponding relation.
9. The method of claim 5, wherein the obtaining the second access method according to the method type of the first access method and the type of the parameter of the first access method comprises:
when the method type of the first access method is the assignment method, acquiring the second access method according to the type of the parameter of the first access method and a second corresponding relation;
the second corresponding relation comprises corresponding relations of each parameter type and each assignment method.
10. The method of claim 9, wherein prior to obtaining the second access method based on the type of parameter and the second correspondence of the first access method, the method further comprises:
And configuring the second corresponding relation and realizing each parameter type and corresponding assignment method in the second corresponding relation.
11. The method of any of claims 1-10, wherein the dynamic modification is further to indicate that instance variables of the first attribute are not generated when compiling the target class.
12. An access device for an attribute, comprising:
a modification unit, configured to dynamically modify a first attribute of a target class when the first attribute is declared, where the dynamic modification is used to indicate that a first access method for accessing the first attribute is not generated when compiling the target class;
the compiling module is used for compiling the target class and obtaining a compiling result corresponding to the target class;
the detection module is used for detecting the call to the first access method when the compiling result is operated;
the acquisition module is used for acquiring a second access method corresponding to the first access method when detecting the call to the first access method;
and the access module is used for accessing the first attribute by calling the second access method.
13. An electronic device, comprising: a memory and a processor, the memory for storing a computer program; the processor is configured to cause the electronic device to implement the method of accessing properties of any of claims 1-11 when executing the computer program.
14. A computer readable storage medium, characterized in that the computer readable storage medium has stored thereon a computer program which, when executed by a computing device, causes the computing device to implement the method of accessing the properties of any of claims 1-11.
15. A computer program product, characterized in that the computer program product, when run on a computer, causes the computer to implement the method of accessing properties according to any of claims 1-11.
CN202210344558.6A 2022-03-31 2022-03-31 Attribute access method and device Pending CN116931939A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210344558.6A CN116931939A (en) 2022-03-31 2022-03-31 Attribute access method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210344558.6A CN116931939A (en) 2022-03-31 2022-03-31 Attribute access method and device

Publications (1)

Publication Number Publication Date
CN116931939A true CN116931939A (en) 2023-10-24

Family

ID=88377775

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210344558.6A Pending CN116931939A (en) 2022-03-31 2022-03-31 Attribute access method and device

Country Status (1)

Country Link
CN (1) CN116931939A (en)

Similar Documents

Publication Publication Date Title
US10908925B2 (en) Dynamic loading method, and target file creation method and apparatus
CN105630463B (en) For detecting the method and device of JAR packet conflict
CN110007920B (en) Method and device for acquiring code dependency relationship and electronic equipment
CN107786630B (en) Web application package processing method, device and equipment
CN112733158B (en) Android system vulnerability detection method, electronic equipment and storage medium
CN112631684B (en) Executable program running method and device, electronic equipment and computer storage medium
CN108595246B (en) Method, device and equipment for running application
CN110084042B (en) Application program stack static analysis method and system
CN116680015B (en) Function calling method, function calling device, electronic equipment and readable storage medium
CN111240761B (en) Configuration file loading method and device
CN112307509A (en) Desensitization processing method, equipment, medium and electronic equipment
CN111506500B (en) Memory leakage detection method and device, electronic equipment and readable storage medium
CN114840427A (en) Code testing and test case generating method and device
CN109032572B (en) Method for Java program method internal connection based on byte code
CN109582300A (en) Code analysis on altered project method, device and equipment based on path
CN110941443B (en) Method and device for modifying file name in SDK (software development kit) and electronic equipment
CN117032719A (en) Static compiling method and device for TypeScript
CN107301097B (en) Method and device for storing calling java object and reference address information of java object
CN116931939A (en) Attribute access method and device
CN112148746A (en) Method and device for generating database table structure document, electronic device and storage medium
CN115618363B (en) Vulnerability path mining method and related equipment
CN109144877B (en) Application detection method and device
CN110275710B (en) Java local interface consistency checking method and system, storage medium and terminal
CN110750271B (en) Service aggregation, method and device for executing aggregated service and electronic equipment
CN111782533A (en) Test method, test device, electronic equipment and 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