All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.teavm.model.ClassReaderSource Maven / Gradle / Ivy

There is a newer version: 0.2.8
Show newest version
/*
 *  Copyright 2014 Alexey Andreev.
 *
 *  Licensed 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.teavm.model;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public interface ClassReaderSource {
    ClassReader get(String name);

    default Stream getAncestorClasses(String name) {
        return StreamSupport.stream(((Iterable) () -> new Iterator() {
            ClassReader currentClass = get(name);
            @Override public ClassReader next() {
                ClassReader result = currentClass;
                if (currentClass.getParent() != null) {
                    currentClass = get(currentClass.getParent());
                } else {
                    currentClass = null;
                }
                return result;
            }
            @Override public boolean hasNext() {
                return currentClass != null;
            }
        }).spliterator(), false);
    }

    default Stream getAncestors(String name) {
        return StreamSupport.stream(((Iterable) () -> new Iterator() {
            Deque> state = new ArrayDeque<>();
            private Set visited = new HashSet<>();
            {
                state.push(new ArrayDeque<>());
                add(name);
            }
            @Override public ClassReader next() {
                while (!state.isEmpty()) {
                    Deque level = state.peek();
                    if (!level.isEmpty()) {
                        ClassReader result = level.removeFirst();
                        follow(result);
                        return result;
                    }
                    state.pop();
                }
                return null;
            }
            @Override public boolean hasNext() {
                return !this.state.stream().allMatch(e -> e.isEmpty());
            }
            private void follow(ClassReader cls) {
                state.push(new ArrayDeque<>());
                if (cls.getParent() != null) {
                    add(cls.getParent());
                }
                for (String iface : cls.getInterfaces()) {
                    add(iface);
                }
            }
            private void add(String name) {
                ClassReader cls = get(name);
                if (cls != null && visited.add(cls)) {
                    state.peek().addLast(cls);
                }
            }
        }).spliterator(), false);
    }

    default MethodReader resolve(MethodReference method) {
        return getAncestors(method.getClassName())
                .map(cls -> cls.getMethod(method.getDescriptor()))
                .filter(Objects::nonNull)
                .findFirst().orElse(null);
    }

    default MethodReader resolveImplementation(MethodReference methodReference) {
        return ClassReaderSourceHelper.resolveMethodImplementation(this, methodReference.getClassName(),
                methodReference.getDescriptor(), new HashSet<>());
    }

    default MethodReader resolveImplementation(String className, MethodDescriptor descriptor) {
        return ClassReaderSourceHelper.resolveMethodImplementation(this, className, descriptor, new HashSet<>());
    }

    default FieldReader resolve(FieldReference field) {
        return getAncestors(field.getClassName())
                .map(cls -> cls.getField(field.getFieldName()))
                .filter(Objects::nonNull)
                .findFirst().orElse(null);
    }

    default Stream overriddenMethods(MethodReference method) {
        return getAncestorClasses(method.getClassName())
                .map(cls -> cls.getMethod(method.getDescriptor()))
                .filter(Objects::nonNull);
    }

    default Optional isSuperType(String superType, String subType) {
        return ClassReaderSourceHelper.isSuperType(this, superType, subType);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy