如何在方面内使用ajc构建参数? [英] How do I use an ajc build parameter inside an aspect?

查看:129
本文介绍了如何在方面内使用ajc构建参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道方面中的.jar名称,以便可以通过@DeclareParents使用它创建一个字符串字段.

I need to know the name of my .jar inside the aspect so I can create a string field with it via @DeclareParents.

我知道我可以将事情传递给ajc编译器,但是实际上可以使用从方面传递的参数吗? 最终结果应该是带有附加字段的类,其中包含我的.jar的名称作为值.

I know I can pass things to the ajc compiler, but is it actually possible to use the passed arguments from the aspect? The end result should be classes with an additional field containing as value the name of my .jar.

更新:测试建议. Gson.jar是类路径上的.jar

UPDATE: Test of suggestion. Gson.jar is a .jar on the classpath

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

输出:

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.

似乎同时只能有一个MANIFEST.MF资源,而AspectJ.jar恰好位于类路径的首位.

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.

推荐答案

我不知道要做什么.即使有可能,如果您解压缩JAR并从文件系统中加载已修改的类或将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. The solution would be static, not dynamic.

无论如何,将这样的信息放入打包到JAR中的配置文件中,甚至直接放入清单文件中,该如何做? Maven具有将信息添加到清单的功能,Java可以在运行时读取它们.我还没有考虑周全,更不用说尝试实现类似的东西了,但是也许这是您可以采用的方法.

Anyway, how about putting information like that into a configuration file packaged into the JAR or maybe even right into the manifest file? Maven has capabilities to add information to the manifest and Java can read them during runtime. 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.

随时提出后续问题.

更新:您可以避免使用清单方法,并尝试直接确定每个已加载类的JAR文件,另请参见

Update: You can avoid the manifest approach and try to directly determine the JAR file for each loaded class, see also this answer.

实用程序类:

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;
    }
}

用法:

我在Java + AspectJ项目中尝试了此操作,在该项目中,方面包装在JAR文件中,而Java文件存储在文件系统目录中.我只是在JAR的一个方面添加了以下建议:

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. I just added the following advice to an aspect within the 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();
    }
}

如您所见,该数组包含

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

控制台输出:

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

这是您想要的吗?

这篇关于如何在方面内使用ajc构建参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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