IntelliJ IDEA + AspectJ [英] IntelliJ IDEA + AspectJ

查看:891
本文介绍了IntelliJ IDEA + AspectJ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在IntelliJ IDEA中的示例项目中使用AspectJ。我有使用Spring AOP的经验,但这是我第一次使用AspectJ,并且无法使其正常工作。



我正在尝试如下所述: https://www.jetbrains.com/help/idea/2017.1/aspectj.html



我的build.gradle:

  apply plugin:' java'

存储库
{
mavenCentral()
}

依赖项
{
compileorg。 projectlombok:lombok:+

compileorg.aspectj:aspectjrt:+
compileorg.aspectj:aspectjweaver:+
compileorg.aspectj:aspectjtools: +
}

buildscript
{
repositories
{
maven
{
urlhttps:/ /maven.eveoh.nl/content/repositories/releases
}
}

依赖项
{
classpathnl.eveoh:gradle-aspectj :+
}
}

pro ject.ext
{
aspectjVersion ='+'
}

申请插件:'aspectj'

我的方面:

 包方面; 

import lombok.SneakyThrows;
import lombok.experimental.FieldDefaults;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import java.util.Arrays;

@Aspect
@FieldDefaults(makeFinal = true)
public aspect LoggingAspect
{
Journal journal = Journal.INSTANCE;

切入点全部(ProceedingJoinPoint proceedingJoinPoint):执行(* * set *(..));

around():all(ProceedingJoinPoint proceedingJoinPoint)
{
System.out.println(asd);
}

@SneakyThrows
@Around(execution(* * repository。*。*(..)))
公共对象日志(ProceedingJoinPoint proceedingJoinPoint)
{
journal.write(proceedingJoinPoint.getThis()。getClass()。getCanonicalName());
journal.write(\ n);

String arguments = Arrays
.stream(proceedingJoinPoint.getArgs())
.map(o - > o.getClass()。getCanonicalName()++ o .toString())
.reduce(new StringBuilder(),StringBuilder :: append,StringBuilder :: append)
.toString();

journal.write(arguments);
journal.write(\ n);

long start = System.currentTimeMillis();

对象结果= proceedingJoinPoint.proceed();

journal.write(result.toString());
journal.write(\ n);

journal.write(String.valueOf(System.currentTimeMillis() - start));
journal.write(\ nn \ n);
journal.flush();

返回结果;
}
}

样本类:

 包存储库; 

import java.io.Serializable;

公共类任务实现Serializable
{
private static final long serialVersionUID = 1L;

enum状态{NEW,IN_PROGRESS,FINISHED};

private Integer id;
private字符串描述;
私人雇员受让人;
私人员工记者;
private状态;

public Task(最终整数id,字符串描述,最终雇员受让人,最终雇员记者){
this.id = id;
this.assignee =受让人;
this.reporter =记者;
this.description = description;
this.status = Status.NEW;
}

公共员工getAssignee(){
返回受让人;
}

public void setAssignee(雇员受让人){
this.assignee = assignee;
}

...
}

我有IntelliJ IDEA Ultimate 2017.2,它正确地暗示所有方法(吸气剂和制定者)都是我的减产。它还暗示我,对于我的方面的日志建议,有多个减少点。但是,它没有暗示全部建议。



我已安装(在最新版本中)插件,如:
- AspectJ支持
- AspectJ weaver
- Spring AOP / @ AspectJ



Gradle依赖项自动为这个项目创建了库,所以我没有复制它。



我启用了:Build> AspectJ weaving。



设置>构建,执行,部署>编译器> Java编译器:



使用编译器:ajc



Ajc编译路径:/home/me/.gradle/caches/modules-2/files-2.1 /org.aspectj/aspectjtools/1.8.10/5c5957ee4615bde33b54ce965f5b85b8827b97d5/aspectjtools-1.8.10.jar



命令行参数:空



生成调试信息:选中



委托给javac:chcecked



启用注释处理选项:已检查



项目构建和编译没有问题。它运行就像从来没有任何方面,特殊性没有从方面打印出来,也没有创建临时文件,并且所有方法运行良好(尽管在我的所有建议中,我没有处理关节点)。



当我在任何建议的第一行创建断点并使用调试器运行项目时,没有任何内容被捕获。



什么我错过了吗?

解决方案

在浪费了更多时间后,我发现我将两个截然不同的AspectJ sytnax混合在一起。



一个只是方面(如public aspect LoggingAspect),第二个是常规Java注释(如@Aspect或@Around)。 / p>

当我用公共类LoggingAspect替换public aspect LoggingAspect时,它起作用了。很难找到,因为一切仍然没有问题编译。



