< prev index next >

src/java.base/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java

Print this page

  1 /*
  2  * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any

659         for (Map.Entry<String, Object> memberValue : streamVals.entrySet()) {
660             String name = memberValue.getKey();
661             Object value = null;
662             Class<?> memberType = memberTypes.get(name);
663             if (memberType != null) {  // i.e. member still exists
664                 value = memberValue.getValue();
665                 if (!(memberType.isInstance(value) ||
666                       value instanceof ExceptionProxy)) {
667                     value = new AnnotationTypeMismatchExceptionProxy(
668                                 Objects.toIdentityString(value))
669                         .setMember(annotationType.members().get(name));
670                 }
671             }
672             mv.put(name, value);
673         }
674 
675         UnsafeAccessor.setType(this, t);
676         UnsafeAccessor.setMemberValues(this, mv);
677     }
678 
679     /**
680      * Gets an unmodifiable view on the member values.
681      */
682     Map<String, Object> memberValues() {
683         return Collections.unmodifiableMap(memberValues);
684     }
685 
686     private static class UnsafeAccessor {
687         private static final jdk.internal.misc.Unsafe unsafe
688                 = jdk.internal.misc.Unsafe.getUnsafe();
689         private static final long typeOffset = unsafe.objectFieldOffset
690                 (AnnotationInvocationHandler.class, "type");
691         private static final long memberValuesOffset = unsafe.objectFieldOffset
692                 (AnnotationInvocationHandler.class, "memberValues");
693 
694         static void setType(AnnotationInvocationHandler o,
695                             Class<? extends Annotation> type) {
696             unsafe.putReference(o, typeOffset, type);
697         }
698 
699         static void setMemberValues(AnnotationInvocationHandler o,
700                                     Map<String, Object> memberValues) {
701             unsafe.putReference(o, memberValuesOffset, memberValues);
702         }
703     }
704 }

  1 /*
  2  * Copyright (c) 2003, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any

659         for (Map.Entry<String, Object> memberValue : streamVals.entrySet()) {
660             String name = memberValue.getKey();
661             Object value = null;
662             Class<?> memberType = memberTypes.get(name);
663             if (memberType != null) {  // i.e. member still exists
664                 value = memberValue.getValue();
665                 if (!(memberType.isInstance(value) ||
666                       value instanceof ExceptionProxy)) {
667                     value = new AnnotationTypeMismatchExceptionProxy(
668                                 Objects.toIdentityString(value))
669                         .setMember(annotationType.members().get(name));
670                 }
671             }
672             mv.put(name, value);
673         }
674 
675         UnsafeAccessor.setType(this, t);
676         UnsafeAccessor.setMemberValues(this, mv);
677     }
678 







679     private static class UnsafeAccessor {
680         private static final jdk.internal.misc.Unsafe unsafe
681                 = jdk.internal.misc.Unsafe.getUnsafe();
682         private static final long typeOffset = unsafe.objectFieldOffset
683                 (AnnotationInvocationHandler.class, "type");
684         private static final long memberValuesOffset = unsafe.objectFieldOffset
685                 (AnnotationInvocationHandler.class, "memberValues");
686 
687         static void setType(AnnotationInvocationHandler o,
688                             Class<? extends Annotation> type) {
689             unsafe.putReference(o, typeOffset, type);
690         }
691 
692         static void setMemberValues(AnnotationInvocationHandler o,
693                                     Map<String, Object> memberValues) {
694             unsafe.putReference(o, memberValuesOffset, memberValues);
695         }
696     }
697 }
< prev index next >