Answer the question
In order to leave comments, you need to log in
Do annotations change data in the code?
I read Bruce Eckel's "Java Philosophy", a section on annotations, which says that annotations themselves and we either mark something in the code with them or store additional information in them that does not affect the code in any way. And then all of a sudden on page 857 it says
The firstName and lastName properties are annotated with
@SQLString and set to 30 and 50 respectively.
//:annotations/database/SQLString.java
package annotations.database;
import java.lang.annotation.*;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQLString{
intvalue() default 0;
Stringname() default "";
Constraints constraints() default @Constraints;
}///
//:annotations/database/Member.java
package annotations.database;
@DBTable(name="MEMBER")
public class Member{
@SQLString(30) String firstName;
@SQLString(50) String lastName;
@SQLInteger Integer age;
@SQLString(value=30,
constraints = @Constraints(primaryKey=true))
String handle;
static int memberCount;
public String getHandle(){return handle;}
public String getFirstName(){return firstName;}
public String getLastName(){return lastName;}
public String toString(){return handle;}
public Integer getAge(){return age;}
}///:~
The Java language provides a language facility that allows you to embed help information in source files. This information is called an annotation and does not change the order in which the program is executed. This means that the annotation keepsBut Eckel's annotations change the values in the program code, and this can affect the order in which the program is executed, I'm completely confused, please explain.
the semantics of the program unchanged.
Answer the question
In order to leave comments, you need to log in
Let's go in order.
What is an annotation? An annotation is some arbitrary metadata that a programmer can attach to some object. Only. Data, nothing more. The annotation itself does not add any behavior.
So the annotation does nothing? Yes exactly. By itself, an annotation does nothing.
Then why is it needed at all? In order for an annotation to have any effect on a program, an annotation processor is needed. There are two types of annotation processor: the first one works during program compilation. And annotations affect the compilation process. This may affect the final code of the program, or change the logic of the compiler, for example, add some additional checks.
The second kind of annotation processor works during program execution. Through the reflection mechanism, it analyzes the annotations that the programmer has added and executes the logic in accordance with the annotations and the data contained in them. So, for example, Spring works, which, in accordance with the annotations @Service, @Scope, @Autowired, @Controller и т.п.
, initializes the application components and launches it in the desired configuration.
Eckel wrote inaccurately (or this is an inaccurate translation, this also often happens). Variables do not receive any value, the field in the annotation receives the value (annotation in Java is also an object and also has fields). And then the annotation processor somehow uses the value of this field (in the given example, it sets a correspondence with a column in the database table of the corresponding type and, if necessary, creates it).
Schildt wrote more correctly - annotations themselves do not affect the semantics of the program in any way, in order for them to work, additional code is required.
Annotations by themselves don't change anything. If you write Member member = new Member()
then the firstName
instance field member
will be null, not 30. Some other code is needed that will process the annotations and perform some actions in accordance with them. Annotations are usually handled by frameworks, the compiler, or its plugins.
Good afternoon.
First of all, you should probably pay attention to this:
Annotations are some metadata that can be added to the source code of the program and do not semantically affect it, but can be used during code analysis, compilation, and even at runtime.
RetentionPolicy.SOURCE - the annotation is used at compile time and should be discarded by the compiler;
RetentionPolicy.CLASS - the annotation will be written to the class file by the compiler, but should not be available at runtime;
RetentionPolicy.RUNTIME - the annotation will be written to the class file and available at runtime via reflection.
@SQLString(30) String firstName;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question