I
I
iscateli2022-01-30 15:05:04
Java
iscateli, 2022-01-30 15:05:04

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.

Full code example:
//: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;}
}///:~

where do they get them from? From annotations? Do annotations set values? In general, this chapter was written very heavily by Eckel, turned to Herbert Schildt "Java. Complete Guide", read a similar chapter about annotations, drew attention to this expression
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 keeps
the semantics of the program unchanged.
But 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.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Michael, 2022-01-30
@iscateli

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.

S
Sergey Gornostaev, 2022-01-30
@sergey-gornostaev

Annotations by themselves don't change anything. If you write Member member = new Member()then the firstNameinstance field memberwill 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.

O
Orkhan, 2022-01-30
Hasanly @azerphoenix

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.

Next, pay attention to the RetentionPolicy:

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.

For the annotation to "work" you need the so-called Analyzer (Processor)
Here is an example of a processor:
https://www.baeldung.com/java-annotation-processin...
https://thetechstack.net/how-to-process-annotation ...
https://cloudogu.com/en/blog/Java-Annotation-Proce...
@SQLString(30) String firstName;
The processor, using reflection, will access an instance of this class, then, after reading the data specified in the annotation, it will create the corresponding table with the appropriate columns according to the specified annotations

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question