use of com.oracle.truffle.dsl.processor.java.ElementUtils.findAnnotationMirror in project graal by oracle.
the class ExportsParser method initializeExportedMethod.
private void initializeExportedMethod(Map<String, NodeData> parsedNodeCache, ExportsData model, ExportMessageData exportedElement) {
ExecutableElement exportedMethod = (ExecutableElement) exportedElement.getMessageElement();
LibraryMessage message = exportedElement.getResolvedMessage();
ExportsLibrary exportsLibrary = exportedElement.getExportsLibrary();
if (ElementUtils.getVisibility(exportedMethod.getModifiers()) == Modifier.PRIVATE) {
exportedElement.addError("The exported method must not be private. " + "Increase visibility to resolve this.");
return;
}
List<TypeMirror> cachedAnnotations = NodeParser.getCachedAnnotations();
List<VariableElement> cachedNodes = new ArrayList<>();
List<VariableElement> cachedLibraries = new ArrayList<>();
int realParameterCount = 0;
parameters: for (VariableElement exportParameter : exportedMethod.getParameters()) {
AnnotationMirror cachedMirror = null;
for (TypeMirror cachedAnnotation : cachedAnnotations) {
AnnotationMirror found = ElementUtils.findAnnotationMirror(exportParameter.getAnnotationMirrors(), cachedAnnotation);
if (found == null) {
continue;
}
if (cachedMirror == null) {
cachedMirror = found;
} else {
StringBuilder b = new StringBuilder();
String sep = "";
for (TypeMirror stringCachedAnnotation : cachedAnnotations) {
b.append(sep);
b.append("@");
b.append(ElementUtils.getSimpleName(stringCachedAnnotation));
sep = ", ";
}
exportedElement.addError(exportParameter, "The following annotations are mutually exclusive for a parameter: %s.", b.toString());
continue parameters;
}
}
AnnotationMirror cachedLibraryMirror = findAnnotationMirror(exportParameter.getAnnotationMirrors(), types.CachedLibrary);
if (cachedLibraryMirror != null) {
cachedLibraries.add(exportParameter);
} else if (cachedMirror != null) {
cachedNodes.add(exportParameter);
} else {
realParameterCount++;
}
}
verifyMethodSignature(model.getTemplateType(), message, exportedElement, exportedMethod, exportsLibrary.getReceiverType(), realParameterCount, true);
boolean aotExcluded = ElementUtils.findAnnotationMirror(exportedMethod, types.GenerateAOT_Exclude) != null;
if (aotExcluded && message.getName().equals("accepts")) {
exportedElement.addError("Cannot use with @%s.%s with the accepts message. The accepts message must always be usable for AOT.", getSimpleName(types.GenerateAOT), getSimpleName(types.GenerateAOT_Exclude));
}
if (exportedElement.hasErrors()) {
return;
}
if (!cachedNodes.isEmpty() || !cachedLibraries.isEmpty()) {
String nodeName = firstLetterUpperCase(exportedMethod.getSimpleName().toString()) + "Node_";
CodeTypeElement type = GeneratorUtils.createClass(model, null, modifiers(PUBLIC, STATIC), nodeName, types.Node);
AnnotationMirror importStatic = findAnnotationMirror(model.getMessageElement(), types.ImportStatic);
if (importStatic != null) {
type.getAnnotationMirrors().add(importStatic);
}
type.getAnnotationMirrors().add(exportedElement.getMessageAnnotation());
CodeExecutableElement element = CodeExecutableElement.clone(exportedMethod);
element.getParameters().clear();
element.getParameters().addAll(exportedMethod.getParameters());
DeclaredType specializationType = types.Specialization;
CodeAnnotationMirror specialization = new CodeAnnotationMirror(specializationType);
specialization.setElementValue(ElementUtils.findExecutableElement(specializationType, "limit"), ElementUtils.getAnnotationValue(exportedElement.getMessageAnnotation(), "limit", false));
element.getAnnotationMirrors().clear();
element.addAnnotationMirror(specialization);
if (aotExcluded) {
element.getAnnotationMirrors().add(new CodeAnnotationMirror(types.GenerateAOT_Exclude));
}
boolean isStatic = element.getModifiers().contains(Modifier.STATIC);
if (!isStatic) {
element.getParameters().add(0, new CodeVariableElement(exportedElement.getReceiverType(), "this"));
element.getModifiers().add(Modifier.STATIC);
}
type.add(element);
NodeData parsedNodeData = parseNode(parsedNodeCache, type, exportedElement, Collections.emptyList());
if (parsedNodeData == null) {
exportedElement.addError("Error could not parse synthetic node: %s", element);
}
element.setEnclosingElement(exportedMethod.getEnclosingElement());
exportedElement.setSpecializedNode(parsedNodeData);
}
if (exportsLibrary.isExplicitReceiver() && !exportedMethod.getModifiers().contains(STATIC)) {
exportedElement.addError("Exported method must be static. @%s annotated types with explcit receiverClass must only contain static methods.", types.ExportLibrary.asElement().getSimpleName().toString());
}
}
use of com.oracle.truffle.dsl.processor.java.ElementUtils.findAnnotationMirror in project graal by oracle.
the class NodeParser method parseFields.
private List<NodeFieldData> parseFields(List<TypeElement> typeHierarchy, List<? extends Element> elements) {
Set<String> names = new HashSet<>();
List<NodeFieldData> fields = new ArrayList<>();
for (VariableElement field : ElementFilter.fieldsIn(elements)) {
if (field.getModifiers().contains(Modifier.STATIC)) {
continue;
} else if (ElementUtils.findAnnotationMirror(field, types.Executed) != null) {
continue;
}
if (field.getModifiers().contains(Modifier.PUBLIC) || field.getModifiers().contains(Modifier.PROTECTED)) {
String name = field.getSimpleName().toString();
fields.add(new NodeFieldData(field, null, field, false));
names.add(name);
}
}
List<TypeElement> reversedTypeHierarchy = new ArrayList<>(typeHierarchy);
Collections.reverse(reversedTypeHierarchy);
for (TypeElement typeElement : reversedTypeHierarchy) {
AnnotationMirror nodeChildrenMirror = findAnnotationMirror(typeElement, types.NodeFields);
List<AnnotationMirror> children = collectAnnotations(nodeChildrenMirror, "value", typeElement, types.NodeField);
for (AnnotationMirror mirror : children) {
String name = firstLetterLowerCase(getAnnotationValue(String.class, mirror, "name"));
TypeMirror type = getAnnotationValue(TypeMirror.class, mirror, "type");
if (type != null) {
NodeFieldData field = new NodeFieldData(typeElement, mirror, new CodeVariableElement(type, name), true);
if (name.isEmpty()) {
field.addError(getAnnotationValue(mirror, "name"), "Field name cannot be empty.");
} else if (names.contains(name)) {
field.addError(getAnnotationValue(mirror, "name"), "Duplicate field name '%s'.", name);
}
names.add(name);
fields.add(field);
} else {
// Type is null here. This indicates that the type could not be resolved.
// The Java compiler will subsequently raise the appropriate error.
}
}
}
for (NodeFieldData nodeFieldData : fields) {
nodeFieldData.setGetter(findGetter(elements, nodeFieldData.getName(), nodeFieldData.getType()));
nodeFieldData.setSetter(findSetter(elements, nodeFieldData.getName(), nodeFieldData.getType()));
}
return fields;
}
Aggregations