summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/mapping/BehaviorEntryFactory.java
blob: d3cfb938e098b61a8a6f4edc2aeae7326a6f5f64 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2014 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Jeff Martin - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma.mapping;

import javassist.CtBehavior;
import javassist.CtConstructor;
import javassist.CtMethod;
import javassist.bytecode.Descriptor;


public class BehaviorEntryFactory
{
	public static BehaviorEntry create( String className, String name, String signature )
	{
		return create( new ClassEntry( className ), name, signature );
	}
	
	public static BehaviorEntry create( ClassEntry classEntry, String name, String signature )
	{
		if( name.equals( "<init>" ) )
		{
			return new ConstructorEntry( classEntry, signature );
		}
		else if( name.equals( "<clinit>" ) )
		{
			return new ConstructorEntry( classEntry );
		}
		else
		{
			return new MethodEntry( classEntry, name, signature );
		}
	}
	
	public static BehaviorEntry create( CtBehavior behavior )
	{
		String className = Descriptor.toJvmName( behavior.getDeclaringClass().getName() );
		if( behavior instanceof CtMethod )
		{
			return create( className, behavior.getName(), behavior.getSignature() );
		}
		else if( behavior instanceof CtConstructor )
		{
			CtConstructor constructor = (CtConstructor)behavior;
			if( constructor.isClassInitializer() )
			{
				return create( className, "<clinit>", null );
			}
			else
			{
				return create( className, "<init>", constructor.getSignature() );
			}
		}
		else
		{
			throw new IllegalArgumentException( "Unable to create BehaviorEntry from " + behavior );
		}
	}
	
	public static BehaviorEntry createObf( ClassEntry classEntry, MethodMapping methodMapping )
	{
		return create( classEntry, methodMapping.getObfName(), methodMapping.getObfSignature() );
	}
	
	public static BehaviorEntry createDeobf( ClassEntry classEntry, MethodMapping methodMapping )
	{
		return create( classEntry, methodMapping.getDeobfName(), methodMapping.getObfSignature() );
	}
}