Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package net.sf.alchim.codeplus.spoonprocessor;
 
public class NullParamProcessor extends AbstractProcessor<CtExecutable<?>> {
    public void process(CtExecutable<?> m) {
        try {
            try {
                if (!(m instanceof CtMethod) && !(m instanceof CtConstructor)) {
                    getEnvironment().report(this.m"the processor only accept method and constructor");
                    return;
                }
                if (m.getModifiers().contains(.)) {
                    getEnvironment().report(this.m"the processor only work on contrect method (not on abstract))");
                    return;
                }
                if (!(m.getDeclaringType() instanceof CtClass)) {
                    getEnvironment().report(this.m"the processor only work into class (not into interface)");
                    return;
                }
                addDefaulting(m);
                addNullChecker(m);
            } finally {
                removeAnnotations(m);
            }
        } catch (Exception exc) {
            throw new ProcessingException(thismexc);
        }
    }
    // TODO: add defaulting to same type as param
    protected void addDefaulting(CtExecutable<?> m) {
        for (CtParameter<?> param : m.getParameters()) {
            Default annotation = param.getAnnotation(Default.class);
            if (annotation == null) {
                continue;
            }
            if ((annotation.value() == null) || (annotation.value().length() == 0)) {
                continue;
            }
            CtExpression<ObjectdefaultExpr = getFactory().Code().createCodeSnippetExpression(annotation.value());
            CtBlock<?> defaultBlock = Substitution.substituteMethodBody((CtClass<?>) m.getDeclaringType(), new NullParamTemplate(paramdefaultExpr), "setDefault"getFactory().Type()
                    .createReference(Object.class));
            m.getBody().insertBegin(defaultBlock);
        }
    }
    protected void addNullChecker(CtExecutable<?> m) {
        CtAnnotation<?> annotation = ProcessorHelper.findAnnotation(mParamsNotNull.class);
        if (annotation == null) {
            return;
        }
        for (CtParameter<?> param : m.getParameters()) {
            if (param.getType().isPrimitive()) {
                continue;
            }
            if (param.getAnnotation(Default.class) != null) {
                ProcessorHelper.removeAnnotation(paramDefault.class);
                continue;
            }
            if (param.getAnnotation(Nullable.class) != null) {
                ProcessorHelper.removeAnnotation(paramNullable.class);
                continue;
            }
            CtBlock<?> checkBlock = Substitution
                    .substituteMethodBody((CtClass<?>) m.getDeclaringType(), new NullParamTemplate(paramnull), "check"getFactory().Type().createReference(Object.class));
            // checkBlock.accept(new
            // SetPositionVisitor(annotation.getPosition()));
            ProcessorHelper.forcePositionOfEveryStmt(checkBlockannotation.getPosition());
            m.getBody().insertBegin(checkBlock);
        }
    }
    protected void removeAnnotations(CtExecutable<?> m) {
        for (CtParameter<?> param : m.getParameters()) {
            ProcessorHelper.removeAnnotation(paramDefault.class);
            ProcessorHelper.removeAnnotation(paramNullable.class);
        }
        ProcessorHelper.removeAnnotation(mParamsNotNull.class);
    }
New to GrepCode? Check out our FAQ X