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