Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 com.facebook.presto.byteCode;
 
 
 import java.util.Map;
 
 public class DynamicClassLoader
         extends ClassLoader
 {
     private final ConcurrentMap<Stringbyte[]> pendingClasses = new ConcurrentHashMap<>();
     private final Map<LongMethodHandlecallsiteBindings;
 
     public DynamicClassLoader()
     {
         this(null);
     }
 
     public DynamicClassLoader(ClassLoader parentClassLoader)
     {
         this(parentClassLoader, ImmutableMap.<LongMethodHandle>of());
     }
 
     public DynamicClassLoader(ClassLoader parentClassLoaderMap<LongMethodHandlecallsiteBindings)
     {
         super(resolveClassLoader(parentClassLoader));
         this. = ImmutableMap.copyOf(callsiteBindings);
     }
 
     public Class<?> defineClass(String classNamebyte[] byteCode)
     {
         return super.defineClass(classNamebyteCode, 0, byteCode.length);
     }
 
     public Map<StringClass<?>> defineClasses(Map<Stringbyte[]> newClasses)
     {
         SetView<Stringconflicts = Sets.intersection(.keySet(), newClasses.keySet());
         Preconditions.checkArgument(conflicts.isEmpty(), "The classes %s have already been defined"conflicts);
 
         .putAll(newClasses);
         try {
             Map<StringClass<?>> classes = new HashMap<>();
             for (String className : newClasses.keySet()) {
                 try {
                     Class<?> clazz = loadClass(className);
                     classes.put(classNameclazz);
                 }
                 catch (ClassNotFoundException e) {
                     // this should never happen
                     throw Throwables.propagate(e);
                 }
             }
             return classes;
         }
         finally {
             .keySet().removeAll(newClasses.keySet());
         }
     }
 
     public Map<LongMethodHandlegetCallsiteBindings()
     {
         return ;
     }
 
     @Override
     protected Class<?> findClass(String name)
             throws ClassNotFoundException
     {
         byte[] byteCode = .get(name);
         if (byteCode == null) {
             throw new ClassNotFoundException(name);
         }
 
         return defineClass(namebyteCode);
     }
 
     @Override
     protected Class<?> loadClass(String nameboolean resolve)
             throws ClassNotFoundException
    {
        // grab the magic lock
        synchronized (getClassLoadingLock(name)) {
            // Check if class is in the loaded classes cache
            Class<?> cachedClass = findLoadedClass(name);
            if (cachedClass != null) {
                return resolveClass(cachedClassresolve);
            }
            try {
                Class<?> clazz = findClass(name);
                return resolveClass(clazzresolve);
            }
            catch (ClassNotFoundException ignored) {
                // not a local class
            }
            Class<?> clazz = getParent().loadClass(name);
            return resolveClass(clazzresolve);
        }
    }
    private Class<?> resolveClass(Class<?> clazzboolean resolve)
    {
        if (resolve) {
            resolveClass(clazz);
        }
        return clazz;
    }
    private static ClassLoader resolveClassLoader(ClassLoader parentClassLoader)
    {
        if (parentClassLoader == null) {
            parentClassLoader = Thread.currentThread().getContextClassLoader();
        }
        if (parentClassLoader == null) {
            parentClassLoader = DynamicClassLoader.class.getClassLoader();
        }
        if (parentClassLoader == null) {
            parentClassLoader = ClassLoader.getSystemClassLoader();
        }
        return parentClassLoader;
    }
New to GrepCode? Check out our FAQ X