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

br.com.objectos.way.code.ProcessingEnvironmentWrapperJdt Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.way.code;

import java.util.List;
import java.util.Optional;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;

/**
 * @author [email protected] (Marcio Endo)
 */
public class ProcessingEnvironmentWrapperJdt extends ProcessingEnvironmentWrapper {

  private final Class binaryTypeBinding;
  private final Class sourceTypeBinding;

  public ProcessingEnvironmentWrapperJdt(ProcessingEnvironment environment) {
    super(environment);

    try {
      binaryTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding");
      sourceTypeBinding = Class.forName("org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding");
    } catch (ClassNotFoundException e) {
      throw new JdtWorkaroundException(e);
    }
  }

  @Override
  public List getEnclosedElements(TypeElement element) {
    JdtWorkaround workaround = workaroundOf(element);
    return workaround.getEnclosedElements(element);
  }

  @Override
  void addSimpleName(List nameList, Element element) {
    Name simpleName = element.getSimpleName();
    String nameString = simpleName.toString();
    String[] parts = nameString.split(Character.toString('$'));
    for (String part : parts) {
      nameList.add(part);
    }
  }

  private JdtWorkaround workaroundOf(TypeElement element) {
    Optional maybeBinding = Jdt.fieldValue(element, "_binding");
    if (!maybeBinding.isPresent()) {
      return JdtWorkaroundDefault.INSTANCE;
    }

    Object binding = maybeBinding.get();
    Class bindingClass = binding.getClass();
    if (binaryTypeBinding.isAssignableFrom(bindingClass)) {
      return new JdtWorkaroundBinding(binding);
    }

    if (sourceTypeBinding.isAssignableFrom(bindingClass)) {
      return new JdtWorkaroundBinding(binding);
    }

    return JdtWorkaroundDefault.INSTANCE;
  }

}