希望有一天或至少在未来版本的ajc中它会帮助某人,他们会添加更详细的编译器输出。


I am trying to use AspectJ in sample project in IntelliJ IDEA. I have an experience with Spring AOP, but this is first time I am using AspectJ, and cannot make it work.

I am trying to do as described here: https://www.jetbrains.com/help/idea/2017.1/aspectj.html

My build.gradle:

apply plugin: 'java'

repositories
{
    mavenCentral()
}

dependencies
{
    compile "org.projectlombok:lombok:+"

    compile "org.aspectj:aspectjrt:+"
    compile "org.aspectj:aspectjweaver:+"
    compile "org.aspectj:aspectjtools:+"
}

buildscript
{
    repositories
    {
        maven
        {
            url "https://maven.eveoh.nl/content/repositories/releases"
        }
    }

    dependencies
    {
        classpath "nl.eveoh:gradle-aspectj:+"
    }
}

project.ext
{
    aspectjVersion = '+'
}

apply plugin: 'aspectj'

My aspect:

package aspects;

import lombok.SneakyThrows;
import lombok.experimental.FieldDefaults;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import java.util.Arrays;

@Aspect
@FieldDefaults(makeFinal = true)
public aspect LoggingAspect
{
    Journal journal = Journal.INSTANCE;

    pointcut all(ProceedingJoinPoint proceedingJoinPoint) : execution(* * set*(..) );

    around() : all(ProceedingJoinPoint proceedingJoinPoint)
    {
        System.out.println("asd");
    }

    @SneakyThrows
    @Around("execution(* * repository.*.*(..))")
    public Object log(ProceedingJoinPoint proceedingJoinPoint)
    {
        journal.write(proceedingJoinPoint.getThis().getClass().getCanonicalName());
        journal.write("\n");

        String arguments = Arrays
            .stream(proceedingJoinPoint.getArgs())
            .map(o -> o.getClass().getCanonicalName() + " " + o.toString())
            .reduce(new StringBuilder(), StringBuilder::append, StringBuilder::append)
            .toString();

        journal.write(arguments);
        journal.write("\n");

        long start = System.currentTimeMillis();

        Object result = proceedingJoinPoint.proceed();

        journal.write(result.toString());
        journal.write("\n");

        journal.write(String.valueOf(System.currentTimeMillis() - start));
        journal.write("\n\n");
        journal.flush();

        return result;
    }
}

Sample class:

package repository;

import java.io.Serializable;

public class Task implements Serializable
{
    private static final long serialVersionUID = 1L;

    enum Status {NEW, IN_PROGRESS, FINISHED};

    private Integer id;
    private String description;
    private Employee assignee;
    private Employee reporter;
    private Status status;

    public Task(final Integer id, String description, final Employee assignee, final Employee reporter) {
        this.id = id;
        this.assignee = assignee;
        this.reporter = reporter;
        this.description = description;
        this.status = Status.NEW;
    }

    public Employee getAssignee() {
        return assignee;
    }

    public void setAssignee(Employee assignee) {
        this.assignee = assignee;
    }

...
}

I have IntelliJ IDEA Ultimate 2017.2 and it hints correctly that all methods (getters and setters) are my point-cuts. It also hints me that for advice "log" in my aspect there are multiple point-cuts. However, it does not hint about "all" advice.

I have installed (in newest version) plugins like: - AspectJ Support - AspectJ weaver - Spring AOP/@AspectJ

Gradle dependencies automatically created libraries for this project so I did not duplicate it.

I have enabled: Build > AspectJ weaving.

Settings > Build, Execution, Deployment > Compiler > Java Compiler:

Use compiler: ajc

Path to Ajc compiles: /home/me/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjtools/1.8.10/5c5957ee4615bde33b54ce965f5b85b8827b97d5/aspectjtools-1.8.10.jar

Command line parameters: empty

Generate debug info: checked

Delegate to javac: chcecked

Enable annotation processing options: checked

Project builds and compiles without problem. It runs like there was never any aspect, particularity nothing from aspect is printed out and no temporary files are created, and all methods runs fine (although in my "all" advice that is around I have not processed joint point).

When I create breakpoints in first lines of any advice and run project with debugger nothing is caught.

What am I missing?

解决方案

After wasting even more hours, I found out that I have mixed two distinct AspectJ sytnax together.

One that is just about aspects (like "public aspect LoggingAspect"), and second one that are regular Java annotations (like "@Aspect" or "@Around").

When I have replaced "public aspect LoggingAspect" with "public class LoggingAspect" it worked. It was very hard to find out, because everything still compiles without problem.

Hope it helps somebody someday or at least in future version of ajc they will add more verbose compiler output.

这篇关于IntelliJ IDEA + AspectJ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