aboutsummaryrefslogtreecommitdiffstats
path: root/loadtime/src/org/aspectj/weaver/loadtime/Options.java
blob: 507afe9900052a505c9df66b61221f561879dec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*******************************************************************************
 * Copyright (c) 2005 Contributors.
 * All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution and is available at
 * http://eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Alexandre Vasseur         initial implementation
 *******************************************************************************/
package org.aspectj.weaver.loadtime;

import org.aspectj.bridge.IMessage;
import org.aspectj.bridge.IMessageHandler;
import org.aspectj.bridge.Message;
import org.aspectj.util.LangUtil;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * A class that hanldes LTW options.
 * Note: AV - I choosed to not reuse AjCompilerOptions and alike since those implies too many dependancies on
 * jdt and ajdt modules.
 *
 * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
 */
public class Options {

    private final static String OPTION_15 = "-1.5";
    private final static String OPTION_lazyTjp = "-XlazyTjp";
    private final static String OPTION_noWarn = "-nowarn";
    private final static String OPTION_noWarnNone = "-warn:none";
    private final static String OPTION_proceedOnError = "-proceedOnError";
    private final static String OPTION_verbose = "-verbose";
    private final static String OPTION_reweavable = "-Xreweavable";
    private final static String OPTION_noinline = "-Xnoinline";
    private final static String OPTION_showWeaveInfo = "-showWeaveInfo";
    private final static String OPTIONVALUED_messageHandler = "-XmessageHandlerClass:";

    public static WeaverOption parse(String options, ClassLoader laoder) {
        if (LangUtil.isEmpty(options)) {
            return new WeaverOption();
        }
        // the first option wins
        List flags = LangUtil.anySplit(options, " ");
        Collections.reverse(flags);

        WeaverOption weaverOption = new WeaverOption();

        // do a first round on the message handler since it will report the options themselves
        for (Iterator iterator = flags.iterator(); iterator.hasNext();) {
            String arg = (String) iterator.next();
            if (arg.startsWith(OPTIONVALUED_messageHandler)) {
                if (arg.length() > OPTIONVALUED_messageHandler.length()) {
                    String handlerClass = arg.substring(OPTIONVALUED_messageHandler.length()).trim();
                    try {
                        Class handler = Class.forName(handlerClass, false, laoder);
                        weaverOption.messageHandler = ((IMessageHandler) handler.newInstance());
                    } catch (Throwable t) {
                        weaverOption.messageHandler.handleMessage(
                                new Message(
                                        "Cannot instantiate message handler " + handlerClass,
                                        IMessage.ERROR,
                                        t,
                                        null
                                )
                        );
                    }
                }
            }
        }

        // configure the other options
        for (Iterator iterator = flags.iterator(); iterator.hasNext();) {
            String arg = (String) iterator.next();
            if (arg.equals(OPTION_15)) {
                weaverOption.java5 = true;
            } else if (arg.equalsIgnoreCase(OPTION_lazyTjp)) {
                weaverOption.lazyTjp = true;
            } else if (arg.equalsIgnoreCase(OPTION_noinline)) {
                weaverOption.noInline = true;
            } else if (arg.equalsIgnoreCase(OPTION_noWarn) || arg.equalsIgnoreCase(OPTION_noWarnNone)) {
                weaverOption.noWarn = true;
            } else if (arg.equalsIgnoreCase(OPTION_proceedOnError)) {
                weaverOption.proceedOnError = true;
            } else if (arg.equalsIgnoreCase(OPTION_reweavable)) {
                weaverOption.reWeavable = true;
            } else if (arg.equalsIgnoreCase(OPTION_showWeaveInfo)) {
                weaverOption.showWeaveInfo = true;
            } else if (arg.equalsIgnoreCase(OPTION_verbose)) {
                weaverOption.verbose = true;
            } else if (arg.startsWith(OPTIONVALUED_messageHandler)) {
                ;// handled in first round
            } else {
                weaverOption.messageHandler.handleMessage(
                        new Message(
                                "Cannot configure weaver with option '" + arg + "': unknown option",
                                IMessage.WARNING,
                                null,
                                null
                        )
                );
            }
        }

        // refine message handler configuration
        if (weaverOption.noWarn) {
            weaverOption.messageHandler.dontIgnore(IMessage.WARNING);
        }
        if (weaverOption.verbose) {
            weaverOption.messageHandler.dontIgnore(IMessage.DEBUG);
        }
        if (weaverOption.showWeaveInfo) {
            weaverOption.messageHandler.dontIgnore(IMessage.WEAVEINFO);
        }

        return weaverOption;
    }

    public static class WeaverOption {
        boolean java5;
        boolean lazyTjp;
        boolean noWarn;
        boolean proceedOnError;
        boolean verbose;
        boolean reWeavable;
        boolean noInline;
        boolean showWeaveInfo;
        IMessageHandler messageHandler;

        public WeaverOption() {
            messageHandler = new DefaultMessageHandler();//default
        }
    }
}