标签: annotations

如何在Java中获取字段的注释

我有自己的注解

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Template {
   Class target();
}
Run Code Online (Sandbox Code Playgroud)

这个注解用在一个简单的 POJO 中:

public class Something {   
    @JsonSerialize(using = TemplateSerializer.class)
    @Template(target = PersonRepresentation.class)
    private TemplateFoo address = new TemplateFoo() {};
}
Run Code Online (Sandbox Code Playgroud)

我有 Jackson seriliazer TemplateSerializer,它在将对象序列化为 JSON 时传递“地址”。

我想知道如何在给定“地址”实例的情况下获得 @Template 注释?我想获取它的“目标”字段,然后检查 PersonRepresentation.class

java reflection serialization annotations jackson

1
推荐指数
1
解决办法
2299
查看次数

无法从方法中获取@override 注释

当我从class实例中获取方法并想要获取@override注释时。但是方法没有任何注释。获得@override 注解是不可能的吗?

代码如下。

package com.test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import javax.annotation.Resource;

public class ReflectionTest {

    public static void main(String[] args) throws Exception {
        ChildHoge childHoge = new ChildHoge();
        Method method = childHoge.getClass().getMethod("init");
        for (Annotation s : method.getAnnotations()) {
            System.out.println(s);
        }
        Method method2 = childHoge.getClass().getMethod("a");
        for (Annotation a : method2.getAnnotations()) {
            System.out.println(a); // =>@javax.annotation.Resource(mappedName=, shareable=true, type=class java.lang.Object, authenticationType=CONTAINER, lookup=, description=, name=)

        }
    }

}

class SuperHoge {
    public void init() {

    }
}


class ChildHoge extends …
Run Code Online (Sandbox Code Playgroud)

java annotations

1
推荐指数
1
解决办法
372
查看次数

Byte Buddy 可以在运行时创建字段和方法注释吗?

我想实现这个 3rd-party 注释以将我的类的字段/属性映射到我的数据库表列。我可以在编译时轻松实现注释(如下面的示例代码所示),但我找不到在运行时执行此操作的方法。(我在运行时使用反射加载库。)

我的问题是如何在运行时加载库时实现相同的映射注释?Byte Buddy 可以为 Android 处理这个吗?

//3rd party annotation code
package weborb.service;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface MapToProperty {
    String property();
}
Run Code Online (Sandbox Code Playgroud)

///////////////////////////////////////////////// ///

