有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使aspectj与scala模型一起工作

我有一个用Scala制作的域和一些用Java制作的类。我需要用Aspectj做一些我知道可以用的方面,因为用Java类,它可以用。 问题是,当Scala类被注释时,它不起作用。其他注释(如hibernate)与我的Scala类配合得很好

这是我的pom。xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>Group</groupId>
    <artifactId>Parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.source-target.version>1.8</java.source-target.version>
        <aspectj.version>1.8.2</aspectj.version>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>${java.source-target.version}</source>
                        <target>${java.source-target.version}</target>
                        <useIncrementalCompilation>false</useIncrementalCompilation>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>createClassesDir</id>
                            <phase>process-resources</phase>
                            <configuration>
                                <tasks>
                                    <mkdir dir="${project.build.directory}\unwoven-classes" />
                                    <mkdir dir="${project.build.directory}\classes" />
                                </tasks>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.7</version>
                    <configuration>
                        <complianceLevel>1.8</complianceLevel>
                        <source>${aspectj.version>}</source>
                        <target>${aspectj.version>}</target>
                        <weaveDirectories>
                            <weaveDirectory>${project.build.directory}\unwoven-classes</weaveDirectory>
                        </weaveDirectories>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>process-classes</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>scala-compile-first</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>add-source</goal>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>scala-test-compile</id>
                            <phase>process-test-resources</phase>
                            <goals>
                                <goal>testCompile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.scala-lang</groupId>
                <artifactId>scala-library</artifactId>
                <version>2.12.1</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>Aspects</module>
    </modules>
</project>

我认为我必须对maven做些什么,因为方面和代码的其余部分工作得很好。 有什么办法吗

谢谢大家!


共 (2) 个答案

  1. # 1 楼答案

    问题很可能是Aspect经常应用于java代码的语义——“找到一个名为X的方法,并围绕它执行以下操作。”但从Java查看Scala代码时,通常不会遵循预期的命名约定。如果没有关于具体切入点的更多细节——以及它们所应用的代码——我就无法给出更多的见解

  2. # 2 楼答案

    首先,确保你的方面(基于注释或本机语法)总是有一个.aj文件扩展名(在你使用的IDE中,通过“New aspect”而不是“New class”菜单将它们添加到你的项目中)。我已经在我的fork中从您的repo中删除了重复类,并相应地重命名了另一个。顺便说一句,我选择了原生语法

    然而,更糟糕的是,在某种程度上,您希望在特定的目录中不包含Scala类,但您没有配置Scala插件来实际将它们放在那里。我通过添加以下代码片段修复了这个问题:

    <configuration>
        <outputDir>${project.build.directory}/unwoven-classes</outputDir>
    </configuration>
    

    现在AspectJ Maven插件在那里找到Scala类,并对它们执行二进制编织。这修复了Java和Scala测试。这两个版本以前在Maven中都失败了,现在至少Java版本可以在IntelliJ中使用,但Scala版本不行。这是因为IDEA不知道这个奇怪的Maven设置和您的附加(中间)目录

    因此,aspect本身或AspectJ无法处理Scala二进制文件并没有什么问题。项目设置是错误的,在某种程度上,IDE支持仍然是错误的

    那么,你如何才能完全修复它呢?你有几个选择:

    • 将aspect代码放入另一个Maven模块中,然后在Java+Scala模块上配置一个weave依赖项,将其中的所有类编织到aspect模块中。但是,在运行测试时,您可能仍然存在问题。但至少您可以为IDEA项目配置具有正确依赖关系的编译后编织
    • 您还可以将Scala代码放在自己的模块中,将其定义为Java+AspectJ模块的依赖项,并以这种方式对其应用二进制编织

    其他变体也是可能的。我不想在这里过度分析,我只是用一种快速简单的方法修复了你的Maven设置,让你继续:

    $ mvn clean verify
    
    (...)
                               -
     T E S T S
                               -
    Aktive Codepage: 65001.
    Running aspects.AnnotationAspectTest
    set(String model.JavaEntity.name)
    set(String model.ScalaEntity.name)
    Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 sec
    
    Results :
    
    Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO]
    [INFO]  - maven-jar-plugin:2.4:jar (default-jar) @ aspectj-with-scala  -
    [INFO] Building jar: C:\Users\Alexander\Documents\java-src\aspectj-with-scala\target\aspectj-with-scala-1.0-SNAPSHOT.jar
    [INFO]                                     
    [INFO] BUILD SUCCESS
    [INFO]                                     
    (...)
    

    附言:我还为您创建了一个pull request,方便您将我的更改整合到您的回购协议中

    p.p.S.:看,一个MCVE比你之前做的更有帮助:发布一个单独的问题,只显示一个方面,然后在这里发布这个问题,只有一个Maven POM。为了重现和解决这个问题,我需要这两门课加上其他课程。发布GitHub项目后,查找和修复非常简单