<commit_before>/*
 * 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.commons.jexl2.util.introspection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.jexl2.util.ArrayIterator;
import org.apache.commons.jexl2.util.EnumerationIterator;
import org.apache.commons.logging.Log;

/**
 * Implementation of Uberspect to provide the default introspective
 * functionality of JEXL.
 * <p>This is the class to derive to customize introspection.</p>
 *
 * @since 1.0
 * @author <a href="mailto:<EMAIL>"><NAME> Jr.</a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public class UberspectImpl extends org.apache.commons.jexl2.util.Introspector implements Uberspect {
    /** {@inheritDoc} */
    public Introspector getIntrospector() {
        return introspector.get();
    }

    /**
     * Creates a new UberspectImpl.
     * @param runtimeLogger the logger used for all logging needs
     */
    public UberspectImpl(Log runtimeLogger) {
        this(runtimeLogger, new Introspector(runtimeLogger));
    }

    /**
     * Creates a new UberspectImpl.
     * @param runtimeLogger the logger used for all logging needs
     * @param intro the introspector to use
     */
    public UberspectImpl(Log runtimeLogger, Introspector intro) {
        super(runtimeLogger, intro);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public Iterator<?> getIterator(Object obj, DebugInfo info) {
        if (obj.getClass().isArray()) {
            return new ArrayIterator(obj);
        } else if (obj instanceof Collection<?>) {
            return ((Collection<?>) obj).iterator();
        } else if (obj instanceof Map<?,?>) {
            return ((Map<?,?>) obj).values().iterator();
        } else if (obj instanceof Iterator<?>) {
            rlog.warn(info.debugString()
                    + "The iterative is not resetable; if used more than once, "
                    + "this may lead to unexpected results.");

            return ((Iterator<?>) obj);
        } else if (obj instanceof Enumeration<?>) {
            rlog.warn(info.debugString()
                    + "The iterative is not resetable; if used more than once, "
                    + "this may lead to unexpected results.");
            return new EnumerationIterator<Object>((Enumeration<Object>) obj);
        } else {
            // look for an iterator() method to support the JDK5 Iterable
            // interface or any user tools/DTOs that want to work in
            // foreach without implementing the Collection interface
            Class<?> type = obj.getClass();
            try {
                Method iter = type.getMethod("iterator", (Class<?>[]) null);
                Class<?> returns = iter.getReturnType();
                if (Iterator.class.isAssignableFrom(returns)) {
                    return (Iterator<?>) iter.invoke(obj, (Object[])null);
                } else {
                    rlog.error(info.debugString()
                            + "iterator() method does not return a true Iterator.");
                }<commit_msg>Document why cast is safe Typo in message: iterative => iterator<commit_after>/*
 * 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.commons.jexl2.util.introspection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.jexl2.util.ArrayIterator;
import org.apache.commons.jexl2.util.EnumerationIterator;
import org.apache.commons.logging.Log;

/**
 * Implementation of Uberspect to provide the default introspective
 * functionality of JEXL.
 * <p>This is the class to derive to customize introspection.</p>
 *
 * @since 1.0
 * @author <a href="mailto:<EMAIL>"><NAME>.</a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public class UberspectImpl extends org.apache.commons.jexl2.util.Introspector implements Uberspect {
    /** {@inheritDoc} */
    public Introspector getIntrospector() {
        return introspector.get();
    }

    /**
     * Creates a new UberspectImpl.
     * @param runtimeLogger the logger used for all logging needs
     */
    public UberspectImpl(Log runtimeLogger) {
        this(runtimeLogger, new Introspector(runtimeLogger));
    }

    /**
     * Creates a new UberspectImpl.
     * @param runtimeLogger the logger used for all logging needs
     * @param intro the introspector to use
     */
    public UberspectImpl(Log runtimeLogger, Introspector intro) {
        super(runtimeLogger, intro);
    }

    /**
     * {@inheritDoc}
     */
    public Iterator<?> getIterator(Object obj, DebugInfo info) {
        if (obj.getClass().isArray()) {
            return new ArrayIterator(obj);
        } else if (obj instanceof Collection<?>) {
            return ((Collection<?>) obj).iterator();
        } else if (obj instanceof Map<?,?>) {
            return ((Map<?,?>) obj).values().iterator();
        } else if (obj instanceof Iterator<?>) {
            rlog.warn(info.debugString()
                    + "The iterator is not resetable; if used more than once, "
                    + "this may lead to unexpected results.");

            return ((Iterator<?>) obj);
        } else if (obj instanceof Enumeration<?>) {
            rlog.warn(info.debugString()
                    + "The iterator is not resetable; if used more than once, "
                    + "this may lead to unexpected results.");
            @SuppressWarnings("unchecked") // OK because of instanceof check above
            final Enumeration<Object> enumObj = (Enumeration<Object>) obj;
            return new EnumerationIterator<Object>(enumObj);
        } else {
            // look for an iterator() method to support the JDK5 Iterable
            // interface or any user tools/DTOs that want to work in
            // foreach without implementing the Collection interface
            Class<?> type = obj.getClass();
            try {
                Method iter = type.getMethod("iterator", (Class<?>[]) null);
                Class<?> returns = iter.getReturnType();
                if (Iterator.class.isAssignableFrom(returns)) {
                    return (Iterator<?>) iter.invoke(obj, (Object[])null);
                } else {
                    rlog.error(info.debugString()
                            + "iterator() method does not return a true Iterator.");
                }
{'fraction_non_alphanumeric': 0.10549957508801748, 'fraction_numerical': 0.002670875318683987, 'mean_word_length': 2.625880281690141, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6891899', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2113, 'n_words': 736}