//Here is the implementation using non-reflection
import weborb.service;
    Class Person
    {
        @MapToProperty(property="Person_Name")
        String name;

        @MapToProperty(property="Person_Age")
        int age;

        @MaptoProperty(property="Person_Name")
        public String getName()
        {
            return this.name;
        }

        @MaptoProperty(property="Person_Name")
        public void setName(String name)
        {
            this.name = name;
        }

        @MaptoProperty(property="Person_Age")
        public int getAge()
        {
             return this.age;
        }

        @MaptoProperty(property="Person_Age")
        public void …
Run Code Online (Sandbox Code Playgroud)

java android annotations runtime byte-buddy

1
推荐指数
1
解决办法
2515
查看次数

如何通过注释获取类的字段

我创建了一个简单的注释类:

@Retention(RUNTIME)
public @interface Column {
    public String name();
}
Run Code Online (Sandbox Code Playgroud)

我在这样的一些类中使用它:

public class FgnPzt extends Point {

    public static final String COLUMN_TYPE = "type";
    @Column(name=COLUMN_TYPE)
    protected String type;

}
Run Code Online (Sandbox Code Playgroud)

我知道我可以遍历声明的字段并获得这样的注释:

for (Field field : current.getDeclaredFields()) {
    try {
        Column c = field.getAnnotation(Column.class);
        [...]
    } catch(Exception e) {
        [...]
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在type不迭代类的声明字段的情况下直接通过其注释名称获取字段?

java annotations

1
推荐指数
1
解决办法
2574
查看次数

Swift 3 - MapKit 注释触摸侦听器

我到处搜索,但我找不到它。我想下面的动作。

当我触摸地图上的注释时,我想更改视图上的文本。

我试过下面的代码,但这不起作用。单击注释图钉时,我只需更改屏幕上的文本。

private func mapView(mapView: MKMapView, didSelect view: MKAnnotationView{
    hastane_adi_text.text = "HAstane"
} 
Run Code Online (Sandbox Code Playgroud)

你可以在下面看到我的“ViewControllerClass”。

import UIKit
import MapKit
import CoreLocation

class HospitalControlloer: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{

    @IBOutlet weak var hastane_adi_text: UILabel!

    @IBOutlet weak var map: MKMapView!

    @IBOutlet weak var randevu_al_button: UIButton!
    @IBOutlet weak var hizala_button: UIButton!

    let locationMenager = CLLocationManager()


    override var preferredStatusBarStyle: UIStatusBarStyle {

        return .lightContent

    }


    override func viewDidLoad() {
        super.viewDidLoad()


        randevu_al_button.layer.cornerRadius = 10;
        hizala_button.layer.cornerRadius = 10;



        let locationS:CLLocationCoordinate2D = CLLocationCoordinate2DMake(41.169425, 29.056801)

        let sd = MKPointAnnotation()

        sd.coordinate = …
Run Code Online (Sandbox Code Playgroud)

annotations mapkit ios swift3

1
推荐指数
1
解决办法
2832
查看次数

休眠@Audited:NOT_AUDITED 无法解析为变量

我在数据库中的表上创建休眠实体。

实体 A 引用实体 B

@Entity
@Table(name="TABLE_A")
@NamedQuery(.. query="SELECT n FROM EntityA n")
public class EntityA {
....
@ManyToOne(...)
@JoinColumn(...)
private EntityB b;


@Entity
@Table(name ="TABLE_B")
@NamedQuery(.. query="SELECT n FROM EntityB n")
public class EntityB {
...
Run Code Online (Sandbox Code Playgroud)

唯一的问题是 EntityA 用@Audited注解 ( org.hibernate.envers.Audited)标记,而 EntityB 没有。

发布应用程序时,我在堆栈跟踪中收到以下错误:

引起:org.hibernate.MappingException:从 EntityA 到未审计实体 EntityB 的审计关系!这种映射是可能的,但必须使用@Audited(targetAuditMode = NOT_AUDITED) 明确定义。

如果我在@Audited(targetAuditMode = NOT_AUDITED)上面添加private EntityB b,Eclipse 会给我以下错误

NOT_AUDITED 无法解析为变量

我怎么解决这个问题?

java annotations hibernate hibernate-mapping

1
推荐指数
1
解决办法
5833
查看次数

如何使用 lombok 在 @NonNull 异常中抛出我自己的自定义消息?

我开始使用lombok项目,我有一个疑问。让我们假设我有一个带有一个参数的方法method(@NonNull arg)。如果我调用该方法使用了空参数method(null),我会得到以下异常:java.lang.NullPointerException: arg

但是让我们假设我希望异常消息说的是类似的东西arg cannot be null而不是另一个(无论该消息的含义如何,我只想知道如何使用 @NonNull 注释自定义异常消息)。

谢谢!

java annotations lombok

1
推荐指数
2
解决办法
5872
查看次数

php swagger注释json属性中方括号的转义字符

我在 Php Laravel 中编写 API 并使用 swagger (2.0) 注释(lib:darkaonline/l5-swagger使用swagger-php)来生成 swagger.json,但是我有以下问题 - 当我输入时:

/**
 *
 * Space Schema
 *
 * @SWG\Get(
 *     path="/api/v1/client/space/schema",
 *     @SWG\Response( 
 *        response=200, 
 *        description="OK",
 *        @SWG\Property(property="result", type="json", example={ "aa": [ "bb", "cc" ] }  )
 *      )
 * )
Run Code Online (Sandbox Code Playgroud)

并尝试生成 swagger.json 我得到:

[Syntax Error] Expected PlainValue, got '[' in ...
Run Code Online (Sandbox Code Playgroud)

但是当我不使用方括号时,例如:

@SWG\Property(property="result", type="json", example={ "ee": "ff" })
Run Code Online (Sandbox Code Playgroud)

然后一切都很好。但是我需要使用方括号所以问题是:

[swagger注释中json字符串中(方括号)的转义字符是什么?

我还想补充一点,我的示例 json 非常大且复杂

php json annotations swagger-php swagger-2.0

1
推荐指数
1
解决办法
1448
查看次数

Python Django:用数据库字段注释同名

我需要注释一些必须命名为“id”的东西,它与表“id”的主键相同。最终输出应该是一个字典数组。我知道我可以使用另一个名称(如“id_for_tree”)进行注释,然后在使用之后values()制作字典并在循环中更改键。就像是:

item_list = MyTable.objects.annotate(id_for_tree=Concat(F('id'), F('Code'), output_field=CharField())).values('Name', 'id_for_tree')
for any_item in item_list:
    any_item['id'] = any_item.pop('id_for_tree')
Run Code Online (Sandbox Code Playgroud)

很明显,这个解决方案没有很好的性能。我怎样才能摆脱

“注释‘id’与模型上的字段冲突”

通过丢弃以前的异常'id'并注释新的'id'?

python django orm annotations

1
推荐指数
1
解决办法
3165
查看次数

Spring AOP中如何拦截元注解(annotated annotations)

假设我想找到所有用@Controller 注释的类,我会创建这个切入点:

    @Pointcut("within(@org.springframework.stereotype.Controller *)")
    public void controllerPointcut() {}
Run Code Online (Sandbox Code Playgroud)

但是找不到那些用@RestController 注释的控制器。因为 RestController 本身是用@Controller 注释的。

关于如何查找使用 @Controller 或 @RestController 注释的类而无需创建两个 Pointcuts 的任何想法?


======编辑====我在这里的真正意图如下:

父注释:

public @interface ParentAnnotation {}
Run Code Online (Sandbox Code Playgroud)

子注释(用@ParentAnnotation 注释):

@ParentAnnotation 
public @interface ChildAnnotation {}
Run Code Online (Sandbox Code Playgroud)

A类:

@ParentAnnotation 
public class MyClassA {}
Run Code Online (Sandbox Code Playgroud)

B类:

@ChildAnnotation 
public class MyClassB {}
Run Code Online (Sandbox Code Playgroud)

现在我想通过@ParentAnnotation 找到MyClassA 和MyClassB。找到MyClassA是没有问题的,但是MyClassB是用@ParentAnnotation间接注释的,有没有通用的方法来处理这种情况?

spring annotations aspectj spring-aop

1
推荐指数
1
解决办法
1294
查看次数