| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| |
| package org.eclipse.osbp.runtime.web.ecview.services.vaadin.impl; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import org.eclipse.xtext.common.types.JvmDeclaredType; |
| import org.eclipse.xtext.common.types.JvmFeature; |
| import org.eclipse.xtext.common.types.JvmOperation; |
| import org.eclipse.xtext.common.types.JvmVisibility; |
| import org.eclipse.xtext.xbase.lib.StringExtensions; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * The Class OperationExtensions. |
| */ |
| public class OperationExtensions { |
| |
| /** |
| * Normalizes the method name. |
| * |
| * @param simpleName |
| * the simple name |
| * @return the string |
| */ |
| public static String toPropertyName(String simpleName) { |
| if (simpleName == null) { |
| return null; |
| } |
| String tempName = null; |
| if (isSetter(simpleName)) { |
| tempName = StringExtensions.toFirstLower(simpleName.replaceFirst( |
| "set", "")); |
| } else if (isGetter(simpleName)) { |
| if (simpleName.startsWith("get")) { |
| tempName = StringExtensions.toFirstLower(simpleName |
| .replaceFirst("get", "")); |
| } else { |
| tempName = StringExtensions.toFirstLower(simpleName |
| .replaceFirst("is", "")); |
| } |
| } |
| return tempName; |
| } |
| |
| /** |
| * Checks if is getter. |
| * |
| * @param simpleName |
| * the simple name |
| * @return true, if is getter |
| */ |
| public static boolean isGetter(String simpleName) { |
| if (simpleName == null) { |
| return false; |
| } |
| return simpleName.startsWith("get") || simpleName.startsWith("is"); |
| } |
| |
| /** |
| * Checks if is setter. |
| * |
| * @param simpleName |
| * the simple name |
| * @return true, if is setter |
| */ |
| public static boolean isSetter(String simpleName) { |
| return simpleName != null && simpleName.startsWith("set"); |
| } |
| |
| /** |
| * Calculates the operation infos for the given type. |
| * |
| * @param type |
| * the type |
| * @return the operation infos |
| */ |
| public static Map<String, OperationInfo> getOperationInfos( |
| JvmDeclaredType type) { |
| return getOperationInfos(type, null); |
| } |
| |
| /** |
| * Calculates the operation infos for the given type. |
| * |
| * @param type |
| * the type |
| * @param filterName |
| * - is used to filter only methods property names matching the |
| * filter name. |
| * @return the operation infos |
| */ |
| public static Map<String, OperationInfo> getOperationInfos( |
| JvmDeclaredType type, String filterName) { |
| Map<String, OperationInfo> infos = new HashMap<String, OperationInfo>(); |
| for (JvmFeature feature : type.getAllFeatures()) { |
| if (!(feature instanceof JvmOperation)) { |
| continue; |
| } |
| |
| JvmOperation operation = (JvmOperation) feature; |
| if (operation.getVisibility() != JvmVisibility.PUBLIC) { |
| continue; |
| } |
| |
| if (!operation.getParameters().isEmpty()) { |
| continue; |
| } |
| |
| String propertyName = toPropertyName(operation.getSimpleName()); |
| if (filterName != null && !filterName.equals(propertyName)) { |
| continue; |
| } |
| |
| if (!isGetter(operation.getSimpleName()) |
| && !isSetter(operation.getSimpleName())) { |
| continue; |
| } |
| |
| String id = calcId(operation.getDeclaringType(), |
| operation.getSimpleName()); |
| if (!infos.containsKey(id)) { |
| OperationInfo info = new OperationInfo(); |
| info.id = id; |
| info.name = propertyName; |
| infos.put(id, info); |
| } |
| |
| OperationInfo info = infos.get(id); |
| if (isGetter(operation.getSimpleName())) { |
| info.getter = operation; |
| } else { |
| info.setter = operation; |
| } |
| } |
| |
| // apply readonly and create descriptions |
| for (OperationInfo info : infos.values()) { |
| if (info.getter == null) { |
| continue; |
| } |
| if (info.setter == null) { |
| info.readonly = true; |
| } |
| } |
| return infos; |
| } |
| |
| /** |
| * Normalizes the name. |
| * |
| * @param declaringType |
| * the declaring type |
| * @param simpleName |
| * the simple name |
| * @return the string |
| */ |
| public static String calcId(JvmDeclaredType declaringType, String simpleName) { |
| String tempName = toPropertyName(simpleName); |
| if (tempName == null) { |
| return null; |
| } |
| |
| return declaringType.getQualifiedName() + ":" + tempName; |
| } |
| |
| /** |
| * The Class OperationInfo. |
| */ |
| public static class OperationInfo { |
| |
| /** The id. */ |
| private String id; |
| |
| /** The name. */ |
| private String name; |
| |
| /** The readonly. */ |
| private boolean readonly; |
| |
| /** The getter. */ |
| private JvmOperation getter; |
| |
| /** The setter. */ |
| private JvmOperation setter; |
| |
| /** |
| * Gets the id. |
| * |
| * @return the id |
| */ |
| public String getId() { |
| return id; |
| } |
| |
| /** |
| * Sets the id. |
| * |
| * @param id |
| * the new id |
| */ |
| public void setId(String id) { |
| this.id = id; |
| } |
| |
| /** |
| * Gets the name. |
| * |
| * @return the name |
| */ |
| public String getName() { |
| return name; |
| } |
| |
| /** |
| * Sets the name. |
| * |
| * @param name |
| * the new name |
| */ |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| /** |
| * Checks if is readonly. |
| * |
| * @return true, if is readonly |
| */ |
| public boolean isReadonly() { |
| return readonly; |
| } |
| |
| /** |
| * Sets the readonly. |
| * |
| * @param readonly |
| * the new readonly |
| */ |
| public void setReadonly(boolean readonly) { |
| this.readonly = readonly; |
| } |
| |
| /** |
| * Gets the getter. |
| * |
| * @return the getter |
| */ |
| public JvmOperation getGetter() { |
| return getter; |
| } |
| |
| /** |
| * Sets the getter. |
| * |
| * @param getter |
| * the new getter |
| */ |
| public void setGetter(JvmOperation getter) { |
| this.getter = getter; |
| } |
| |
| /** |
| * Gets the setter. |
| * |
| * @return the setter |
| */ |
| public JvmOperation getSetter() { |
| return setter; |
| } |
| |
| /** |
| * Sets the setter. |
| * |
| * @param setter |
| * the new setter |
| */ |
| public void setSetter(JvmOperation setter) { |
| this.setter = setter; |
| } |
| |
| } |
| |
| } |