ReturnEventInfo.java
4.78 KB
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
139
140
141
142
143
144
145
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.transformers.event;
import org.spongepowered.asm.lib.Type;
import com.mumfrey.liteloader.core.event.EventCancellationException;
/**
* EventInfo for events which have a return type
*
* @author Adam Mummery-Smith
*
* @param <S> Source object type. For non-static methods this will be the
* containing object instance.
* @param <R> Return type
*/
public class ReturnEventInfo<S, R> extends EventInfo<S>
{
private R returnValue;
public ReturnEventInfo(String name, S source, boolean cancellable)
{
super(name, source, cancellable);
this.returnValue = null;
}
public ReturnEventInfo(String name, S source, boolean cancellable, R returnValue)
{
super(name, source, cancellable);
this.returnValue = returnValue;
}
@SuppressWarnings("unchecked")
public ReturnEventInfo(String name, S source, boolean cancellable, byte returnValue)
{
super(name, source, cancellable);
this.returnValue = (R)Byte.valueOf(returnValue);
}
@SuppressWarnings("unchecked")
public ReturnEventInfo(String name, S source, boolean cancellable, char returnValue)
{
super(name, source, cancellable);
this.returnValue = (R)Character.valueOf(returnValue);
}
@SuppressWarnings("unchecked")
public ReturnEventInfo(String name, S source, boolean cancellable, double returnValue)
{
super(name, source, cancellable);
this.returnValue = (R)Double.valueOf(returnValue);
}
@SuppressWarnings("unchecked")
public ReturnEventInfo(String name, S source, boolean cancellable, float returnValue)
{
super(name, source, cancellable);
this.returnValue = (R)Float.valueOf(returnValue);
}
@SuppressWarnings("unchecked")
public ReturnEventInfo(String name, S source, boolean cancellable, int returnValue)
{
super(name, source, cancellable);
this.returnValue = (R)Integer.valueOf(returnValue);
}
@SuppressWarnings("unchecked")
public ReturnEventInfo(String name, S source, boolean cancellable, long returnValue)
{
super(name, source, cancellable);
this.returnValue = (R)Long.valueOf(returnValue);
}
@SuppressWarnings("unchecked")
public ReturnEventInfo(String name, S source, boolean cancellable, short returnValue)
{
super(name, source, cancellable);
this.returnValue = (R)Short.valueOf(returnValue);
}
@SuppressWarnings("unchecked")
public ReturnEventInfo(String name, S source, boolean cancellable, boolean returnValue)
{
super(name, source, cancellable);
this.returnValue = (R)Boolean.valueOf(returnValue);
}
/**
* Sets a return value for this event and cancels the event (required in
* order to return the new value).
*
* @param returnValue
*/
public void setReturnValue(R returnValue) throws EventCancellationException
{
super.cancel();
this.returnValue = returnValue;
}
public R getReturnValue()
{
return this.returnValue;
}
// CHECKSTYLE:OFF
// All of the accessors below are to avoid having to generate unboxing conversions in bytecode
public byte getReturnValueB() { if (this.returnValue == null) return 0; return (Byte) this.returnValue; }
public char getReturnValueC() { if (this.returnValue == null) return 0; return (Character)this.returnValue; }
public double getReturnValueD() { if (this.returnValue == null) return 0.0; return (Double) this.returnValue; }
public float getReturnValueF() { if (this.returnValue == null) return 0.0F; return (Float) this.returnValue; }
public int getReturnValueI() { if (this.returnValue == null) return 0; return (Integer) this.returnValue; }
public long getReturnValueJ() { if (this.returnValue == null) return 0; return (Long) this.returnValue; }
public short getReturnValueS() { if (this.returnValue == null) return 0; return (Short) this.returnValue; }
public boolean getReturnValueZ() { if (this.returnValue == null) return false; return (Boolean) this.returnValue; }
// CHECKSTYLE:ON
public static String getReturnAccessor(Type returnType)
{
if (returnType.getSort() == Type.OBJECT)
{
return "getReturnValue";
}
return String.format("getReturnValue%s", returnType.getDescriptor());
}
public static String getReturnDescriptor(Type returnType)
{
if (returnType.getSort() == Type.OBJECT)
{
return String.format("()%s", EventInfo.OBJECT);
}
return String.format("()%s", returnType.getDescriptor());
}
}