ada.editor/src/org/netbeans/modules/ada/editor/CodeUtils.java
author Andrea Lucarelli <raster@netbeans.org>
Sun, 22 Aug 2010 23:37:11 +0200
branchrelease68
changeset 16367 d2820c029d3a
parent 15779 367c7fdb5d23
permissions -rw-r--r--
Add JVM compiler support.
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
     5  *
     6  * The contents of this file are subject to the terms of either the GNU
     7  * General Public License Version 2 only ("GPL") or the Common
     8  * Development and Distribution License("CDDL") (collectively, the
     9  * "License"). You may not use this file except in compliance with the
    10  * License. You can obtain a copy of the License at
    11  * http://www.netbeans.org/cddl-gplv2.html
    12  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    13  * specific language governing permissions and limitations under the
    14  * License.  When distributing the software, include this License Header
    15  * Notice in each file and include the License file at
    16  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    17  * particular file as subject to the "Classpath" exception as provided
    18  * by Sun in the GPL Version 2 section of the License file that
    19  * accompanied this code. If applicable, add the following below the
    20  * License Header, with the fields enclosed by brackets [] replaced by
    21  * your own identifying information:
    22  * "Portions Copyrighted [year] [name of copyright owner]"
    23  *
    24  * If you wish your version of this file to be governed by only the CDDL
    25  * or only the GPL Version 2, indicate your decision by adding
    26  * "[Contributor] elects to include this software in this distribution
    27  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    28  * single choice of license, a recipient has the option to distribute
    29  * your version of this file under either the CDDL, the GPL Version 2 or
    30  * to extend the choice of license to its licensees as provided above.
    31  * However, if you add GPL Version 2 code and therefore, elected the GPL
    32  * Version 2 license, then the option applies only if the new code is
    33  * made subject to such option by the copyright holder.
    34  *
    35  * Contributor(s):
    36  *
    37  * Portions Copyrighted 2008 Sun Microsystems, Inc.
    38  */
    39 package org.netbeans.modules.ada.editor;
    40 
    41 import org.netbeans.api.annotations.common.CheckForNull;
    42 import org.netbeans.modules.ada.editor.ast.nodes.Expression;
    43 import org.netbeans.modules.ada.editor.ast.nodes.FormalParameter;
    44 import org.netbeans.modules.ada.editor.ast.nodes.Identifier;
    45 import org.netbeans.modules.ada.editor.ast.nodes.MethodDeclaration;
    46 import org.netbeans.modules.ada.editor.ast.nodes.MethodInvocation;
    47 import org.netbeans.modules.ada.editor.ast.nodes.NameBase;
    48 import org.netbeans.modules.ada.editor.ast.nodes.PackageBody;
    49 import org.netbeans.modules.ada.editor.ast.nodes.PackageName;
    50 import org.netbeans.modules.ada.editor.ast.nodes.PackageSpecification;
    51 import org.netbeans.modules.ada.editor.ast.nodes.SubprogramBody;
    52 import org.netbeans.modules.ada.editor.ast.nodes.SubprogramSpecification;
    53 import org.netbeans.modules.ada.editor.ast.nodes.TypeAccess;
    54 import org.netbeans.modules.ada.editor.ast.nodes.TypeDeclaration;
    55 import org.netbeans.modules.ada.editor.ast.nodes.TypeName;
    56 import org.netbeans.modules.ada.editor.ast.nodes.Variable;
    57 
    58 /**
    59  * Based on org.netbeans.modules.php.editor.CodeUtils
    60  *
    61  * @author Andrea Lucarelli
    62  */
    63 public class CodeUtils {
    64 
    65     public static final String FUNCTION_TYPE_PREFIX = "@fn:";
    66     public static final String PROCEDURE_TYPE_PREFIX = "@prc:";
    67     public static final String METHOD_TYPE_PREFIX = "@mtd:";
    68 
    69     private CodeUtils() {
    70     }
    71 
    72     public static String extractPackageName(PackageName pkgName) {
    73         Expression name = pkgName.getPackageName();
    74 
    75         assert name instanceof Identifier :
    76                 "unsupported type of PackageName.getName(): " + name.getClass().getName();
    77 
    78         return (name instanceof Identifier) ? ((Identifier) name).getName() : "";//NOI18N
    79     }
    80 
    81     public static String extractPackageName(PackageSpecification pkgSpecification) {
    82         return pkgSpecification.getName().getName();
    83     }
    84 
    85     public static String extractPackageName(PackageBody pkgBody) {
    86         return pkgBody.getName().getName();
    87     }
    88 
    89     public static String extractSubprogramName(SubprogramSpecification subprogramSpecification){
    90         return subprogramSpecification.getSubprogramName().getName();
    91     }
    92 
    93     public static String extractProcedureName(SubprogramBody subprogramBody){
    94         return subprogramBody.getSubprogramSpecification().getSubprogramName().getName();
    95     }
    96 
    97     public static String extractMethodName(MethodDeclaration methodDeclaration) {
    98         return methodDeclaration.getMethodName();
    99     }
   100 
   101     @CheckForNull
   102     public static String extractVariableName(Variable var) {
   103         if (var.getName() instanceof Identifier) {
   104             Identifier id = (Identifier) var.getName();
   105             StringBuilder varName = new StringBuilder();
   106 
   107             varName.append(id.getName());
   108             return varName.toString();
   109         }
   110 
   111         return null;
   112     }
   113 
   114     @CheckForNull
   115     public static String extractTypeName(TypeDeclaration var) {
   116         if (var.getTypeName() instanceof Identifier) {
   117             Identifier id = (Identifier) var.getTypeName();
   118             StringBuilder varName = new StringBuilder();
   119 
   120             varName.append(id.getName());
   121             return varName.toString();
   122         }
   123 
   124         return null;
   125     }
   126 
   127     @CheckForNull
   128     public static String extractTypeName(NameBase type) {
   129         if (type instanceof TypeName) {
   130             Identifier id = ((TypeName)type).getTypeName();
   131             StringBuilder typeName = new StringBuilder();
   132             typeName.append(id.getName());
   133             return typeName.toString();
   134         } else if (type instanceof TypeAccess) {
   135             NameBase name = ((TypeAccess) type).getMember();
   136             return extractTypeName(name);
   137         }
   138 
   139         return null;
   140     }
   141 
   142     @CheckForNull
   143     public static TypeName extractType(NameBase type) {
   144         if (type instanceof TypeName) {
   145             return (TypeName)type;
   146         } else if (type instanceof TypeAccess) {
   147             NameBase name = ((TypeAccess) type).getMember();
   148             return extractType(name);
   149         }
   150 
   151         return null;
   152     }
   153 
   154     public static String getParamDisplayName(FormalParameter param) {
   155         Variable var = param.getParameterName();
   156         StringBuilder paramName = new StringBuilder();
   157 
   158 		Identifier id = (Identifier) var.getName();
   159 		paramName.append(id.getName());
   160 
   161         return paramName.length() == 0 ? null : paramName.toString();
   162     }
   163 }