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.10.2
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.Optional;
import java.util.Set;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 *
 * @author Alexey Andreev
 */
public interface ClassReaderSource {
    ClassReader get(String name);

    default Stream getAncestorClasses(String name) {
        return StreamSupport.stream(((Iterable) () -> {
            return new Iterator() {
                ClassReader currentClass = get(name);
                @Override public ClassReader next() {
                    ClassReader result = currentClass;
                    if (currentClass.getParent() != null && !currentClass.getName().equals(currentClass.getParent())) {
                        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) () -> {
            return 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(candidate -> candidate != null)
                .findFirst().orElse(null);
    }

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

    default Stream overridenMethods(MethodReference method) {
        return getAncestorClasses(method.getClassName())
                .map(cls -> cls.getMethod(method.getDescriptor()))
                .filter(candidate -> candidate != null);
    }

    default Optional isSuperType(String superType, String subType) {
        if (superType.equals(subType)) {
            return Optional.of(true);
        }
        ClassReader cls = get(subType);
        if (cls == null) {
            return Optional.empty();
        }
        if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) {
            if (isSuperType(superType, cls.getParent()).orElse(false)) {
                return Optional.of(true);
            }
        }
        for (String iface : cls.getInterfaces()) {
            if (isSuperType(superType, iface).orElse(false)) {
                return Optional.of(true);
            }
        }
        return Optional.of(false);
    }

    default Optional isSuperType(ValueType superType, ValueType subType) {
        if (superType.equals(subType)) {
            return Optional.of(true);
        }
        if (superType instanceof ValueType.Primitive || subType instanceof ValueType.Primitive) {
            return Optional.of(false);
        }
        if (superType.isObject("java.lang.Object")) {
            return Optional.of(true);
        }
        if (superType instanceof ValueType.Object && subType instanceof ValueType.Object) {
            return isSuperType(((ValueType.Object) superType).getClassName(),
                    ((ValueType.Object) subType).getClassName());
        } else if (superType instanceof ValueType.Array & subType instanceof ValueType.Array) {
            return isSuperType(((ValueType.Array) superType).getItemType(), ((ValueType.Array) subType).getItemType());
        } else {
            return Optional.of(false);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy