PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/cnd.simpleunit/src/org/netbeans/modules/cnd/simpleunit/utils/CodeGenerationUtils.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 140 lines | 89 code | 7 blank | 44 comment | 16 complexity | 311b408ffe0ed2308c8dee205d7c9304 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2010 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.cnd.simpleunit.utils;
  43. import java.util.Collection;
  44. import org.netbeans.modules.cnd.api.model.CsmClass;
  45. import org.netbeans.modules.cnd.api.model.CsmFunction;
  46. import org.netbeans.modules.cnd.api.model.CsmMethod;
  47. import org.netbeans.modules.cnd.api.model.CsmParameter;
  48. import org.netbeans.modules.cnd.api.model.util.CsmBaseUtilities;
  49. import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
  50. /**
  51. * @author Nikolay Krasilnikov (nnnnnk@netbeans.org)
  52. */
  53. public class CodeGenerationUtils {
  54. private CodeGenerationUtils() {
  55. }
  56. public static String generateFunctionDeclaration(CsmFunction fun) {
  57. String returnType = fun.getReturnType().getText().toString();
  58. StringBuilder functionDecl = new StringBuilder(""); // NOI18N
  59. functionDecl.append(returnType).append(" ") // NOI18N
  60. .append(fun.getQualifiedName()).append("("); // NOI18N
  61. Collection<CsmParameter> params = fun.getParameters();
  62. int i = 0;
  63. for (CsmParameter param : params) {
  64. if (i != 0) {
  65. functionDecl.append(", "); // NOI18N
  66. }
  67. functionDecl.append(param.getText().toString()); // NOI18N
  68. i++;
  69. }
  70. functionDecl.append(");"); // NOI18N
  71. return functionDecl.toString();
  72. }
  73. public static String generateParameterDeclaration(CsmParameter param, int paramNumber) {
  74. StringBuilder paramDecl = new StringBuilder(""); // NOI18N
  75. paramDecl.append(generateCanonicalParameterTypeAndName(param, paramNumber))
  76. .append(";"); // NOI18N
  77. return paramDecl.toString();
  78. }
  79. private static String generateCanonicalParameterTypeAndName(CsmParameter param, int paramNumber) {
  80. String paramName = param.getName().toString();
  81. if(paramName == null || paramName.isEmpty()) {
  82. paramName = "p" + paramNumber; // NOI18N
  83. }
  84. StringBuilder paramTypeAndName = new StringBuilder(""); // NOI18N
  85. String paramType = param.getType().getCanonicalText().toString();
  86. paramTypeAndName.append(paramType) // NOI18N
  87. .append(" ").append(paramName); // NOI18N
  88. return paramTypeAndName.toString();
  89. }
  90. public static String generateFunctionCall(CsmFunction fun) {
  91. StringBuilder functionCall = new StringBuilder(""); // NOI18N
  92. String returnType = fun.getReturnType().getText().toString();
  93. if (CsmKindUtilities.isMethod(fun)) {
  94. CsmMethod method = (CsmMethod) CsmBaseUtilities.getFunctionDeclaration(fun);
  95. CsmClass cls = method.getContainingClass();
  96. if (cls != null) {
  97. String clsName = cls.getName().toString();
  98. String clsVarName =
  99. Character.toLowerCase(clsName.charAt(0))
  100. + clsName.substring(1);
  101. clsVarName = (clsVarName.equals(clsName)) ? '_' + clsVarName : clsVarName; // NOI18N
  102. functionCall.append(" ") // NOI18N
  103. .append(cls.getQualifiedName()) // NOI18N
  104. .append(" ") // NOI18N
  105. .append(clsVarName); // NOI18N
  106. if (!CsmKindUtilities.isConstructor(method)) {
  107. functionCall.append(";\n"); // NOI18N
  108. functionCall.append(" ") // NOI18N
  109. .append(((!"void".equals(returnType)) ? returnType + " result = " : "")) // NOI18N
  110. .append(clsVarName) // NOI18N
  111. .append(".") // NOI18N
  112. .append(method.getName()); // NOI18N
  113. }
  114. }
  115. } else {
  116. functionCall.append(" ").append(((!"void".equals(returnType)) ? returnType + " result = " : "")) // NOI18N
  117. .append(fun.getName()); // NOI18N
  118. }
  119. int i = 0;
  120. functionCall.append("("); // NOI18N
  121. Collection<CsmParameter> params = fun.getParameters();
  122. for (CsmParameter param : params) {
  123. if (i != 0) {
  124. functionCall.append(", "); // NOI18N
  125. }
  126. String paramName = param.getName().toString();
  127. functionCall.append(((paramName != null && !paramName.isEmpty()) ? paramName : "p" + i)); // NOI18N
  128. i++;
  129. }
  130. functionCall.append(");\n"); // NOI18N
  131. return functionCall.toString();
  132. }
  133. }