|
Java example source code file (AnnotatedTypeFactory.java)
The AnnotatedTypeFactory.java Java example source code/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.reflect.annotation; import java.lang.annotation.*; import java.lang.reflect.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import static sun.reflect.annotation.TypeAnnotation.*; public final class AnnotatedTypeFactory { /** * Create an AnnotatedType. * * @param type the type this AnnotatedType corresponds to * @param currentLoc the location this AnnotatedType corresponds to * @param actualTypeAnnos the type annotations this AnnotatedType has * @param allOnSameTarget all type annotation on the same TypeAnnotationTarget * as the AnnotatedType being built * @param decl the declaration having the type use this AnnotatedType * corresponds to */ public static AnnotatedType buildAnnotatedType(Type type, LocationInfo currentLoc, TypeAnnotation[] actualTypeAnnos, TypeAnnotation[] allOnSameTarget, AnnotatedElement decl) { if (type == null) { return EMPTY_ANNOTATED_TYPE; } if (isArray(type)) return new AnnotatedArrayTypeImpl(type, currentLoc, actualTypeAnnos, allOnSameTarget, decl); if (type instanceof Class) { return new AnnotatedTypeBaseImpl(type, addNesting(type, currentLoc), actualTypeAnnos, allOnSameTarget, decl); } else if (type instanceof TypeVariable) { return new AnnotatedTypeVariableImpl((TypeVariable)type, currentLoc, actualTypeAnnos, allOnSameTarget, decl); } else if (type instanceof ParameterizedType) { return new AnnotatedParameterizedTypeImpl((ParameterizedType)type, addNesting(type, currentLoc), actualTypeAnnos, allOnSameTarget, decl); } else if (type instanceof WildcardType) { return new AnnotatedWildcardTypeImpl((WildcardType) type, currentLoc, actualTypeAnnos, allOnSameTarget, decl); } throw new AssertionError("Unknown instance of Type: " + type + "\nThis should not happen."); } private static LocationInfo addNesting(Type type, LocationInfo addTo) { if (isArray(type)) return addTo; if (type instanceof Class) { Class<?> clz = (Class)type; if (clz.getEnclosingClass() == null) return addTo; if (Modifier.isStatic(clz.getModifiers())) return addNesting(clz.getEnclosingClass(), addTo); return addNesting(clz.getEnclosingClass(), addTo.pushInner()); } else if (type instanceof ParameterizedType) { ParameterizedType t = (ParameterizedType)type; if (t.getOwnerType() == null) return addTo; return addNesting(t.getOwnerType(), addTo.pushInner()); } return addTo; } private static boolean isArray(Type t) { if (t instanceof Class) { Class<?> c = (Class)t; if (c.isArray()) return true; } else if (t instanceof GenericArrayType) { return true; } return false; } static final AnnotatedType EMPTY_ANNOTATED_TYPE = new AnnotatedTypeBaseImpl(null, LocationInfo.BASE_LOCATION, new TypeAnnotation[0], new TypeAnnotation[0], null); static final AnnotatedType[] EMPTY_ANNOTATED_TYPE_ARRAY = new AnnotatedType[0]; private static class AnnotatedTypeBaseImpl implements AnnotatedType { private final Type type; private final AnnotatedElement decl; private final LocationInfo location; private final TypeAnnotation[] allOnSameTargetTypeAnnotations; private final Map<Class annotations; AnnotatedTypeBaseImpl(Type type, LocationInfo location, TypeAnnotation[] actualTypeAnnotations, TypeAnnotation[] allOnSameTargetTypeAnnotations, AnnotatedElement decl) { this.type = type; this.decl = decl; this.location = location; this.allOnSameTargetTypeAnnotations = allOnSameTargetTypeAnnotations; this.annotations = TypeAnnotationParser.mapTypeAnnotations(location.filter(actualTypeAnnotations)); } // AnnotatedElement @Override public final Annotation[] getAnnotations() { return getDeclaredAnnotations(); } @Override public final <T extends Annotation> T getAnnotation(Class Other Java examples (source code examples)Here is a short list of links related to this Java AnnotatedTypeFactory.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.