src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/FunctionAvailableCall.java
author joehw
Wed, 18 Oct 2017 13:25:49 -0700
changeset 47359 e1a6c0168741
parent 47216 71c04702a3d5
child 47712 bde0215f1f70
permissions -rw-r--r--
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked Reviewed-by: lancea, rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     3
 * @LastModified: Oct 2017
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xalan.internal.xsltc.compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.bcel.internal.generic.PUSH;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    26
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    33
import java.lang.reflect.Method;
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    34
import java.lang.reflect.Modifier;
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    35
import java.util.List;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * @author G. Todd Miller
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
final class FunctionAvailableCall extends FunctionCall {
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
    private Expression _arg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
    private String     _nameOfFunct = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
    private String     _namespaceOfFunct = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
    private boolean    _isFunctionAvailable = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
     * Constructs a FunctionAvailableCall FunctionCall. Takes the
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
     * function name qname, for example, 'function-available', and
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
     * a list of arguments where the arguments must be instances of
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
     * LiteralExpression.
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    54
    public FunctionAvailableCall(QName fname, List<Expression> arguments) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
        super(fname, arguments);
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    56
        _arg = (Expression)arguments.get(0);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
        _type = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
        if (_arg instanceof LiteralExpr) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
            LiteralExpr arg = (LiteralExpr) _arg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
            _namespaceOfFunct = arg.getNamespace();
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
            _nameOfFunct = arg.getValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
            if (!isInternalNamespace()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
              _isFunctionAvailable = hasMethods();
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
     * Argument of function-available call must be literal, typecheck
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
     * returns the type of function-available to be boolean.
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
        if (_type != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
           return _type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
        if (_arg instanceof LiteralExpr) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
            return _type = Type.Boolean;
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
        ErrorMsg err = new ErrorMsg(ErrorMsg.NEED_LITERAL_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
                        "function-available", this);
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
        throw new TypeCheckError(err);
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     * Returns an object representing the compile-time evaluation
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
     * of an expression. We are only using this for function-available
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
     * and element-available at this time.
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    public Object evaluateAtCompileTime() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
        return getResult() ? Boolean.TRUE : Boolean.FALSE;
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
     * for external java functions only: reports on whether or not
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
     * the specified method is found in the specifed class.
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    private boolean hasMethods() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
        LiteralExpr arg = (LiteralExpr)_arg;
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
        // Get the class name from the namespace uri
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
        String className = getClassNameFromUri(_namespaceOfFunct);
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
        // Get the method name from the argument to function-available
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
        String methodName = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        int colonIndex = _nameOfFunct.indexOf(":");
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
        if (colonIndex > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
          String functionName = _nameOfFunct.substring(colonIndex+1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
          int lastDotIndex = functionName.lastIndexOf('.');
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
          if (lastDotIndex > 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
            methodName = functionName.substring(lastDotIndex+1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
            if (className != null && !className.equals(""))
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
              className = className + "." + functionName.substring(0, lastDotIndex);
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
              className = functionName.substring(0, lastDotIndex);
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
          else
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
            methodName = functionName;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
          methodName = _nameOfFunct;
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
        if (className == null || methodName == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
        // Replace the '-' characters in the method name
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
        if (methodName.indexOf('-') > 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
          methodName = replaceDash(methodName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        try {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   133
            final Class<?> clazz = ObjectFactory.findProviderClass(className, true);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
            if (clazz == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
                return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
            final Method[] methods = clazz.getMethods();
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
            for (int i = 0; i < methods.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
                final int mods = methods[i].getModifiers();
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
                if (Modifier.isPublic(mods) && Modifier.isStatic(mods)
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
                        && methods[i].getName().equals(methodName))
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
                {
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
                    return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
        catch (ClassNotFoundException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
          return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
     * Reports on whether the function specified in the argument to
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
     * xslt function 'function-available' was found.
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    public boolean getResult() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
        if (_nameOfFunct == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
            return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
        if (isInternalNamespace()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
            final Parser parser = getParser();
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
            _isFunctionAvailable =
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
                parser.functionSupported(Util.getLocalName(_nameOfFunct));
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
        return _isFunctionAvailable;
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     * Return true if the namespace uri is null or it is the XSLTC translet uri.
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
    private boolean isInternalNamespace() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
        return (_namespaceOfFunct == null ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
            _namespaceOfFunct.equals(EMPTYSTRING) ||
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
            _namespaceOfFunct.equals(TRANSLET_URI));
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
     * Calls to 'function-available' are resolved at compile time since
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
     * the namespaces declared in the stylsheet are not available at run
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
     * time. Consequently, arguments to this function must be literals.
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
        methodGen.getInstructionList().append(new PUSH(cpg, getResult()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
}