All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.immutables.processor.ProxyProcessor Maven / Gradle / Ivy

Go to download

Compile time annotations and compile time annotation processor to generate consistent value object using either abstract class, interface or annotation as a base.

There is a newer version: 2.10.1
Show newest version
/*
   Copyright 2016 Immutables Authors and 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.immutables.processor;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.Set;
import javax.annotation.processing.Completion;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import org.immutables.metainf.Metainf;

/**
 * Proxy processor to overcome eclipse class loading problems which renders our workarounds
 * for Eclipse bugs and quirks useless. When the condition is not detected we try to use direct
 * delegation to {@code org.immutables.value.processor.Processor}.
 */
@Metainf.Service
public final class ProxyProcessor implements Processor {
  private static final String DELEGATE_CLASS = "org.immutables.value.processor.Processor";
  private static final String ECLIPSE_PACKAGE_PREFIX = "org.eclipse.";
  private static final String OSGI_SYSTEM_PROPERTY = "osgi.arch";

  // initialization not quite atomic but that's ok
  private static volatile ClassLoader cachedProxyClassLoader;

  private final Processor delegate = requiresClassLoaderDelegate()
      ? createClassLoaderDelegate()
      : createDefaultDelegate();

  @Override
  public Set getSupportedOptions() {
    return delegate.getSupportedOptions();
  }

  @Override
  public Set getSupportedAnnotationTypes() {
    return delegate.getSupportedAnnotationTypes();
  }

  @Override
  public SourceVersion getSupportedSourceVersion() {
    return delegate.getSupportedSourceVersion();
  }

  @Override
  public void init(ProcessingEnvironment processing) {
    delegate.init(processing);
  }

  @Override
  public boolean process(Set annotations, RoundEnvironment round) {
    return delegate.process(annotations, round);
  }

  @Override
  public Iterable getCompletions(
      Element element,
      AnnotationMirror annotation,
      ExecutableElement member,
      String userText) {
    return delegate.getCompletions(element, annotation, member, userText);
  }

  private static Processor createDefaultDelegate() {
    return new org.immutables.value.processor.Processor();
  }

  /**
   * We find the delegating classloader by traversing the call stack and looking for the first
   * class that has a classloader from the Eclipse namespace. This is bound to be the bundle classloader
   * of a JDT bundle which can load the required Eclipse compiler types.
   * 
   * NOTE: We cannot simply use the thread context class loader. This works in the Eclipse IDE
   * (where it is set to ContextFinder), but not in the JDT Language Server.
   */
  private static Processor createClassLoaderDelegate() {
    try {
      if (cachedProxyClassLoader == null) {
        CallStack callStack = new CallStack();
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        for (Class klass : callStack.getCallingClasses()) {
          if (klass.getClassLoader() != null && klass.getClassLoader().getClass().getName().startsWith(ECLIPSE_PACKAGE_PREFIX)) {
            loader = klass.getClassLoader();
            break;
          }
        }
        cachedProxyClassLoader = new ProxyClassLoader(
            ((URLClassLoader) ProxyProcessor.class.getClassLoader()).getURLs(),
            loader);
      }
      return (Processor) cachedProxyClassLoader.loadClass(DELEGATE_CLASS).newInstance();
    } catch (RuntimeException | Error ex) {
      throw ex;
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }

  private static boolean requiresClassLoaderDelegate() {
    if (System.getProperty(OSGI_SYSTEM_PROPERTY) != null) {
      CallStack callStack = new CallStack();
      for (Class klass : callStack.getCallingClasses()) {
        ClassLoader classLoader = klass.getClassLoader();
        if (classLoader != null && klass.getClassLoader().getClass().getName().startsWith(ECLIPSE_PACKAGE_PREFIX)) {
          return true;
        }
      }
    }
    return false;
  }

  private static class ProxyClassLoader extends URLClassLoader {
    private final ClassLoader contextLoader;

    ProxyClassLoader(URL[] urls, ClassLoader contextLoader) {
      super(urls, contextLoader);
      this.contextLoader = contextLoader;
    }

    @Override
    public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
      synchronized (getClassLoadingLock(name)) {
        Class c;
        if (name.startsWith(ECLIPSE_PACKAGE_PREFIX)) {
          c = contextLoader.loadClass(name);
        } else {
          c = findLoadedClass(name);
          if (c == null) {
            try {
              c = findClass(name);
            } catch (ClassNotFoundException ex) {
              return super.loadClass(name, resolve);
            }
          }
        }
        if (resolve) {
          resolveClass(c);
        }
        return c;
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy