List of usage examples for org.apache.commons.lang ClassUtils getShortClassName
public static String getShortClassName(String className)
Gets the class name minus the package name from a String.
The string passed in is assumed to be a class name - it is not checked.
From source file:org.eclipse.wb.internal.rcp.databinding.wizards.autobindings.SwtDatabindingProvider.java
public String performSubstitutions(String code, ImportsManager imports) throws Exception { // prepare properties final List<PropertyAdapter> properties = Lists.newArrayList(); Display.getDefault().syncExec(new Runnable() { public void run() { CollectionUtils.addAll(properties, m_propertiesViewer.getCheckedElements()); }// w w w. j a va2 s . c om }); // if (m_firstPage.isCreateControlClass()) { return ControllerSupport.automaticWizardPerformSubstitutions(m_firstPage, code, imports, m_javaProject, m_classLoader, m_beanClass, properties, m_propertyToEditor); } // String begin = ""; String end = "\t\t"; String widgetStart = ""; boolean blockMode = useBlockMode(); if (blockMode) { begin = "\t\t{\r\n"; end = "\t\t}"; widgetStart = "\t"; } // prepare imports Collection<String> importList = Sets.newHashSet(); importList.add(SWT.class.getName()); importList.add("org.eclipse.jface.databinding.swt.SWTObservables"); importList.add("org.eclipse.core.databinding.observable.value.IObservableValue"); importList.add("org.eclipse.core.databinding.UpdateValueStrategy"); importList.add("org.eclipse.swt.widgets.Label"); importList.add("org.eclipse.swt.layout.GridLayout"); importList.add("org.eclipse.swt.layout.GridData"); // DataBindingsCodeUtils.ensureDBLibraries(m_javaProject); // IAutomaticWizardStub automaticWizardStub = GlobalFactoryHelper.automaticWizardCreateStub(m_javaProject, m_classLoader, m_beanClass); // String observeMethod = null; if (automaticWizardStub == null) { if (ObservableInfo.isPojoBean(m_beanClass)) { String pojoClass = DataBindingsCodeUtils.getPojoObservablesClass(); observeMethod = "ObserveValue = " + ClassUtils.getShortClassName(pojoClass) + ".observeValue("; importList.add(pojoClass); } else { observeMethod = "ObserveValue = BeansObservables.observeValue("; importList.add("org.eclipse.core.databinding.beans.BeansObservables"); } } else { automaticWizardStub.addImports(importList); } // prepare bean String beanClassName = CoreUtils.getClassName(m_beanClass); String beanClassShortName = ClassUtils.getShortClassName(beanClassName); String fieldPrefix = JavaCore.getOption(JavaCore.CODEASSIST_FIELD_PREFIXES); String fieldName = fieldPrefix + StringUtils.uncapitalize(beanClassShortName); code = StringUtils.replace(code, "%BeanClass%", beanClassName); // if (ReflectionUtils.getConstructorBySignature(m_beanClass, "<init>()") == null) { code = StringUtils.replace(code, "%BeanField%", fieldName); } else { code = StringUtils.replace(code, "%BeanField%", fieldName + " = new " + beanClassName + "()"); } // IPreferenceStore preferences = ToolkitProvider.DESCRIPTION.getPreferences(); String accessPrefix = preferences.getBoolean(FieldUniqueVariableSupport.P_PREFIX_THIS) ? "this." : ""; code = StringUtils.replace(code, "%BeanFieldAccess%", accessPrefix + fieldName); // code = StringUtils.replace(code, "%BeanName%", StringUtils.capitalize(beanClassShortName)); // prepare code StringBuffer widgetFields = new StringBuffer(); StringBuffer widgets = new StringBuffer(); String swtContainer = StringUtils.substringBetween(code, "%Widgets%", "%"); String swtContainerWithDot = "this".equals(swtContainer) ? "" : swtContainer + "."; // StringBuffer observables = new StringBuffer(); StringBuffer bindings = new StringBuffer(); // importList.add(GridLayout.class.getName()); widgets.append("\t\t" + swtContainerWithDot + "setLayout(new GridLayout(2, false));\r\n"); if (!blockMode) { widgets.append("\t\t\r\n"); } // for (Iterator<PropertyAdapter> I = properties.iterator(); I.hasNext();) { PropertyAdapter property = I.next(); Object[] editorData = m_propertyToEditor.get(property); SwtWidgetDescriptor widgetDescriptor = (SwtWidgetDescriptor) editorData[0]; JFaceBindingStrategyDescriptor strategyDescriptor = (JFaceBindingStrategyDescriptor) editorData[1]; // String propertyName = property.getName(); String widgetClassName = widgetDescriptor.getClassName(); String widgetFieldName = fieldPrefix + propertyName + widgetClassName; String widgetFieldAccess = accessPrefix + widgetFieldName; // field widgetFields.append("\r\nfield\r\n\tprivate " + widgetClassName + " " + widgetFieldName + ";"); // widget widgets.append(begin); widgets.append(widgetStart + "\t\tnew Label(" + swtContainer + ", SWT.NONE).setText(\"" + StringUtils.capitalize(propertyName) + ":\");\r\n"); widgets.append(end + "\r\n"); // widgets.append(begin); widgets.append( "\t\t" + widgetFieldAccess + " = " + widgetDescriptor.getCreateCode(swtContainer) + ";\r\n"); widgets.append(widgetStart + "\t\t" + widgetFieldAccess + ".setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r\n"); widgets.append(end); // observables observables.append("\t\tIObservableValue " + propertyName + "ObserveWidget = " + widgetDescriptor.getBindingCode(widgetFieldName) + ";\r\n"); if (automaticWizardStub == null) { observables.append("\t\tIObservableValue " + propertyName + observeMethod + fieldName + ", \"" + propertyName + "\");"); } else { observables.append(automaticWizardStub.createSourceCode(fieldName, propertyName)); } // bindings bindings.append("\t\tbindingContext.bindValue(" + propertyName + "ObserveWidget, " + propertyName + "ObserveValue, " + getStrategyValue(strategyDescriptor.getTargetStrategyCode()) + ", " + getStrategyValue(strategyDescriptor.getModelStrategyCode()) + ");"); // if (I.hasNext()) { widgetFields.append("\r\n"); widgets.append("\r\n"); observables.append("\r\n"); bindings.append("\r\n"); } // importList.add(widgetDescriptor.getFullClassName()); } // replace template patterns code = StringUtils.replace(code, "%WidgetFields%", widgetFields.toString()); code = StringUtils.replace(code, "%Widgets%" + swtContainer + "%", widgets.toString()); // code = StringUtils.replace(code, "%Observables%", observables.toString()); code = StringUtils.replace(code, "%Bindings%", bindings.toString()); // add imports for (String qualifiedTypeName : importList) { imports.addImport(qualifiedTypeName); } // return code; }
From source file:org.eclipse.wb.internal.rcp.databinding.wizards.autobindings.SwtWidgetDescriptor.java
/** * Sets widget class name. */ public void setFullClassName(String className) { m_fullClassName = className; m_className = ClassUtils.getShortClassName(m_fullClassName); }
From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.ConverterInfo.java
public void appendValue(StringBuffer value) throws Exception { if (m_update && getValue() != null) { value.append(", converter="); if (m_staticResurce) { value.append("{StaticResource " + m_resourceReference + "}"); } else {//from w w w . j a va 2s.co m value.append( StringUtils.defaultString(m_namespace) + ":" + ClassUtils.getShortClassName(m_className)); } } }
From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.ConverterInfo.java
public void applyChanges(AbstractDocumentObject object) throws Exception { if (m_update) { if (object instanceof DocumentAttribute) { if (m_className != null && m_namespace == null) { DocumentAttribute attribute = (DocumentAttribute) object; createNamespace(attribute.getEnclosingElement().getRoot()); // String value = attribute.getValue(); int index = value.indexOf("converter=") + 10; attribute.setValue(value.substring(0, index) + m_namespace + value.substring(index)); }/*www. j a va 2 s .c o m*/ } else { DocumentElement bindingElement = (DocumentElement) object; DocumentElement converterElement = bindingElement.getChild("Binding.converter", true); if (getValue() == null) { if (converterElement != null) { converterElement.remove(); } } else { if (m_namespace == null) { createNamespace(bindingElement.getRoot()); } String value = m_namespace + ":" + ClassUtils.getShortClassName(m_className); // if (converterElement == null) { converterElement = new DocumentElement("Binding.converter"); converterElement.addChild(new DocumentElement(value)); bindingElement.addChild(converterElement); } else { List<DocumentElement> children = converterElement.getChildren(); if (children.size() == 1) { children.get(0).setTag(value); } else { converterElement.removeChildren(); converterElement.addChild(new DocumentElement(value)); } } } } m_update = false; } }
From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.ValidationInfo.java
public void appendValue(StringBuffer value) throws Exception { if (m_update && !m_classNames.isEmpty()) { if (m_classNames.size() == 1) { String className = m_classNames.get(0); String namespace = m_packageToNamespace.get(ClassUtils.getPackageName(className)); value.append(", validationRule=" + StringUtils.defaultString(namespace) + ":" + ClassUtils.getShortClassName(className)); } else {//from ww w .j a v a 2 s . com value.append(", validationRules={"); int index = 0; for (String className : m_classNames) { if (index++ > 0) { value.append(", "); } String namespace = m_packageToNamespace.get(ClassUtils.getPackageName(className)); value.append( StringUtils.defaultString(namespace) + ":" + ClassUtils.getShortClassName(className)); } value.append("}"); } } }
From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.ValidationInfo.java
public void applyChanges(AbstractDocumentObject object) throws Exception { if (m_update) { if (object instanceof DocumentAttribute) { if (m_classNames.size() == 1) { String className = m_classNames.get(0); String namespace = m_packageToNamespace.get(ClassUtils.getPackageName(className)); if (namespace == null) { DocumentAttribute attribute = (DocumentAttribute) object; namespace = getNamespace(attribute.getEnclosingElement().getRoot(), className); // String value = attribute.getValue(); int index = value.indexOf("validationRule=") + 15; attribute.setValue(value.substring(0, index) + namespace + value.substring(index)); }/*w w w.j a v a 2 s. c om*/ } else { List<String> nonExistingNamespace = Lists.newArrayList(); DocumentAttribute attribute = (DocumentAttribute) object; for (String className : m_classNames) { String namespace = m_packageToNamespace.get(ClassUtils.getPackageName(className)); if (namespace == null || nonExistingNamespace.contains(namespace)) { if (namespace == null) { // create non-existing namespace namespace = getNamespace(attribute.getEnclosingElement().getRoot(), className); nonExistingNamespace.add(namespace); } // String value = attribute.getValue(); int index = value.indexOf(":" + ClassUtils.getShortClassName(className), value.indexOf("validationRules=") + 16); attribute.setValue(value.substring(0, index) + namespace + value.substring(index)); } } } } else { DocumentElement bindingElement = (DocumentElement) object; if (m_classNames.isEmpty()) { DocumentElement validationRules = bindingElement.getChild("Binding.validationRules", true); if (validationRules != null) { validationRules.remove(); } // DocumentElement validationRule = bindingElement.getChild("Binding.validationRule", true); if (validationRule != null) { validationRule.remove(); } } else { DocumentElement validationRule = bindingElement.getChild("Binding.validationRule", true); if (validationRule != null) { validationRule.remove(); } // DocumentElement validationRules = bindingElement.getChild("Binding.validationRules", true); if (validationRules == null) { validationRules = new DocumentElement("Binding.validationRule"); bindingElement.addChild(validationRules); } else { validationRules.removeChildren(); } // DocumentElement rootElement = bindingElement.getRoot(); // for (String className : m_classNames) { DocumentElement ruleElement = new DocumentElement(); ruleElement.setTag(getNamespace(rootElement, className) + ":" + ClassUtils.getShortClassName(className)); validationRules.addChild(ruleElement); } } } m_update = false; } }
From source file:org.eclipse.wb.internal.rcp.databinding.xwt.model.widgets.XmlObjectReferenceProvider.java
public static void generateName(XmlObjectInfo objectInfo) throws Exception { if (getName(objectInfo) == null) { final Set<String> variables = Sets.newHashSet(); //// w w w .j av a2 s . co m DocumentElement rootElement = objectInfo.getElement().getRoot(); final String[] xName = new String[1]; // for (DocumentAttribute attribute : rootElement.getDocumentAttributes()) { String name = attribute.getName(); String value = attribute.getValue(); if (name.startsWith(BeansObserveTypeContainer.NAMESPACE_KEY)) { if (value.equals("http://www.eclipse.org/xwt")) { xName[0] = name.substring(BeansObserveTypeContainer.NAMESPACE_KEY.length()) + ":Name"; break; } } } // rootElement.accept(new DocumentModelVisitor() { @Override public void visit(DocumentAttribute attribute) { if (attribute.getName().equalsIgnoreCase(xName[0])) { variables.add(attribute.getValue()); } } }); // String baseVariable = StringUtils .uncapitalize(ClassUtils.getShortClassName(objectInfo.getDescription().getComponentClass())); String variable = baseVariable; int variableIndex = 1; // ensure unique while (variables.contains(variable)) { variable = baseVariable + "_" + Integer.toString(variableIndex++); } // Property property = objectInfo.getPropertyByTitle("Name"); property.setValue(variable); } }
From source file:org.eclipse.wb.internal.rcp.databinding.xwt.ui.contentproviders.ValidationUiContentProvider.java
private ClassInfo createInfo(String className) { ClassInfo info = new ClassInfo(); info.className = className;// ww w . j a v a2s. c om // if (className.length() == 0) { info.message = Messages.ValidationUiContentProvider_noClass; } else { if (className.startsWith("{") && className.endsWith("}")) { return info; } // try { // check load class Class<?> testClass = loadClass(className); // check permissions int modifiers = testClass.getModifiers(); if (!Modifier.isPublic(modifiers)) { info.message = Messages.ValidationUiContentProvider_notPublicClass; return info; } if (Modifier.isAbstract(modifiers)) { info.message = Messages.ValidationUiContentProvider_abstractClass; return info; } // check constructor boolean noConstructor = true; try { testClass.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY); noConstructor = false; } catch (SecurityException e) { } catch (NoSuchMethodException e) { } // prepare error message for constructor if (noConstructor) { info.message = Messages.ValidationUiContentProvider_noPublicConstructor + ClassUtils.getShortClassName(className) + "()."; } } catch (ClassNotFoundException e) { info.message = Messages.ValidationUiContentProvider_notExistClass; } } return info; }
From source file:org.eclipse.wb.internal.swing.databinding.wizards.autobindings.SwingDatabindingProvider.java
public String performSubstitutions(String code, ImportsManager imports) throws Exception { // calculate states boolean blockMode = useBlockMode(); boolean lazyVariables = createLazyVariables(); ////from w w w . ja va2 s . com DataBindingsCodeUtils.ensureDBLibraries(m_javaProject); CodeGenerationSupport generationSupport = new CodeGenerationSupport(CoreUtils.useGenerics(m_javaProject)); // prepare imports Collection<String> importList = Sets.newHashSet(); importList.add("java.awt.GridBagLayout"); importList.add("java.awt.GridBagConstraints"); importList.add("java.awt.Insets"); importList.add("javax.swing.JLabel"); importList.add("org.jdesktop.beansbinding.AutoBinding"); importList.add("org.jdesktop.beansbinding.Bindings"); importList.add("org.jdesktop.beansbinding.BeanProperty"); // if (!generationSupport.useGenerics()) { importList.add("org.jdesktop.beansbinding.Property"); } // bean class, field, name, field access String beanClassName = m_beanClass.getName().replace('$', '.'); String beanClassShortName = ClassUtils.getShortClassName(beanClassName); String fieldPrefix = JavaCore.getOption(JavaCore.CODEASSIST_FIELD_PREFIXES); String fieldName = fieldPrefix + StringUtils.uncapitalize(beanClassShortName); // code = StringUtils.replace(code, "%BeanClass%", beanClassName); // if (ReflectionUtils.getConstructorBySignature(m_beanClass, "<init>()") == null) { code = StringUtils.replace(code, "%BeanField%", fieldName); } else { code = StringUtils.replace(code, "%BeanField%", fieldName + " = new " + beanClassName + "()"); } // IPreferenceStore preferences = ToolkitProvider.DESCRIPTION.getPreferences(); String accessPrefix = preferences.getBoolean(FieldUniqueVariableSupport.P_PREFIX_THIS) ? "this." : ""; String beanFieldAccess = accessPrefix + fieldName; // code = StringUtils.replace(code, "%BeanName%", StringUtils.capitalize(beanClassShortName)); code = StringUtils.replace(code, "%BeanFieldAccess%", accessPrefix + fieldName); // prepare properties final List<PropertyAdapter> properties = Lists.newArrayList(); Display.getDefault().syncExec(new Runnable() { public void run() { CollectionUtils.addAll(properties, m_propertiesViewer.getCheckedElements()); } }); // prepare code StringBuffer componentFields = new StringBuffer(); StringBuffer components = new StringBuffer(); String swingContainer = StringUtils.substringBetween(code, "%Components%", "%"); String swingContainerWithDot = "this".equals(swingContainer) ? "" : swingContainer + "."; // int propertiesCount = properties.size(); int lastPropertyIndex = propertiesCount - 1; StringBuffer bindings = new StringBuffer(); // prepare layout code components.append("\t\tGridBagLayout gridBagLayout = new GridBagLayout();\r\n"); components.append("\t\tgridBagLayout.columnWidths = new int[]{0, 0, 0};\r\n"); components.append("\t\tgridBagLayout.rowHeights = new int[]{" + StringUtils.repeat("0, ", propertiesCount) + "0};\r\n"); components.append("\t\tgridBagLayout.columnWeights = new double[]{0.0, 1.0, 1.0E-4};\r\n"); components.append("\t\tgridBagLayout.rowWeights = new double[]{" + StringUtils.repeat("0.0, ", propertiesCount) + "1.0E-4};\r\n"); components.append("\t\t" + swingContainerWithDot + "setLayout(gridBagLayout);\r\n"); // StringBuffer group = new StringBuffer(); generationSupport.generateLocalName("bindingGroup"); // StringBuffer lazy = new StringBuffer(); // for (int i = 0; i < propertiesCount; i++) { String index = Integer.toString(i); // PropertyAdapter property = properties.get(i); Object[] editorData = m_propertyToEditor.get(property); SwingComponentDescriptor componentDescriptor = (SwingComponentDescriptor) editorData[0]; // String propertyName = property.getName(); // label addLabelCode(componentFields, components, lazy, swingContainerWithDot, blockMode, lazyVariables, propertyName, index); // String componentClassName = componentDescriptor.getComponentClass(); String componentShortClassName = ClassUtils.getShortClassName(componentClassName); String componentFieldName = fieldPrefix + propertyName + componentShortClassName; String componentFieldAccess = accessPrefix + componentFieldName; String componentLazyAccess = "get" + StringUtils.capitalize(propertyName) + componentShortClassName + "()"; String componentAccess = lazyVariables ? componentLazyAccess : componentFieldAccess; // importList.add(componentClassName); // field componentFields .append("\r\nfield\r\n\tprivate " + componentShortClassName + " " + componentFieldName + ";"); // component addComponentCode(components, lazy, swingContainerWithDot, blockMode, lazyVariables, componentShortClassName, componentFieldAccess, componentLazyAccess, index); // binding properties AutoBindingUpdateStrategyDescriptor strategyDescriptor = (AutoBindingUpdateStrategyDescriptor) editorData[1]; // String modelPropertyName = generationSupport.generateLocalName(propertyName, "Property"); String targetPropertyName = generationSupport.generateLocalName(componentDescriptor.getName(1), "Property"); String bindingName = generationSupport.generateLocalName("autoBinding"); String modelGeneric = null; String targetGeneric = null; // if (generationSupport.useGenerics()) { modelGeneric = beanClassName + ", " + GenericUtils.convertPrimitiveType(property.getType().getName()); bindings.append("\t\tBeanProperty<" + modelGeneric + "> "); } else { bindings.append("\t\tProperty "); } bindings.append(modelPropertyName + " = BeanProperty.create(\"" + propertyName + "\");\r\n"); // if (generationSupport.useGenerics()) { targetGeneric = componentDescriptor.getComponentClass() + ", " + componentDescriptor.getPropertyClass(); bindings.append("\t\tBeanProperty<" + targetGeneric + "> "); } else { bindings.append("\t\tProperty "); } bindings.append( targetPropertyName + " = BeanProperty.create(\"" + componentDescriptor.getName(1) + "\");\r\n"); // binding bindings.append("\t\tAutoBinding"); if (generationSupport.useGenerics()) { bindings.append("<" + modelGeneric + ", " + targetGeneric + ">"); } bindings.append(" " + bindingName + " = Bindings.createAutoBinding(" + strategyDescriptor.getSourceCode() + ", " + beanFieldAccess + ", " + modelPropertyName + ", " + componentAccess + ", " + targetPropertyName + ");\r\n"); bindings.append("\t\t" + bindingName + ".bind();"); // group.append("\t\tbindingGroup.addBinding(" + bindingName + ");"); // if (i < lastPropertyIndex) { componentFields.append("\r\n"); components.append("\r\n"); bindings.append("\r\n\t\t//\r\n"); group.append("\r\n"); } // } // replace template patterns code = StringUtils.replace(code, "%ComponentFields%", componentFields.toString()); code = StringUtils.replace(code, "%Components%" + swingContainer + "%", components.toString()); code = StringUtils.replace(code, "%Bindings%", bindings.toString()); code = StringUtils.replace(code, "%Group%", group.toString()); code = StringUtils.replace(code, "%LAZY%", lazy.toString()); // add imports for (String qualifiedTypeName : importList) { imports.addImport(qualifiedTypeName); } // return code; }
From source file:org.jbpm.bpel.graph.exe.MockIntegrationService.java
private static String getIdString(Object objectWithId) { Class classWithId = objectWithId.getClass(); // get the class name excluding the package String className = ClassUtils.getShortClassName(classWithId); // obtain the identifier getter method according to jBPM conventions String idString = "0"; try {/*from w w w . j a v a 2 s . c om*/ Method idGetter = classWithId.getMethod("getId", null); idGetter.setAccessible(true); Long idWrapper = (Long) idGetter.invoke(objectWithId, null); long id = idWrapper.longValue(); if (id != 0L) { idString = Long.toHexString(id); } else { // object is transient, fall back to hash code idString = Integer.toHexString(objectWithId.hashCode()); } } catch (NoSuchMethodException e) { // no id getter, fall back to hash code idString = Integer.toHexString(objectWithId.hashCode()); } catch (IllegalAccessException e) { // we made the getter accessible, should not happen e.printStackTrace(); } catch (InvocationTargetException e) { // strange a getter throws a checked exception e.printStackTrace(); } return className + idString; }