Search in sources :

Example 31 with CodeExecutableElement

use of com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement in project graal by oracle.

the class InstrumentableProcessor method generateFactory.

@SuppressWarnings("deprecation")
private static CodeTypeElement generateFactory(ProcessorContext context, Element e, CodeTypeElement wrapper) {
    TypeElement sourceType = (TypeElement) e;
    PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(sourceType);
    Set<Modifier> typeModifiers = ElementUtils.modifiers(Modifier.PUBLIC, Modifier.FINAL);
    CodeTypeElement factory = new CodeTypeElement(typeModifiers, ElementKind.CLASS, pack, createWrapperClassName(sourceType));
    TypeMirror factoryType = context.reloadType(context.getType(com.oracle.truffle.api.instrumentation.InstrumentableFactory.class));
    factory.getImplements().add(new CodeTypeMirror.DeclaredCodeTypeMirror(ElementUtils.fromTypeMirror(factoryType), Arrays.asList(sourceType.asType())));
    addGeneratedBy(context, factory, sourceType);
    TypeMirror returnType = context.getType(com.oracle.truffle.api.instrumentation.InstrumentableFactory.WrapperNode.class);
    CodeExecutableElement createMethod = new CodeExecutableElement(ElementUtils.modifiers(Modifier.PUBLIC), returnType, "createWrapper");
    createMethod.addParameter(new CodeVariableElement(sourceType.asType(), FIELD_DELEGATE));
    createMethod.addParameter(new CodeVariableElement(context.getType(ProbeNode.class), FIELD_PROBE));
    CodeTreeBuilder builder = createMethod.createBuilder();
    ExecutableElement constructor = ElementFilter.constructorsIn(wrapper.getEnclosedElements()).iterator().next();
    String firstParameterReference = null;
    if (constructor.getParameters().size() > 2) {
        TypeMirror firstParameter = constructor.getParameters().get(0).asType();
        if (ElementUtils.typeEquals(firstParameter, sourceType.asType())) {
            firstParameterReference = FIELD_DELEGATE;
        } else if (ElementUtils.typeEquals(firstParameter, context.getType(SourceSection.class))) {
            firstParameterReference = FIELD_DELEGATE + ".getSourceSection()";
        }
    }
    builder.startReturn().startNew(wrapper.asType());
    if (firstParameterReference != null) {
        builder.string(firstParameterReference);
    }
    builder.string(FIELD_DELEGATE).string(FIELD_PROBE);
    builder.end().end();
    factory.add(createMethod);
    return factory;
}
Also used : CodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) TypeElement(javax.lang.model.element.TypeElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) CodeTypeMirror(com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) CodeVariableElement(com.oracle.truffle.dsl.processor.java.model.CodeVariableElement) PackageElement(javax.lang.model.element.PackageElement) Modifier(javax.lang.model.element.Modifier) CodeTreeBuilder(com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)

Aggregations

CodeExecutableElement (com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement)31 CodeTreeBuilder (com.oracle.truffle.dsl.processor.java.model.CodeTreeBuilder)23 CodeVariableElement (com.oracle.truffle.dsl.processor.java.model.CodeVariableElement)18 TypeMirror (javax.lang.model.type.TypeMirror)18 GeneratedTypeMirror (com.oracle.truffle.dsl.processor.java.model.GeneratedTypeMirror)11 ArrayList (java.util.ArrayList)11 ArrayCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.ArrayCodeTypeMirror)10 ExecutableElement (javax.lang.model.element.ExecutableElement)10 DeclaredCodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror.DeclaredCodeTypeMirror)9 CodeTypeElement (com.oracle.truffle.dsl.processor.java.model.CodeTypeElement)8 SpecializationData (com.oracle.truffle.dsl.processor.model.SpecializationData)8 VariableElement (javax.lang.model.element.VariableElement)8 TypeElement (javax.lang.model.element.TypeElement)6 Arrays (java.util.Arrays)5 CodeTree (com.oracle.truffle.dsl.processor.java.model.CodeTree)4 NodeExecutionData (com.oracle.truffle.dsl.processor.model.NodeExecutionData)4 Modifier (javax.lang.model.element.Modifier)4 CodeTypeMirror (com.oracle.truffle.dsl.processor.java.model.CodeTypeMirror)3 NodeChildData (com.oracle.truffle.dsl.processor.model.NodeChildData)3 SpecializationGroup (com.oracle.truffle.dsl.processor.parser.SpecializationGroup)3