Search in sources :

Example 1 with LayerGenerationException

use of org.openide.filesystems.annotations.LayerGenerationException in project netbeans-rcp-lite by outersky.

the class TopComponentProcessor method findInfo.

private Description findInfo(Element e) throws LayerGenerationException {
    Element type;
    switch(e.asType().getKind()) {
        case DECLARED:
            type = e;
            break;
        case EXECUTABLE:
            type = ((DeclaredType) ((ExecutableType) e.asType()).getReturnType()).asElement();
            break;
        default:
            throw new LayerGenerationException("" + e.asType().getKind(), e);
    }
    TopComponent.Description info = type.getAnnotation(TopComponent.Description.class);
    return info;
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) Description(org.openide.windows.TopComponent.Description) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) LayerGenerationException(org.openide.filesystems.annotations.LayerGenerationException) TopComponent(org.openide.windows.TopComponent)

Example 2 with LayerGenerationException

use of org.openide.filesystems.annotations.LayerGenerationException in project netbeans-rcp-lite by outersky.

the class TopComponentProcessor method handleProcess.

@Override
protected boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws LayerGenerationException {
    for (Element e : roundEnv.getElementsAnnotatedWith(TopComponent.Registration.class)) {
        TopComponent.Registration reg = e.getAnnotation(TopComponent.Registration.class);
        if (reg == null) {
            continue;
        }
        Description info = findInfo(e);
        if (info == null) {
            throw new LayerGenerationException("Cannot find TopComponent.Description for this element", e, processingEnv, reg);
        }
        String id = info.preferredID();
        checkValidId(id, e, processingEnv, info);
        String rootFolder;
        String[] roles = reg.roles();
        if (roles.length == 0) {
            rootFolder = "Windows2";
            generateSettingsAndWstcref(e, rootFolder, id, reg, info);
        } else {
            Set<String> uniqueRoles = new HashSet<String>();
            for (String role : roles) {
                if (!uniqueRoles.add(role)) {
                    throw new LayerGenerationException("Duplicate role name found", e, processingEnv, reg);
                }
                if (role.isEmpty()) {
                    throw new LayerGenerationException("Unnamed role found", e, processingEnv, reg);
                }
                rootFolder = "Windows2/Roles/" + role;
                generateSettingsAndWstcref(e, rootFolder, id, reg, info);
            }
        }
    }
    for (Element e : roundEnv.getElementsAnnotatedWith(TopComponent.OpenActionRegistration.class)) {
        TopComponent.OpenActionRegistration reg = e.getAnnotation(TopComponent.OpenActionRegistration.class);
        assert reg != null;
        Description info = findInfo(e);
        ActionID aid = e.getAnnotation(ActionID.class);
        if (aid != null) {
            File actionFile = layer(e).file("Actions/" + aid.category() + "/" + aid.id().replace('.', '-') + ".instance").methodvalue("instanceCreate", "org.openide.windows.TopComponent", "openAction");
            actionFile.instanceAttribute("component", TopComponent.class, reg, null);
            if (reg.preferredID().length() > 0) {
                actionFile.stringvalue("preferredID", reg.preferredID());
            }
            actionFile.bundlevalue("displayName", reg.displayName(), reg, "displayName");
            if (info != null && info.iconBase().length() > 0) {
                actionFile.stringvalue("iconBase", info.iconBase());
            }
            actionFile.write();
        }
    }
    return true;
}
Also used : ActionID(org.openide.awt.ActionID) Description(org.openide.windows.TopComponent.Description) Registration(org.openide.windows.TopComponent.Registration) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) LayerGenerationException(org.openide.filesystems.annotations.LayerGenerationException) File(org.openide.filesystems.annotations.LayerBuilder.File) TopComponent(org.openide.windows.TopComponent) HashSet(java.util.HashSet)

Example 3 with LayerGenerationException

use of org.openide.filesystems.annotations.LayerGenerationException in project netbeans-rcp-lite by outersky.

the class MultiViewProcessor method handleProcess.

@Override
protected boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws LayerGenerationException {
    if (roundEnv.processingOver()) {
        return false;
    }
    TypeMirror pane = null;
    TypeElement typeElement = processingEnv.getElementUtils().getTypeElement(CloneableEditorSupport.Pane.class.getCanonicalName());
    if (typeElement != null) {
        pane = typeElement.asType();
    }
    for (Element e : roundEnv.getElementsAnnotatedWith(MultiViewElement.Registration.class)) {
        MultiViewElement.Registration mvr = e.getAnnotation(MultiViewElement.Registration.class);
        if (mvr.mimeType().length == 0) {
            throw new LayerGenerationException("You must specify mimeType", e, processingEnv, mvr, "mimeType");
        }
        TypeMirror[] exprType = new TypeMirror[1];
        String[] binAndMethodNames = findDefinition(e, exprType, mvr);
        String fileBaseName = binAndMethodNames[0].replace('.', '-');
        if (binAndMethodNames[1] != null) {
            fileBaseName += "-" + binAndMethodNames[1];
        }
        for (String type : mvr.mimeType()) {
            final LayerBuilder builder = layer(e);
            LayerBuilder.File f = builder.file("Editors/" + (type.equals("") ? "" : type + '/') + "MultiView/" + fileBaseName + ".instance");
            f.methodvalue("instanceCreate", MultiViewFactory.class.getName(), "createMultiViewDescription");
            f.stringvalue("instanceClass", ContextAwareDescription.class.getName());
            f.stringvalue("class", binAndMethodNames[0]);
            f.bundlevalue("displayName", mvr.displayName(), mvr, "displayName");
            if (!mvr.iconBase().isEmpty()) {
                builder.validateResource(mvr.iconBase(), e, mvr, "iconBase", true);
                f.stringvalue("iconBase", mvr.iconBase());
            }
            f.stringvalue("preferredID", mvr.preferredID());
            f.intvalue("persistenceType", mvr.persistenceType());
            f.position(mvr.position());
            if (binAndMethodNames[1] != null) {
                f.stringvalue("method", binAndMethodNames[1]);
            }
            if (pane != null && processingEnv.getTypeUtils().isAssignable(exprType[0], pane)) {
                f.boolvalue("sourceview", true);
            }
            f.write();
        }
    }
    return true;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) MultiViewElement(org.netbeans.core.spi.multiview.MultiViewElement) LayerGenerationException(org.openide.filesystems.annotations.LayerGenerationException) MultiViewElement(org.netbeans.core.spi.multiview.MultiViewElement) TypeMirror(javax.lang.model.type.TypeMirror) LayerBuilder(org.openide.filesystems.annotations.LayerBuilder) MultiViewFactory(org.netbeans.core.spi.multiview.MultiViewFactory)

Example 4 with LayerGenerationException

use of org.openide.filesystems.annotations.LayerGenerationException in project netbeans-rcp-lite by outersky.

the class OptionAnnotationProcessorImpl method handleProcess.

@Override
protected boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws LayerGenerationException {
    PrimitiveType boolType = processingEnv.getTypeUtils().getPrimitiveType(TypeKind.BOOLEAN);
    TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
    ArrayType stringArray = processingEnv.getTypeUtils().getArrayType(stringType);
    Set<TypeElement> processors = new HashSet<TypeElement>();
    for (Element e : roundEnv.getElementsAnnotatedWith(Arg.class)) {
        if (e.getModifiers().contains(Modifier.STATIC)) {
            throw new LayerGenerationException("@Arg can be applied only to non-static fields", e);
        }
        if (!e.getModifiers().contains(Modifier.PUBLIC)) {
            throw new LayerGenerationException("@Arg can be applied only to public fields", e);
        }
        if (e.getModifiers().contains(Modifier.FINAL)) {
            throw new LayerGenerationException("@Arg can be applied only to non-final fields", e);
        }
        Arg arg = e.getAnnotation(Arg.class);
        if (arg.longName().isEmpty() && arg.shortName() == Option.NO_SHORT_NAME) {
            throw new LayerGenerationException("At least one of longName or shortName attributes needs to be non-empty", e);
        }
        if (arg.implicit() && !e.asType().equals(stringArray)) {
            throw new LayerGenerationException("implicit @Arg can only be applied to String[] fields", e);
        }
        processors.add(TypeElement.class.cast(e.getEnclosingElement()));
    }
    for (TypeElement te : processors) {
        int cnt = 1;
        final String typeName = processingEnv.getElementUtils().getBinaryName(te).toString().replace('.', '-');
        File f = layer(te).file("Services/OptionProcessors/" + typeName + ".instance");
        f.methodvalue("instanceCreate", DefaultProcessor.class.getName(), "create");
        f.stringvalue("instanceOf", OptionProcessor.class.getName());
        f.stringvalue("class", processingEnv.getElementUtils().getBinaryName(te).toString());
        for (Element e : te.getEnclosedElements()) {
            Arg o = e.getAnnotation(Arg.class);
            if (o == null) {
                continue;
            }
            Description d = e.getAnnotation(Description.class);
            if (o.shortName() != Option.NO_SHORT_NAME) {
                f.charvalue(cnt + ".shortName", o.shortName());
            }
            if (!o.longName().isEmpty()) {
                f.stringvalue(cnt + ".longName", o.longName());
            }
            if (boolType == e.asType()) {
                f.stringvalue(cnt + ".type", "withoutArgument");
            } else if (stringType == e.asType()) {
                if (o.defaultValue().equals("\u0000")) {
                    f.stringvalue(cnt + ".type", "requiredArgument");
                } else {
                    f.stringvalue(cnt + ".type", "optionalArgument");
                }
            } else {
                if (!stringArray.equals(e.asType())) {
                    throw new LayerGenerationException("Field type has to be either boolean, String or String[]!", e);
                }
                f.stringvalue(cnt + ".type", "additionalArguments");
            }
            if (o.implicit()) {
                f.boolvalue(cnt + ".implicit", true);
            }
            if (d != null) {
                writeBundle(f, cnt + ".displayName", d.displayName(), e);
                writeBundle(f, cnt + ".shortDescription", d.shortDescription(), e);
            }
            cnt++;
        }
        f.write();
    }
    return true;
}
Also used : Description(org.netbeans.spi.sendopts.Description) LayerGenerationException(org.openide.filesystems.annotations.LayerGenerationException) ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) Arg(org.netbeans.spi.sendopts.Arg) PrimitiveType(javax.lang.model.type.PrimitiveType) File(org.openide.filesystems.annotations.LayerBuilder.File) OptionProcessor(org.netbeans.spi.sendopts.OptionProcessor) HashSet(java.util.HashSet)

Example 5 with LayerGenerationException

use of org.openide.filesystems.annotations.LayerGenerationException in project netbeans-rcp-lite by outersky.

the class TemplateProcessor method contentURI.

private URI contentURI(Element e, String relativePath, LayerBuilder builder, TemplateRegistration t, String annotationMethod) throws LayerGenerationException {
    String path = LayerBuilder.absolutizeResource(e, relativePath);
    builder.validateResource(path, e, t, annotationMethod, false);
    try {
        return new URI("nbresloc", "/" + path, null).normalize();
    } catch (URISyntaxException x) {
        throw new LayerGenerationException("could not translate " + path, e, processingEnv, t);
    }
}
Also used : LayerGenerationException(org.openide.filesystems.annotations.LayerGenerationException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

LayerGenerationException (org.openide.filesystems.annotations.LayerGenerationException)19 TypeElement (javax.lang.model.element.TypeElement)14 Element (javax.lang.model.element.Element)9 ExecutableElement (javax.lang.model.element.ExecutableElement)8 TypeMirror (javax.lang.model.type.TypeMirror)7 File (org.openide.filesystems.annotations.LayerBuilder.File)7 LayerBuilder (org.openide.filesystems.annotations.LayerBuilder)5 VariableElement (javax.lang.model.element.VariableElement)4 PackageElement (javax.lang.model.element.PackageElement)3 HashSet (java.util.HashSet)2 DeclaredType (javax.lang.model.type.DeclaredType)2 ActionID (org.openide.awt.ActionID)2 TopComponent (org.openide.windows.TopComponent)2 Description (org.openide.windows.TopComponent.Description)2 ActionListener (java.awt.event.ActionListener)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 List (java.util.List)1