aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/apt/PKG.java
blob: cf04efafe341e419854e4838cc2bdd63d54bae48 (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
package lombok.apt;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.lang.model.element.Element;
import javax.lang.model.type.TypeKind;

import lombok.Lombok;
import lombok.transformations.TransformationsUtil;


class PKG {
	static final String CURRENT_SUPPORT = "javac 1.6 and eclipse (ecj).";
	
	private PKG() {}
	
	static boolean isInstanceOf(Object o, String className) {
		if ( o == null ) return false;
		return isInstanceOf(o.getClass(), className);
	}
	
	static boolean isInstanceOf(Class<?> c, String className) {
		if ( c == Object.class || c == null ) return false;
		
		if ( c.getName().equals(className) ) return true;
		
		if ( isInstanceOf(c.getSuperclass(), className) ) return true;
		for ( Class<?> iface : c.getInterfaces() ) {
			if ( isInstanceOf(iface, className) ) return true;
		}
		
		return false;
	}
	
	static byte[] readResource(String name) {
		return readResource(PKG.class.getClassLoader(), name);
	}
	
	static byte[] readResource(ClassLoader loader, String name) {
		InputStream in = loader.getResourceAsStream(name);
		if ( in == null ) throw Lombok.sneakyThrow(new IOException("Not found: " + name));
		
		try {
			return readStream(in);
		} catch (IOException e) {
			throw Lombok.sneakyThrow(e);
		}
	}
	
	static String toGetterName(Element field) {
		CharSequence fieldName = field.getSimpleName();
		
		boolean isBoolean = field.asType().getKind() == TypeKind.BOOLEAN;
		
		return TransformationsUtil.toGetterName(fieldName, isBoolean);
	}
	
	static byte[] readStream(InputStream in) throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] b = new byte[65536];
		while ( true ) {
			int r = in.read(b);
			if ( r == -1 ) break;
			if ( r > 0 ) baos.write(b, 0, r);
		}
		return baos.toByteArray();
	}
}