Skip to main content
summaryrefslogtreecommitdiffstats
blob: 538460089d849e1c65a2c1ffbdeca968463bca93 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*******************************************************************************
 * Copyright (c) 2012 BestSolution.at and others.
 * 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://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
 *******************************************************************************/
package org.eclipse.fx.ide.model.internal.utils;

import org.eclipse.fx.ide.model.IFXPrimitiveProperty;
import org.eclipse.fx.ide.model.IFXPrimitiveProperty.Type;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IMemberValuePair;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;


public class Util {
	@SuppressWarnings("unchecked")
	public static <@Nullable R> R getAnnotationMemberValue(IAnnotation annotation, String name) throws JavaModelException {
		for (IMemberValuePair pair : annotation.getMemberValuePairs()) {
			if( name.equals(pair.getMemberName()) ) {
				return (R) pair.getValue();
			}
		}
		return null;
	}
	
	public static boolean checkStatemask(int state, int mask) {
		return (state & mask) != 0;
	}
	
	public static String getFQNType(IType referenceType, String name) throws JavaModelException {
		// no need to resolve it is already an FQN
		if( name.contains(".") ) {
			return name;
		}
		
		// This is a primitive type
		for( Type t : IFXPrimitiveProperty.Type.values() ) {
			if( t.jvmType().equals(name) ) {
				return null;
			}
		}
		
		String[][] parts = referenceType.resolveType(name);
		if( parts != null && parts.length > 0 ) {
			return toFQN(parts[0]);
		} else {
			//FIXME Log it
		}
		return null;
	}
	
	public static String toFQN(String[] array) {
		StringBuilder b = new StringBuilder();
		
		for( String s : array ) {
			if( b.length() > 0 && ! b.toString().endsWith(".") ) {
				b.append(".");
			}
			b.append(s);
		}
		
		return b.toString();
	}
	
	public static String toFQN(IType owner, String signature) throws JavaModelException {
		String genericType = Signature.toString(signature);
		String eType = genericType;
		
		if( genericType.contains("<") ) {
			eType = genericType.substring(genericType.indexOf('<')+1, genericType.indexOf('>'));	
		}
		
		// FIXME Is there a better way?
		if( eType.contains("super") ) {
			eType = eType.substring(eType.indexOf("super")+"super".length()).trim();
		} else if( eType.contains("extends") ) {
			eType = eType.substring(eType.indexOf("extends")+"extends".length()).trim();
		}
		
		String fqn = getFQNType(owner,eType); 
		return fqn;
	}
}

Back to the top