Example usage for org.apache.commons.lang StringUtils substringBefore

List of usage examples for org.apache.commons.lang StringUtils substringBefore

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils substringBefore.

Prototype

public static String substringBefore(String str, String separator) 

Source Link

Document

Gets the substring before the first occurrence of a separator.

Usage

From source file:org.eclipse.wb.internal.core.utils.ast.AstNodeUtils.java

/**
 * @param binding//from   w ww  . ja  v a2 s  .c o m
 *          the {@link ITypeBinding} to analyze.
 * @param runtime
 *          flag <code>true</code> if we need name for class loading, <code>false</code> if we
 *          need name for source generation.
 * @param withGenerics
 *          flag <code>true</code> if generics type arguments should be appended.
 * 
 * @return the fully qualified name of given {@link ITypeBinding}, or
 *         {@link #NO_TYPE_BINDING_NAME}.
 */
public static String getFullyQualifiedName(ITypeBinding binding, boolean runtime, boolean withGenerics) {
    // check if no binding
    if (binding == null) {
        return NO_TYPE_BINDING_NAME;
    }
    // check for primitive type
    if (binding.isPrimitive()) {
        return binding.getName();
    }
    // array
    if (binding.isArray()) {
        StringBuilder sb = new StringBuilder();
        // append element type qualified name
        ITypeBinding elementType = binding.getElementType();
        String elementTypeQualifiedName = getFullyQualifiedName(elementType, runtime);
        sb.append(elementTypeQualifiedName);
        // append dimensions
        for (int i = 0; i < binding.getDimensions(); i++) {
            sb.append("[]");
        }
        // done
        return sb.toString();
    }
    // object
    {
        String scope;
        ITypeBinding declaringType = binding.getDeclaringClass();
        if (declaringType == null) {
            IPackageBinding packageBinding = binding.getPackage();
            if (packageBinding == null || packageBinding.isUnnamed()) {
                scope = "";
            } else {
                scope = packageBinding.getName() + ".";
            }
        } else if (binding.isTypeVariable()) {
            return binding.getName();
        } else {
            // use '$', because we use this class name for loading class
            scope = getFullyQualifiedName(declaringType, runtime);
            if (runtime) {
                scope += "$";
            } else {
                scope += ".";
            }
        }
        // prepare "simple" name, without scope
        String jdtName = binding.getName();
        String name = StringUtils.substringBefore(jdtName, "<");
        if (withGenerics) {
            ITypeBinding[] typeArguments = binding.getTypeArguments();
            if (typeArguments.length != 0) {
                StringBuilder sb = new StringBuilder(name);
                sb.append("<");
                for (ITypeBinding typeArgument : typeArguments) {
                    if (sb.charAt(sb.length() - 1) != '<') {
                        sb.append(",");
                    }
                    String typeArgumentName = getFullyQualifiedName(typeArgument, runtime, withGenerics);
                    sb.append(typeArgumentName);
                }
                sb.append(">");
                name = sb.toString();
            }
        }
        // qualified name is scope plus "simple" name
        return scope + name;
    }
}

From source file:org.eclipse.wb.internal.core.utils.jdt.core.CodeUtils.java

/**
 * @return the {@link IMethod} with given signature exactly in given {@link IType} or
 *         <code>null</code>.
 *//*from   www . j av a2  s  .c o  m*/
public static IMethod findMethodSingleType(IType type, String signature) throws JavaModelException {
    String name = StringUtils.substringBefore(signature, "(");
    boolean wantConstructor = "<init>".equals(name);
    for (IMethod method : type.getMethods()) {
        if (method.getElementName().equals(name) || wantConstructor && method.isConstructor()) {
            if (getMethodSignature(method).equals(signature)) {
                return method;
            }
        }
    }
    // not found
    return null;
}

From source file:org.eclipse.wb.internal.core.utils.jdt.core.JavaDocUtils.java

/**
 * @return the tooltip for given {@link IMethod} or <code>null</code> if no source found.
 *//*from   w w  w . j  a  v  a 2  s  .co m*/
