Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2010, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * 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.jboss.weld.resources;
 
 import static org.jboss.weld.util.cache.LoadingCacheUtils.getCastCacheValue;
 import static org.jboss.weld.util.reflection.Reflections.cast;
 
 
 
 
 
 public class MemberTransformer implements BootstrapService {
 
     private static class MemberKey<X, A extends AnnotatedMember<X>> {
         private final EnhancedAnnotatedType<X> type;
         private final A member;
         private final int hashCode;
 
         public MemberKey(EnhancedAnnotatedType<X> type, A member) {
             this. = type;
             this. = member;
             this. = Objects.hashCode(typemember);
         }
 
         @Override
         public int hashCode() {
             return ;
         }
 
         @Override
         public boolean equals(Object obj) {
             if (obj instanceof MemberKey) {
                 MemberKey<?, ?> that = (MemberKey<?, ?>) obj;
                 return Objects.equal(this.that.type) && Objects.equal(this.that.member);
             }
             return false;
         }
     }
 
     private final ClassTransformer transformer;
 
 
     private final LoadingCache<MemberKey<?, ?>, EnhancedAnnotatedMember<?, ?, ?>> enhancedMemberCache;
     private final EnhancedFieldLoader enhancedFieldLoader;
     private final EnhancedMethodLoader enhancedMethodLoader;
 
     public MemberTransformer(ClassTransformer transformer) {
         CacheBuilder<ObjectObjectcacheBuilder = CacheBuilder.newBuilder();
         this. = transformer;
        this. = cacheBuilder.build(new UnbackedMemberById());
        this. = new EnhancedFieldLoader();
        this. = new EnhancedMethodLoader();
        this. = cacheBuilder.build(new EnhancedMemberLoaderFunction());
    }
    // Unbacked members
    public <X> UnbackedAnnotatedMember<X> getUnbackedMember(UnbackedMemberIdentifier<X> identifier) {
        return getCastCacheValue(identifier);
    }

    
If an unbacked member is being deserialized it is looked in all the members of the declaring type and cached.
    private static class UnbackedMemberById extends CacheLoader<UnbackedMemberIdentifier<?>, UnbackedAnnotatedMember<?>> {
        @Override
        public UnbackedAnnotatedMember<?> load(UnbackedMemberIdentifier<?> identifier) {
            return findMatchingMember(identifier.getType(), identifier.getMemberId());
        }
        private <T> UnbackedAnnotatedMember<T> findMatchingMember(UnbackedAnnotatedType<T> typeString id) {
            for (AnnotatedField<? super T> field : type.getFields()) {
                if (id.equals(AnnotatedTypes.createFieldId(field))) {
                    return cast(field);
                }
            }
            for (AnnotatedMethod<? super T> method : type.getMethods()) {
                if (id.equals(AnnotatedTypes.createMethodId(method.getJavaMember(), method.getAnnotations(), method.getParameters()))) {
                    return Reflections.cast(method);
                }
            }
            for (AnnotatedConstructor<T> constructor : type.getConstructors()) {
                if (id.equals(AnnotatedTypes.createConstructorId(constructor.getJavaMember(), constructor.getAnnotations(), constructor.getParameters()))) {
                    return cast(constructor);
                }
            }
            throw ..unableToLoadMember(id);
        }
    }
    // Enhanced members
    public <X, A extends EnhancedAnnotatedMember<?, X, ? extends Member>> A loadEnhancedMember(AnnotatedMember<X> memberString bdaId) {
        if (member instanceof EnhancedAnnotatedMember<?, ?, ?>) {
            return Reflections.cast(member);
        }
        EnhancedAnnotatedType<X> declaringType = .getEnhancedAnnotatedType(member.getDeclaringType(), bdaId);
        return getCastCacheValue(new MemberKey<X, AnnotatedMember<X>>(declaringTypemember));
    }
    public <X> EnhancedAnnotatedParameter<?, X> loadEnhancedParameter(AnnotatedParameter<X> parameterString bdaId) {
        if (parameter instanceof EnhancedAnnotatedParameter<?, ?>) {
            return Reflections.cast(parameter);
        }
        EnhancedAnnotatedCallable<?, X, Membercallable = loadEnhancedMember(parameter.getDeclaringCallable(), bdaId);
        return callable.getEnhancedParameters().get(parameter.getPosition());
    }
    private class EnhancedMemberLoaderFunction extends CacheLoader<MemberKey<?, ?>, EnhancedAnnotatedMember<?, ?, ?>> {
        @Override
        public EnhancedAnnotatedMember<?, ?, ?> load(MemberKey<?, ?> from) {
            if (from.member instanceof AnnotatedField<?>) {
                return .load(Reflections.<MemberKey<?, AnnotatedField<?>>> cast(from));
            }
            if (from.member instanceof AnnotatedMethod<?>) {
                return .load(Reflections.<MemberKey<?, AnnotatedMethod<?>>> cast(from));
            }
            if (from.member instanceof AnnotatedConstructor<?>) {
                return .load(Reflections.<MemberKey<?, AnnotatedConstructor<?>>> cast(from));
            }
            throw ..invalidAnnotatedMember(from);
        }
    }
    private abstract class AbstractEnhancedMemberLoader<A extends AnnotatedMember<?>, W extends EnhancedAnnotatedMember<?, ?, ?>> {
        public W load(MemberKey<?, A> source) {
            return findMatching(getMembersOfDeclaringType(source), source.member);
        }
        public W findMatching(Collection<W> members, A source) {
            for (W member : members) {
                if (equals(membersource)) {
                    return member;
                }
            }
            throw ..unableToLoadMember(source);
        }
        public abstract boolean equals(W member1, A member2);
        public abstract Collection<W> getMembersOfDeclaringType(MemberKey<?, A> source);
    }
        @Override
        public boolean equals(EnhancedAnnotatedField<?, ?> member1AnnotatedField<?> member2) {
            return AnnotatedTypes.compareAnnotatedField(member1member2);
        }
        @Override
        public Collection<EnhancedAnnotatedField<?, ?>> getMembersOfDeclaringType(MemberKey<?, AnnotatedField<?>> source) {
            return cast(source.type.getFields());
        }
    }
        @Override
        public boolean equals(EnhancedAnnotatedMethod<?, ?> member1AnnotatedMethod<?> member2) {
            return AnnotatedTypes.compareAnnotatedCallable(member1member2);
        }
        @Override
        public Collection<EnhancedAnnotatedMethod<?, ?>> getMembersOfDeclaringType(MemberKey<?, AnnotatedMethod<?>> source) {
            return cast(source.type.getMethods());
        }
    }
        @Override
        public boolean equals(EnhancedAnnotatedConstructor<?> member1AnnotatedConstructor<?> member2) {
            return AnnotatedTypes.compareAnnotatedCallable(member1member2);
        }
        @Override
            return cast(source.type.getConstructors());
        }
    }
    public void cleanupAfterBoot() {
    }
    @Override
    public void cleanup() {
        cleanupAfterBoot();
    }
New to GrepCode? Check out our FAQ X