简体   繁体   English

如何在方面内使用ajc build参数?

[英]How do I use an ajc build parameter inside an aspect?

I need to know the name of my .jar inside the aspect so I can create a string field with it via @DeclareParents. 我需要知道方面中的.jar的名称,以便可以通过@DeclareParents使用它创建一个字符串字段。

I know I can pass things to the ajc compiler, but is it actually possible to use the passed arguments from the aspect? 我知道我可以将事情传递给AJC编译器,但是实际上可以使用从方面传递的参数吗? The end result should be classes with an additional field containing as value the name of my .jar. 最终结果应该是带有附加字段的类,该字段包含.jar名称的值。

UPDATE: Test of suggestion. 更新:建议测试。 Gson.jar is a .jar on the classpath Gson.jar是类路径上的.jar

    InputStream r = Gson.class.getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF");
    String x = getString(r);
    System.out.println(x);

Output: 输出:

Manifest-Version: 1.0

Name: org/aspectj/lang/
Specification-Title: AspectJ Runtime Classes 
Specification-Version: 1.7
Specification-Vendor: aspectj.org
Implementation-Title: org.aspectj.tools
Implementation-Version: 1.7.3
Implementation-Vendor: aspectj.org
Bundle-Name: AspectJ Runtime
Bundle-Version: 1.7.3
Bundle-Copyright: (C) Copyright 1999-2001 Xerox Corporation, 2002 Palo 
  Alto Research Center, Incorporated (PARC), 2003-2009 Contributors. 
  All Rights Reserved.

Seems like there can only be one MANIFEST.MF resource at the same time and AspectJ.jar happens to be first on the class path. 似乎一次只能有一个MANIFEST.MF资源,而AspectJ.jar恰好位于类路径的首位。

I do not know any way to do what you want. 我不知道您要做什么。 Even if it was possible, the string values would still be the same if you unpacked the JAR and loaded the modified classes from the file system or repackaged/renamed the JAR. 即使有可能,如果您解压缩JAR并从文件系统加载已修改的类或将JAR重新打包/重命名,则字符串值仍将相同。 The solution would be static, not dynamic. 解决方案将是静态的,而不是动态的。

Anyway, how about putting information like that into a configuration file packaged into the JAR or maybe even right into the manifest file? 无论如何,如何将这样的信息放入打包到JAR中的配置文件中,甚至直接放入清单文件中? Maven has capabilities to add information to the manifest and Java can read them during runtime. Maven具有将信息添加到清单的功能,Java可以在运行时读取它们。 I have not thought it through, not to speak of trying to implement something like that, but maybe this is a way you can go. 我没有仔细考虑,更不用说尝试实现类似的东西了,但是也许这是您可以采取的方法。

Feel free to ask follow-up questions. 随时提出后续问题。


Update: You can avoid the manifest approach and try to directly determine the JAR file for each loaded class, see also this answer . 更新:您可以避免使用清单方法,并尝试直接为每个已加载的类确定JAR文件,另请参见此答案

Utility class: 实用类:

package de.scrum_master.util;

import java.net.URL;

public class ClassFileHelper {
    public static URL getClassURL(Class<?> clazz) {
        return clazz.getResource('/' + clazz.getName().replace('.', '/') + ".class");
    }

    public static String getJARFromURL(URL url) {
        if (!url.getProtocol().equals("jar"))
            return null;
        String fileName = url.getFile();
        fileName = fileName.substring(0, fileName.lastIndexOf('!'));
        fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
        return fileName;
    }
}

Usage: 用法:

I tried this in a Java + AspectJ project in which the aspects are wrapped in a JAR file and the Java files are stored in a file system directory. 我在Java + AspectJ项目中尝试了此操作,在该项目中,方面包装在JAR文件中,而Java文件存储在文件系统目录中。 I just added the following advice to an aspect within the JAR: 我只是在JAR的一个方面添加了以下建议:

before() : execution(public static void main(..)) {
    try {
        Class<?>[] classes = { String.class, this.getClass(), Class.forName("com.BadReader") };
        for (Class<?> clazz : classes) {
            System.out.println(clazz);
            URL classURL = ClassFileHelper.getClassURL(clazz);
            System.out.println(classURL);
            System.out.println(ClassFileHelper.getJARFromURL(classURL));
            System.out.println();
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

As you can see, the array contains 如您所见,该数组包含

  • a JRE bootstrap class ( String ), 一个JRE引导类( String ),
  • the aspect class itself ( this.getClass() ), 方面类本身( this.getClass() ),
  • a class from the Java project outside the JAR ( Class.forName("com.BadReader") ). JAR之外的Java项目中的类( Class.forName("com.BadReader") )。

Console output: 控制台输出:

class java.lang.String
jar:file:/C:/Programme/Java/jre7/lib/rt.jar!/java/lang/String.class
rt.jar

class com.SafeReaderAspect
jar:file:/C:/Users/Alexander/Documents/java-src/SO_AJ_ITD2StepCompilation_AspectJ/aspectLib.jar!/com/SafeReaderAspect.class
aspectLib.jar

class com.BadReader
file:/C:/Users/Alexander/Documents/java-src/SO_AJ_ITD2StepCompilation_Java/bin/com/BadReader.class
null

Is this what you want? 这是你想要的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM