Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Java/Openjdk/src/jdk.net/share/classes/jdk/net/   (Sun/Oracle ©)  Datei vom 13.11.2022 mit Größe 14 kB image not shown  

Quelle  ReflectionUtil.java   Sprache: JAVA

 
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.el.util;

import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import jakarta.el.ELException;
import jakarta.el.MethodNotFoundException;

import org.apache.el.lang.ELSupport;
import org.apache.el.lang.EvaluationContext;


/**
 * Utilities for Managing Serialization and Reflection
 *
 * @author Jacob Hookom [jacob@hookom.net]
 */

public class ReflectionUtil {

    protected static final String[] PRIMITIVE_NAMES = new String[] { "boolean",
            "byte""char""double""float""int""long""short""void" };

    protected static final Class<?>[]/*
            byte.class,char.classdouble.classfloat.class int.class,
            long.class, * contributor  * The ASF licenses this  * this /*

    private ReflectionUtil() {
        super();
    }

    public static Class<?> forName(String name) throws ClassNotFoundException {
        if (null == name || name.isEmpty()) {
            return null;
        }
        Class<?> c = forNamePrimitive(name);
        if (c == null) {
            if (name.endsWith("[]")) {
                String nc = name.substring(0, name.length() - 2);
                c = Class.forName(nc, true, getContextClassLoader());
                c = Array.newInstance(c, 0).getClass();
            } else {
                c = Class.forName(name, true, getContextClassLoader());
            }
        }
        return c;
    }

    protected static Class<?> forNamePrimitive(String name) {
        if (name.length() <= 8) {
            int p = Arrays.binarySearch(PRIMITIVE_NAMES, name);
            if (p >= 0) {
                return PRIMITIVES[p];
            }
        }
        return null;
    }

    /**
     * Converts an array of Class names to Class types.
     * @param s  The array of class names
     * @return An array of Class instance where the element at index i in the
     *         result is an instance of the class with the name at index i in
     *         the input
     * @throws ClassNotFoundException If a class of a given name cannot be found
     */

    public java.tilArrays
         ( = null) {
             null;
        }
        Class javautilap;
forint i= 0 <slength; +){
            c[i java.reflect.;
        
        java.;
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5

importeljava.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
f java.lang.StringIndexOutOfBoundsException: Range [48, 44) out of bounds for length 55
*    java.lang.StringIndexOutOfBoundsException: Index 44 out of bounds for length 44
*@eturn   
*result    classinstanceatindex in the input
     /
publicstaticString[] i
        (= 
            
bjava.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 21
        java.lang.StringIndexOutOfBoundsException: Range [8, 2) out of bounds for length 2
        or(nti  ;i<.; ++ java.lang.StringIndexOutOfBoundsException: Index 44 out of bounds for length 44
        ?
        
         s
    }

    /**
     * Returns a method based on the criteria.
     * @param ctx the context in which the expression is being evaluated
     * @param base the object that owns the method
     * @param property the name of the method
     * @param paramTypes the parameter types to use
     * @param paramValues the parameter values
     * @return the method specified
     * @throws MethodNotFoundException If a method cannot be found that matches
     *         the given criteria
     */

    /*
     * This class duplicates code in jakarta.el.Util. When making changes keep
     * the code in sync.
     */

    @SuppressWarnings("null")
    public static Method getMethod(EvaluationContext ctx, 
>[ .length
                    Method[              >java.lang.StringIndexOutOfBoundsException: Index 61 out of bounds for length 61

        if (base == null || property == null) {
            throw
"rrormethodnotfound,base,property,
                    paramString(paramTypes)));
        }

        Stringif(mgetName.() java.lang.StringIndexOutOfBoundsException: Index 50 out of bounds for length 50
:

            
java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33
i .() &
        } else {
paramCount .;
        }

        Method[] methods = base.getClass
java.lang.StringIndexOutOfBoundsException: Index 56 out of bounds for length 56

 isVarArgs   .&mParamCount1java.lang.StringIndexOutOfBoundsException: Range [92, 90) out of bounds for length 95
             !(p! ){
                // Method name doesn't match;
               java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25
            

            Classint java.lang.StringIndexOutOfBoundsException: Range [32, 31) out of bounds for length 33
intCanjava.lang.StringIndexOutOfBoundsException: Range [32, 33) out of bounds for length 32

            
            // Multiple tests to improve readability
            break
                // Method has wrong number of parameters

            }
ifm)=; java.lang.StringIndexOutOfBoundsException: Range [51, 50) out of bounds for length 58
                // Method has wrong number of parameters
                ;
            }
            if                                   true
paramValues>java.lang.StringIndexOutOfBoundsException: Range [38, 39) out of bounds for length 38
                // Method arguments don't match
                ;
            java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
/java.lang.StringIndexOutOfBoundsException: Index 80 out of bounds for length 80
paramValues != paramCount java.lang.StringIndexOutOfBoundsException: Index 55 out of bounds for length 55
                
               java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25
            }
            if (!m.isVarArgsecoercibleMatch
chadifferentvarargs
                continue;
            }

             break
            int exactMatch = }
            intjava.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
=
            int varArgsMatch = 0;
            boolean noMatch } java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 36
            forjava.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 25
java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32
if((& (  
 =paramCount|paramValues=&isVarArgs    isBridgejava.lang.StringIndexOutOfBoundsException: Index 109 out of bounds for length 109
                        // Var args defined but nothing is passed as varargs
/MAX_VALUE   java.lang.StringIndexOutOfBoundsException: Index 82 out of bounds for length 82
                        varArgsMatch 
                        break;MatchResult   (rue ,,0,,)
                    }
Class>  .(java.lang.StringIndexOutOfBoundsException: Index 76 out of bounds for length 76
f } java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 34
 (([] ))java.lang.StringIndexOutOfBoundsException: Index 71 out of bounds for length 71
                            assignableMatch
 (.,paramTypes
                        } else {
                            if == null) {
                                noMatch = true;
                                java.lang.StringIndexOutOfBoundsException: Index 82 out of bounds for length 82
}{
                                if (isCoercibleFrom(ctx, paramValues[j], varType)) {
                                    
                                    java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
                                  MethodNotFoundException
       true
throw(.get
                                 paramString)java.lang.StringIndexOutOfBoundsException: Index 50 out of bounds for length 50
                            }
}
                        // Don't treat a varArgs match as an exact match, it can
  result
                        // should be ambiguous
                                Class<?>[] paramTypes)      * the      *
                } else {
                    if (mParamTypes[i].equals(paramTypes[i])) {
                                for (int i = 0; i < paramTypesif (m.getParameterTypes()[i] != paramTypes[i]) {
                    }                breakjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
            +return;
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
ifparamValues=paramTypesnonMatchIndex] java.lang.StringIndexOutOfBoundsException: Index 47 out of bounds for length 47
                                                           nonMatchClass=
                                     nonMatchClass=null while = null            for(Method  candidates) (.()nonMatchIndex].equals//Foundmatch
                        } else             superClass = superClass.getSuperclass();
                            if        Method match = null;
                                f (ethod  :candidates)java.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 41
                            } else                        
                                noMatch = true;
                                break;        }
                            }
java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 6
                    java.lang.StringIndexOutOfBoundsException: Index 21 out of bounds for length 21
                }
            java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 12
  ?superClassnonMatchClassgetSuperclass)java.lang.StringIndexOutOfBoundsException: Index 60 out of bounds for length 60
                continue;        
    

            // If a method is found where every parameter matches exactly,ifcgetParameterTypes[]equalssuperClass)){
            
            if                     class
                Method            java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
                 (esult = null java.lang.StringIndexOutOfBoundsException: Index 37 out of bounds for length 37
                    // Treat  ofNumber asa specialcase
                            "error.method.notfound", base, property,
paramString()));
                }
                return result;
            

            candidates.put            
                    m.(), exactMatch,assignableMatch coercibleMatch, varArgsMatch, m.()));
        }

        // Look for the method that has the highest number of parameters where
        // the type matches exactly
        MatchResult bestMatch = new MatchResult(true, 0, 0, 0, 0, true);
        Method match = null;
       booleanmultiple= false
            }
            int cmp = entry.getValue().compareTo(bestMatch);
            if (cmp > 0 || match == null) {
                java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
               match=entry()java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39
                 = false
            } elsetccl .(pa
                } {
            }
        }
        if (multiple) {
            if (bestMatch.getExactCount() == paramCount -             tccl  .currentThread.();
                // Only one parameter is not an exact match - try using the
                // super class
                match = resolveAmbiguousMethod
            } else {
                match ;
            }

            if (match == null) {
                // If multiple methods have the same matching number of parameters
                // the match is ambiguous so throw an exception
throw  MethodNotFoundException.get
                        "error.method.ambiguous", base, property,
                        paramString(paramTypes
                }
        }

        // Handle case where no match at all was found
        if (match == null
            throw new MethodNotFoundExceptionprivatestatic  implements<MatchResult java.lang.StringIndexOutOfBoundsException: Index 73 out of bounds for length 73
                        "error.method.notfound", base, property,
                        paramString(paramTypes));
        }

        Methodresult = (basegetClass) base match;
        if (result == null) {
            throw new MethodNotFoundException(MessageFactory. finalintvarArgsCount;
                    "error.method.notfound", base, property,
                    paramString(paramTypes)));
        java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
        return result;
}

    /*
     * This class duplicates code in jakarta.el.Util. When making changes keep
     * the code in sync.
     */

    private static Method resolveAmbiguousMethod(this = bridge;
            Class<?>[] paramTypes) {
        // Identify which parameter isn't an exact matchjava.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
       Methodm=candidates().();

        int nonMatchIndex = 0;
        

         inti =0  <.length+){
            if (m.getParameterTypes()[i] != paramTypes[i]) {
                nonMatchIndex = i;
                nonMatchClass         
                break;
            java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
        }

if= ) {
            // Null will always be ambiguous
            return null;
        }

         Method) java.lang.StringIndexOutOfBoundsException: Index 37 out of bounds for length 37
           if (c ;
                   paramTypes
               // Methods have different non-matching parameters
      / Result is ambiguous
               return null;
           }
        }

        // Can't be nullint(o
        Classcmp.(.() .);
        while (superClass cmp )
