Search in sources :

Example 1 with JFormatter

use of com.helger.jcodemodel.JFormatter in project androidannotations by androidannotations.

the class APTCodeModelHelper method replaceSuperCall.

public void replaceSuperCall(JMethod method, JBlock replacement) {
    String superCallStart = "super." + method.name() + "(";
    JBlock oldBody = removeBody(method);
    JBlock newBody = method.body();
    for (Object content : oldBody.getContents()) {
        StringWriter writer = new StringWriter();
        JFormatter formatter = new JFormatter(writer);
        IJStatement statement = (IJStatement) content;
        statement.state(formatter);
        String statementString = writer.getBuffer().toString();
        if (statementString.startsWith(superCallStart)) {
            newBody.add(replacement);
        } else {
            newBody.add(statement);
        }
    }
}
Also used : StringWriter(java.io.StringWriter) IJStatement(com.helger.jcodemodel.IJStatement) JFormatter(com.helger.jcodemodel.JFormatter) JBlock(com.helger.jcodemodel.JBlock)

Example 2 with JFormatter

use of com.helger.jcodemodel.JFormatter in project androidannotations by androidannotations.

the class APTCodeModelHelper method addSuppressWarnings.

public void addSuppressWarnings(IJAnnotatable generatedElement, String annotationValue) {
    Collection<JAnnotationUse> annotations = generatedElement.annotations();
    for (JAnnotationUse annotationUse : annotations) {
        if (SuppressWarnings.class.getCanonicalName().equals(annotationUse.getAnnotationClass().fullName())) {
            AbstractJAnnotationValue value = annotationUse.getParam("value");
            StringWriter code = new StringWriter();
            JFormatter formatter = new JFormatter(code);
            formatter.generable(value);
            if (!code.toString().contains(annotationValue)) {
                if (value instanceof JAnnotationArrayMember) {
                    ((JAnnotationArrayMember) value).param(annotationValue);
                } else {
                    String foundValue = code.toString().substring(1, code.toString().length() - 1);
                    JAnnotationArrayMember newParamArray = annotationUse.paramArray("value");
                    newParamArray.param(foundValue).param(annotationValue);
                }
            }
            return;
        }
    }
    generatedElement.annotate(SuppressWarnings.class).param("value", annotationValue);
}
Also used : StringWriter(java.io.StringWriter) AbstractJAnnotationValue(com.helger.jcodemodel.AbstractJAnnotationValue) JFormatter(com.helger.jcodemodel.JFormatter) JAnnotationUse(com.helger.jcodemodel.JAnnotationUse) JAnnotationArrayMember(com.helger.jcodemodel.JAnnotationArrayMember)

Aggregations

JFormatter (com.helger.jcodemodel.JFormatter)2 StringWriter (java.io.StringWriter)2 AbstractJAnnotationValue (com.helger.jcodemodel.AbstractJAnnotationValue)1 IJStatement (com.helger.jcodemodel.IJStatement)1 JAnnotationArrayMember (com.helger.jcodemodel.JAnnotationArrayMember)1 JAnnotationUse (com.helger.jcodemodel.JAnnotationUse)1 JBlock (com.helger.jcodemodel.JBlock)1