Example usage for org.eclipse.jdt.core IType getElementType

List of usage examples for org.eclipse.jdt.core IType getElementType

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType getElementType.

Prototype

int getElementType();

Source Link

Document

Returns this element's kind encoded as an integer.

Usage

From source file:io.sarl.eclipse.tests.util.Jdt2EcoreTest.java

License:Apache License

/** Replies a mock of a IType.
 *
 * <table>//  w  w  w. j a  v  a2  s  . co  m
 * <tr><td>Z</td><td>boolean</td></tr>
 * <tr><td>B</td><td>byte</td></tr>
 * <tr><td>C</td><td>char</td></tr>
 * <tr><td>S</td><td>short</td></tr>
 * <tr><td>I</td><td>int</td></tr>
 * <tr><td>J</td><td>long</td></tr>
 * <tr><td>F</td><td>float</td></tr>
 * <tr><td>D</td><td>double</td></tr>
 * <tr><td>V</td><td>void</td></tr>
 * <tr><td>L fully-qualified-class ;</td><td>fully-qualified-class</td></tr>
 * <tr><td>[ type</td><td>type[]</td></tr>
 * </table>
 */
protected static IType createITypeMock(boolean isInterface, String fullyQualifiedName, String superType,
        String[] superInterfaces, Procedure2<IType, List<IField>> fieldInitializer,
        Procedure2<IType, List<IMethod>> methodInitializer, TypeFinder typeFinder) {
    try {
        IType type = AbstractSarlTest.mock(IType.class);
        when(type.getFullyQualifiedName()).thenReturn(fullyQualifiedName);
        IPackageFragment packageFragment = AbstractSarlTest.mock(IPackageFragment.class);
        int idx = fullyQualifiedName.lastIndexOf('.');
        if (idx >= 0) {
            when(packageFragment.getElementName()).thenReturn(fullyQualifiedName.substring(0, idx));
            when(packageFragment.getElementType()).thenReturn(IJavaElement.PACKAGE_FRAGMENT);
            when(packageFragment.isDefaultPackage()).thenReturn(false);
            when(type.getElementName()).thenReturn(fullyQualifiedName.substring(idx + 1));
        } else {
            when(packageFragment.getElementName()).thenReturn("");
            when(packageFragment.getElementType()).thenReturn(IJavaElement.PACKAGE_FRAGMENT);
            when(packageFragment.isDefaultPackage()).thenReturn(true);
            when(type.getElementName()).thenReturn(fullyQualifiedName);
        }
        when(type.getPackageFragment()).thenReturn(packageFragment);
        when(type.getElementType()).thenReturn(IJavaElement.TYPE);
        if (isInterface) {
            final List<String> superNames = new ArrayList<>();
            final List<String> superSignatures = new ArrayList<>();
            if (!Strings.isNullOrEmpty(superType)) {
                superNames.add(superType);
                superSignatures.add("L" + superType + ";");
            }
            if (superInterfaces != null) {
                for (String superInterface : superInterfaces) {
                    superNames.add(superInterface);
                    superSignatures.add("L" + superInterface + ";");
                }
            }
            String[] array = new String[superNames.size()];
            superNames.toArray(array);
            doReturn(array).when(type).getSuperInterfaceNames();
            array = new String[superSignatures.size()];
            superSignatures.toArray(array);
            doReturn(array).when(type).getSuperInterfaceTypeSignatures();
        } else {
            if (Strings.isNullOrEmpty(superType)) {
                when(type.getSuperclassName()).thenReturn("java.lang.Object");
                when(type.getSuperclassTypeSignature()).thenReturn("Ljava.lang.Object;");
            } else {
                when(type.getSuperclassName()).thenReturn(superType);
                when(type.getSuperclassTypeSignature()).thenReturn("L" + superType + ";");
            }
        }
        List<IField> fields = new ArrayList<>();
        if (fieldInitializer != null) {
            fieldInitializer.apply(type, fields);
        }
        IField[] fieldArray = new IField[fields.size()];
        fields.toArray(fieldArray);
        doReturn(fieldArray).when(type).getFields();
        List<IMethod> methods = new ArrayList<>();
        if (methodInitializer != null) {
            methodInitializer.apply(type, methods);
        }
        IMethod[] methodArray = new IMethod[methods.size()];
        methods.toArray(methodArray);
        doReturn(methodArray).when(type).getMethods();
        if (!isInterface) {
            if (superInterfaces != null && superInterfaces.length > 0) {
                String[] interSigs = new String[superInterfaces.length];
                for (int i = 0; i < superInterfaces.length; ++i) {
                    interSigs[i] = "L" + superInterfaces[i] + ";";
                }
                doReturn(superInterfaces).when(type).getSuperInterfaceNames();
                doReturn(interSigs).when(type).getSuperInterfaceTypeSignatures();
            } else {
                doReturn(new String[0]).when(type).getSuperInterfaceNames();
                doReturn(new String[0]).when(type).getSuperInterfaceTypeSignatures();
            }
        }
        if (typeFinder != null) {
            when(type.resolveType(ArgumentMatchers.anyString())).thenAnswer((it) -> {
                final IType itype = typeFinder.findType(it.getArgument(0));
                if (itype != null) {
                    return new String[][] { new String[] { itype.getPackageFragment().getElementName(),
                            itype.getElementName() }, };
                }
                return null;
            });
        }
        return type;
    } catch (JavaModelException exception) {
        throw new RuntimeException(exception);
    }
}

From source file:org.eclipse.objectteams.otdt.debug.ui.internal.actions.OTToggleBreakpointAdapter.java

License:Open Source License

/** Make sure each role type is prefixed with __OT__. */
private static String getSimpleTypeName(IType type) {
    String name = type.getElementName();
    if (name.startsWith(IOTConstants.OT_DELIM))
        return name; // already prefixed

    boolean isRole = false;
    if (type.getElementType() == IOTJavaElement.ROLE)
        isRole = true;//from  w  w w.  j  ava  2  s . c  o m
    else if (OTModelManager.hasOTElementFor(type)) {
        IOTType otType = OTModelManager.getOTElement(type);
        isRole = otType.isRole();
    }
    if (isRole)
        return IOTConstants.OT_DELIM + name;
    return name;
}