public static String getTooltip(IMember method) throws Exception {
    List<String> javaDocLines = getJavaDocLines(method, true);
    if (javaDocLines == null) {
        return null;
    }
    // read tooltip
    String tooltip = StringUtils.join(javaDocLines.iterator(), " ");
    tooltip = StringUtilities.normalizeWhitespaces(tooltip);
    // remove other meta data
    tooltip = StringUtils.replace(tooltip, "{@inheritDoc}", "");
    tooltip = StringUtils.substringBefore(tooltip, "@param");
    tooltip = StringUtils.substringBefore(tooltip, "@since");
    tooltip = StringUtils.substringBefore(tooltip, "@see");
    tooltip = StringUtils.substringBefore(tooltip, "@author");
    tooltip = getTooltip_useShortTypeNames(tooltip);
    tooltip = tooltip.trim();
    // done
    return tooltip;
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

/**
 * @return the {@link Class} with given name - primitive or {@link Object} (including arrays).
 *///from  w  w w .java2  s  .c om
public static Class<?> getClassByName(ClassLoader classLoader, String className) throws Exception {
    Assert.isNotNull(className);
    // check for primitive type
    if ("boolean".equals(className)) {
        return boolean.class;
    } else if ("byte".equals(className)) {
        return byte.class;
    } else if ("char".equals(className)) {
        return char.class;
    } else if ("short".equals(className)) {
        return short.class;
    } else if ("int".equals(className)) {
        return int.class;
    } else if ("long".equals(className)) {
        return long.class;
    } else if ("float".equals(className)) {
        return float.class;
    } else if ("double".equals(className)) {
        return double.class;
    }
    // check for array
    if (className.endsWith("[]")) {
        int dimensions = StringUtils.countMatches(className, "[]");
        String componentClassName = StringUtils.substringBefore(className, "[]");
        Class<?> componentClass = getClassByName(classLoader, componentClassName);
        return Array.newInstance(componentClass, new int[dimensions]).getClass();
    }
    // OK, load this class as object
    return classLoader.loadClass(className);
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

private static String getSimplePropertyName(String qualifiedPropertyName) {
    return StringUtils.substringBefore(qualifiedPropertyName, "(");
}

From source file:org.eclipse.wb.internal.core.xml.model.generic.FlowContainerFactory.java

private Association getAssociation(String prefix) {
    String associationString = getParameter(prefix + ".x-association");
    if (StringUtils.isEmpty(associationString)) {
        return Associations.direct();
    } else if (associationString.startsWith("inter ")) {
        associationString = StringUtils.removeStart(associationString, "inter ");
        // extract tag
        String tag = StringUtils.substringBefore(associationString, " ");
        associationString = StringUtils.substringAfter(associationString, " ");
        // extract attributes
        Map<String, String> attributes = parseAttributes(associationString);
        return Associations.intermediate(tag, attributes);
    } else {/* w w  w.j  a v  a2 s .c  o m*/
        Assert.isTrue(associationString.startsWith("property "));
        String property = StringUtils.removeStart(associationString, "property ");
        return Associations.property(property);
    }
}

From source file:org.eclipse.wb.internal.core.xml.model.generic.SimpleContainerFactory.java

private Association getAssociation(String prefix) {
    String associationString = getParameter(prefix + ".x-association");
    if (associationString == null) {
        return Associations.direct();
    } else if (associationString.startsWith("inter ")) {
        associationString = StringUtils.removeStart(associationString, "inter ");
        // extract tag
        String tag = StringUtils.substringBefore(associationString, " ");
        associationString = StringUtils.substringAfter(associationString, " ");
        // extract attributes
        Map<String, String> attributes = parseAttributes(associationString);
        return Associations.intermediate(tag, attributes);
    } else {/*from w  w w  .j  av a2 s . c o  m*/
        Assert.isTrue(associationString.startsWith("property "));
        String property = StringUtils.removeStart(associationString, "property ");
        return Associations.property(property);
    }
}

From source file:org.eclipse.wb.internal.rcp.databinding.model.widgets.input.VirtualEditingSupportInfo.java

public String getCellEditorPresentationText() throws Exception {
    return isEmpty() ? ""
            : ClassUtils.getShortClassName(StringUtils.substringBefore(m_cellEditorClassName, "(")) + "."
                    + StringUtils.remove(m_cellEditorProperty, '"');
}

From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.ConverterInfo.java

private void parseClass(DatabindingsProvider provider, DocumentElement root, String value) throws Exception {
    m_namespace = StringUtils.substringBefore(value, ":");
    String packageValue = root.getAttribute("xmlns:" + m_namespace);
    ////from ww w .  j  av a  2s . com
    if (packageValue == null) {
        m_namespace = null;
        provider.addWarning(MessageFormat.format(Messages.ConverterInfo_converterNotFound, value),
                new Throwable());
    } else {
        m_className = StringUtils.substringAfter(packageValue, "clr-namespace:") + "."
                + StringUtils.substringAfter(value, ":");
        try {
            ClassLoader classLoader = provider.getXmlObjectRoot().getContext().getClassLoader();
            classLoader.loadClass(m_className);
        } catch (ClassNotFoundException e) {
            m_className = null;
            m_namespace = null;
            provider.addWarning(
                    MessageFormat.format(Messages.ConverterInfo_converterClassNotFound, m_className),
                    new Throwable());
        }
    }
}

From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.ValidationInfo.java

private void parseClass(DatabindingsProvider provider, String value) throws Exception {
    String namespace = StringUtils.substringBefore(value, ":");
    String packageValue = m_namespaceToPackage.get(namespace);
    ///*from  w  ww. ja v  a2s. c  om*/
    if (packageValue == null) {
        provider.addWarning(MessageFormat.format(Messages.ValidationInfo_validatorNotFound, value),
                new Throwable());
    } else {
        String className = packageValue + "." + StringUtils.substringAfter(value, ":");
        //
        try {
            ClassLoader classLoader = provider.getXmlObjectRoot().getContext().getClassLoader();
            classLoader.loadClass(className);
            m_classNames.add(className);
        } catch (ClassNotFoundException e) {
            provider.addWarning(MessageFormat.format(Messages.ValidationInfo_validatorClassNotFound, className),
                    new Throwable());
        }
    }
}