![JAR search and dependency download from the Maven repository](/logo.png)
br.com.objectos.code.ProcessingEnvironmentWrapperJdt Maven / Gradle / Ivy
/*
* 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.code;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;
import javax.tools.JavaFileManager.Location;
/**
* @author [email protected] (Marcio Endo)
*/
public class ProcessingEnvironmentWrapperJdt extends ProcessingEnvironmentWrapper {
private static final Set ENUM_CONSTANT_MODIFIER_SET = EnumSet.of(
Modifier.PUBLIC,
Modifier.STATIC,
Modifier.FINAL);
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 String filePath(Location location, TypeElement element) {
StringBuilder optionName = new StringBuilder();
String[] parts = location.getName().toLowerCase().split("_");
optionName.append(parts[0]);
if (parts.length > 1) {
for (int i = 1; i < parts.length; i++) {
String part = parts[i];
optionName.append(Code.upperCaseFirstChar(part));
}
}
String sourcePath = unwrap().getOptions().get(optionName.toString());
String qualifiedName = element.asType().toString();
String fileName = qualifiedName.replace(".", FILE_SEPARATOR);
return sourcePath + FILE_SEPARATOR + fileName + ".java";
}
@Override
public List extends Element> getEnclosedElements(TypeElement element) {
JdtWorkaround workaround = workaroundOf(element);
return workaround.getEnclosedElements(element);
}
@Override
public Stream streamEnclosedEnumConstantElements(Element enumElement) {
// just a workaround... will not work 100% of times...
TypeMirror enumType = enumElement.asType();
Types types = unwrap().getTypeUtils();
return super.streamEnclosedEnumConstantElements(enumElement)
.filter(el -> el.getModifiers().equals(ENUM_CONSTANT_MODIFIER_SET))
.filter(el -> types.isSameType(el.asType(), enumType));
}
@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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy