Testing, reflection, annotations conceptual knowledge

Test classification:
1, white box: the need to write code. Specific attention program execution flow
2, black box testing: no need to write code to input values, to see whether the program can output the desired value

Junit use: white box

  • Step
    1, a test class definitions (test)
    2, define test methods: can run independently
    3, a method to add @Test
    . 4, introduced environment-dependent Junit

The judgment result:
red: Failure
Green: successful
(it plainly, we generally do not see the validity of the results of the program to determine whether the bug, but in order to output the correct result, we generally will use assertions way to deal with the results)
assert: (a desired result, the result of the operation) Assert.assertEquals;

@Before: initialization method for the application of resources, it will first perform the method before all testing methods and then executed.
@After: release of resources method, after the implementation in all the test methods will automatically execute the method

Reflection: Design frame Soul

  • Frame: semi-finished software. Software can be developed on the base frame, to simplify the code

  • Reflection: The various components of the package as the other objects of the class, which is the reflection
    benefits:
    1, during a program's operation, the operation of these objects.
    2, can be decoupled, improved scalability program.

  • Get Class object ways:
    . 1, class.forName ( "full class name"): the bytecode file loaded into memory, the object return Class
    (used for configuration files, the class name is defined in the configuration file to read the file. loading class)
    2, class name .class: property class obtained by class name
    (used for passing parameters)
    3, the object .getClass (): getClass () methods defined in the class Object.
    (Get bytecode manner used for object)

Conclusion: The same bytecode files during a program is running, it will only be loaded once, either by acquiring Class object which way are the same.

Common acquisition function:
1, access to member variables:

Filed[] getFileds():
Filed getFiled(String name)

Filed[] getDeclaredFileds()
Filed getDeclaredFiled(String name)

Filed member variable operation:
1, set the value: void the SET (Object Object, Object value)
2, get the value: GET (Object obj)
3, ignored access modifier security check: setAccessible (true): Violence reflection


2, obtaining the constructor:

Constructor<?>[] getConstructor():获取所有public修饰的成员变量
Constructor<T> getConstructor(Class<?>... parameterTypes):获取指定名称的public修饰的成员变量

Constructor<?>[] getDeclaredConstructor():获取所有成员变量,不考虑修饰符
Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)

3, members get method:

Method<?>[] getMethods()
Method<T> getMethod(Class<?>... parameterTypes)

Method<?>[] getDeclaredMethods()
Method<T> getDeclaredMethod(Class<?>... parameterTypes)

4. Obtaining Class Names

String getName()

Here Insert Picture Description


  • Case:
    Demand: Write a "framework" can not be changed under any premise of the class code, can help us to create any type of object and execute any of these methods.
    Implementation:
    1, the profile
    2, reflecting
    the steps of:
    1, the full class name of the object to be created and the method to be executed is defined in a configuration file
    2, the program is loaded reads the configuration file
    3, using the reflection technique to load class files into the memory
    4, the object is created
    5, execution method

Note:
The concept: description of the process, to look at the computer
Notes: The text description of the program, the programmer to see

Use Notes: @ annotation name (such as @Override)

Role Category:
1, the preparation of the document: by annotating document generation code identifies the [generation doc documentation]
2, code analysis: analysis of the code by annotating code identified [using reflection]
3, the compiler checks: by code identified annotations allow the compiler to achieve the basic compiler checks [Override]

Published 52 original articles · won praise 0 · Views 4279

Guess you like

Origin blog.csdn.net/qq_43616001/article/details/104107237