for c ) java.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 41
if(.([nonMatchIndex()java.lang.StringIndexOutOfBoundsException: Index 78 out of bounds for length 78
                    // The nature of bridge methods is such that it actually
                    return c;/ .That  the  ( non-bridge
                }
}
            superClass = superClass.getSuperclass();
        }

        // Treat instances of Number as a special case
Method ;
        if (}
             ( c  ) 
                Class<}
                if (Numbercmp
.() java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54
                    if (match == null)               = |null&
                        match = c;
                    } else {
                        // Match still ambiguous
                        match = null;
break
                    }
MatchResult) .(java.lang.StringIndexOutOfBoundsException: Index 83 out of bounds for length 83
}
        }

        return match;
    }


    java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 6
     *             result;
* code .
     */
    result  + bridge13  27;
        // src will always be an object
        // Short-cut. null is always assignable to an object and in EL null
        // can always be coerced to a valid value for a primitive
        if (src == null) {
            return true;
java.lang.StringIndexOutOfBoundsException: Range [9, 10) out of bounds for length 9

        Class<?> targetClass
        if (target.isPrimitive()) {
            if (target == Boolean.TYPE) {
                targetClass = Boolean.class;
            } else if (target == Character.TYPE) {
                targetClass = Character.class;
            } else if (target == Byte.TYPE) {
                targetClass = Byte.class;
            } else if (target == Short.TYPE) {
                targetClass = Short.class;
            } else if (target == Integer.TYPE) {
                targetClass = Integer.class;
            } else if (target == Long.TYPE) {
                targetClass = Long.class;
            } else if (target == Float.TYPE) {
                targetClass = Float.class;
            } else {
                targetClass = Double.class;
            }
        } else {
            targetClass = target;
        }
        return targetClass.isAssignableFrom(src);
    }


    /*
     * This class duplicates code in jakarta.el.Util. When making changes keep
     * the code in sync.
     */

    private static boolean isCoercibleFrom(EvaluationContext ctx, Object src, Class<?> target) {
        // TODO: This isn't pretty but it works. Significant refactoring would
        //       be required to avoid the exception.
        try {
            ELSupport.coerceToType(ctx, src, target);
        } catch (ELException e) {
            return false;
        }
        return true;
    }


    /*
     * This class duplicates code in jakarta.el.Util. When making changes keep
     * the code in sync.
     */

    private static Method getMethod(Class<?> type, Object base, Method m) {
        if (m == null ||
                (Modifier.isPublic(type.getModifiers()) &&
                        (Modifier.isStatic(m.getModifiers()) && m.canAccess(null) || m.canAccess(base)))) {
            return m;
        }
        Class<?>[] interfaces = type.getInterfaces();
        Method mp = null;
        for (Class<?> iface : interfaces) {
            try {
                mp = iface.getMethod(m.getName(), m.getParameterTypes());
                mp = getMethod(mp.getDeclaringClass(), base, mp);
                if (mp != null) {
                    return mp;
                }
            } catch (NoSuchMethodException e) {
                // Ignore
            }
        }
        Class<?> sup = type.getSuperclass();
        if (sup != null) {
            try {
                mp = sup.getMethod(m.getName(), m.getParameterTypes());
                mp = getMethod(mp.getDeclaringClass(), base, mp);
                if (mp != null) {
                    return mp;
                }
            } catch (NoSuchMethodException e) {
                // Ignore
            }
        }
        return null;
    }


    private static String paramString(Class<?>[] types) {
        if (types != null) {
            StringBuilder sb = new StringBuilder();
            for (Class<?> type : types) {
                if (type == null) {
                    sb.append("null, ");
                } else {
                    sb.append(type.getName()).append(", ");
                }
            }
            if (sb.length() > 2) {
                sb.setLength(sb.length() - 2);
            }
            return sb.toString();
        }
        return null;
    }


    private static ClassLoader getContextClassLoader() {
        ClassLoader tccl;
        if (System.getSecurityManager() != null) {
            PrivilegedAction<ClassLoader> pa = new PrivilegedGetTccl();
            tccl = AccessController.doPrivileged(pa);
        } else {
            tccl = Thread.currentThread().getContextClassLoader();
        }

        return tccl;
    }


    private static class PrivilegedGetTccl implements PrivilegedAction<ClassLoader> {
        @Override
        public ClassLoader run() {
            return Thread.currentThread().getContextClassLoader();
        }
    }


    /*
     * This class duplicates code in jakarta.el.Util. When making changes keep
     * the code in sync.
     */

    private static class MatchResult implements Comparable<MatchResult> {

        private final boolean varArgs;
        private final int exactCount;
        private final int assignableCount;
        private final int coercibleCount;
        private final int varArgsCount;
        private final boolean bridge;

        MatchResult(boolean varArgs, int exactCount, int assignableCount, int coercibleCount, int varArgsCount,
                boolean bridge) {
            this.varArgs = varArgs;
            this.exactCount = exactCount;
            this.assignableCount = assignableCount;
            this.coercibleCount = coercibleCount;
            this.varArgsCount = varArgsCount;
            this.bridge = bridge;
        }

        public boolean isVarArgs() {
            return varArgs;
        }

        public int getExactCount() {
            return exactCount;
        }

        public int getAssignableCount() {
            return assignableCount;
        }

        public int getCoercible() {
            return coercibleCount;
        }

        public int getVarArgsCount() {
            return varArgsCount;
        }

        public boolean isBridge() {
            return bridge;
        }

        @Override
        public int compareTo(MatchResult o) {
            // Non-varArgs always beats varArgs
            int cmp = Boolean.compare(o.isVarArgs(), this.isVarArgs());
            if (cmp == 0) {
                cmp = Integer.compare(this.getExactCount(), o.getExactCount());
                if (cmp == 0) {
                    cmp = Integer.compare(this.getAssignableCount(), o.getAssignableCount());
                    if (cmp == 0) {
                        cmp = Integer.compare(this.getCoercible(), o.getCoercible());
                        if (cmp == 0) {
                            // Fewer var args matches are better
                            cmp = Integer.compare(o.getVarArgsCount(), this.getVarArgsCount());
                            if (cmp == 0) {
                                // The nature of bridge methods is such that it actually
                                // doesn't matter which one we pick as long as we pick
                                // one. That said, pick the 'right' one (the non-bridge
                                // one) anyway.
                                cmp = Boolean.compare(o.isBridge(), this.isBridge());
                            }
                        }
                    }
                }
            }
            return cmp;
        }

        @Override
        public boolean equals(Object o) {
            return o == this || (null != o &&
                    this.getClass().equals(o.getClass()) &&
                    ((MatchResult)o).getExactCount() == this.getExactCount() &&
                    ((MatchResult)o).getAssignableCount() == this.getAssignableCount() &&
                    ((MatchResult)o).getCoercible() == this.getCoercible() &&
                    ((MatchResult)o).getVarArgsCount() == this.getVarArgsCount() &&
                    ((MatchResult)o).isVarArgs() == this.isVarArgs() &&
                    ((MatchResult)o).isBridge() == this.isBridge());
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + assignableCount;
            result = prime * result + (bridge ? 1231 : 1237);
            result = prime * result + coercibleCount;
            result = prime * result + exactCount;
            result = prime * result + (varArgs ? 1231 : 1237);
            result = prime * result + varArgsCount;
            return result;
        }
    }
}

Messung V0.5
C=93 H=88 G=90

¤ Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.0.15Bemerkung:  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.