com.hannesdorfmann.parcelableplease.annotation.ParcelablePlease Java Examples
The following examples show how to use
com.hannesdorfmann.parcelableplease.annotation.ParcelablePlease.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParcelablePleaseProcessorTest.java From ParcelablePlease with Apache License 2.0 | 6 votes |
@Test public void abstractClass() { String annotation = ParcelablePlease.class.getCanonicalName(); JavaFileObject componentFile = JavaFileObjects.forSourceLines("test.AbstractClass", "package test;", "", "@" + annotation, "abstract class AbstractClass {}"); Truth.assertAbout(JavaSourceSubjectFactory.javaSource()) .that(componentFile).processedWith(new ParcelablePleaseProcessor()) .failsToCompile() .withErrorContaining("Element AbstractClass is annotated with @ParcelablePlease but is an abstract class. Abstract classes can not be annotated. Annotate the concrete class that implements all abstract methods with @ParcelablePlease"); }
Example #2
Source File: ParcelablePleaseProcessorTest.java From ParcelablePlease with Apache License 2.0 | 6 votes |
@Test public void simpleClass() { String annotation = ParcelablePlease.class.getCanonicalName(); JavaFileObject componentFile = JavaFileObjects.forSourceLines("test.SimpleClass", "package test;", "", "@" + annotation, "class SimpleClass {" + "int id;" + "String name;" + "}"); Truth.assertAbout(JavaSourceSubjectFactory.javaSource()) .that(componentFile).processedWith(new ParcelablePleaseProcessor()) .compilesWithoutError(); }
Example #3
Source File: ParcelablePleaseProcessorTest.java From ParcelablePlease with Apache License 2.0 | 6 votes |
@Test public void innerClass() { String annotation = ParcelablePlease.class.getCanonicalName(); JavaFileObject componentFile = JavaFileObjects.forSourceLines("test.OuterClass", "package test;", "", "class OuterClass {", "@" + annotation, "class InnerClass {", "int id;", "String name;" , "}", "}"); Truth.assertAbout(JavaSourceSubjectFactory.javaSource()) .that(componentFile).processedWith(new ParcelablePleaseProcessor()) .compilesWithoutError(); }
Example #4
Source File: ParcelablePleaseProcessorTest.java From ParcelablePlease with Apache License 2.0 | 6 votes |
@Test public void stringList() { String annotation = ParcelablePlease.class.getCanonicalName(); JavaFileObject componentFile = JavaFileObjects.forSourceLines("test.StringListTest", "package test;", "", "@" + annotation, "class StringListTest {", " java.util.List<String> strings;", "}"); Truth.assertAbout(JavaSourceSubjectFactory.javaSource()) .that(componentFile).processedWith(new ParcelablePleaseProcessor()) .compilesWithoutError(); }
Example #5
Source File: ParcelablePleaseProcessor.java From ParcelablePlease with Apache License 2.0 | 5 votes |
/** * Checks if the element is a class */ private boolean isClass(Element element) { if (element.getKind() == ElementKind.CLASS) { if (element.getModifiers().contains(Modifier.ABSTRACT)) { ProcessorMessage.error(element, "Element %s is annotated with @%s but is an abstract class. " + "Abstract classes can not be annotated. Annotate the concrete class " + "that implements all abstract methods with @%s", element.getSimpleName(), ParcelablePlease.class.getSimpleName(), ParcelablePlease.class.getSimpleName()); return false; } if (element.getModifiers().contains(Modifier.PRIVATE)) { ProcessorMessage.error(element, "The private class %s is annotated with @%s. " + "Private classes are not supported because of lacking visibility.", element.getSimpleName(), ParcelablePlease.class.getSimpleName()); return false; } // Ok, its a valid class return true; } else { ProcessorMessage.error(element, "Element %s is annotated with @%s but is not a class. Only Classes are supported", element.getSimpleName(), ParcelablePlease.class.getSimpleName()); return false; } }
Example #6
Source File: CodeGenerator.java From ParcelablePlease with Apache License 2.0 | 5 votes |
public void generate(TypeElement classElement, List<ParcelableField> fields) throws Exception { String classSuffix = "ParcelablePlease"; String packageName = TypeUtils.getPackageName(elementUtils, classElement); String binaryName = TypeUtils.getBinaryName(elementUtils, classElement); String originFullQualifiedName = classElement.getQualifiedName().toString(); String className; if (packageName.length() > 0) { className = binaryName.substring(packageName.length() + 1) + classSuffix; } else { className = binaryName + classSuffix; } String qualifiedName = binaryName + classSuffix; // // Write code // JavaFileObject jfo = filer.createSourceFile(qualifiedName, classElement); Writer writer = jfo.openWriter(); JavaWriter jw = new JavaWriter(writer); jw.emitPackage(packageName); jw.emitImports("android.os.Parcel"); jw.emitEmptyLine(); jw.emitJavadoc("Generated class by @%s . Do not modify this code!", ParcelablePlease.class.getSimpleName()); jw.beginType(className, "class", EnumSet.of(Modifier.PUBLIC)); jw.emitEmptyLine(); generateWriteToParcel(jw, originFullQualifiedName, fields); jw.emitEmptyLine(); generateReadFromParcel(jw, originFullQualifiedName, fields); jw.endType(); jw.close(); }
Example #7
Source File: ParcelablePleaseProcessor.java From ParcelablePlease with Apache License 2.0 | 4 votes |
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) { Element lastElement = null; CodeGenerator codeGenerator = new CodeGenerator(elementUtils, filer); for (Element element : env.getElementsAnnotatedWith(ParcelablePlease.class)) { if (!isClass(element)) { continue; } List<ParcelableField> fields = new ArrayList<ParcelableField>(); lastElement = element; ParcelablePlease annotation = element.getAnnotation(ParcelablePlease.class); boolean allFields = annotation.allFields(); boolean ignorePrivateFields = annotation.ignorePrivateFields(); List<? extends Element> memberFields = elementUtils.getAllMembers((TypeElement) element); if (memberFields != null) { for (Element member : memberFields) { // Search for fields if (member.getKind() != ElementKind.FIELD || !(member instanceof VariableElement)) { continue; // Not a field, so go on } // it's a field, so go on ParcelableNoThanks skipFieldAnnotation = member.getAnnotation(ParcelableNoThanks.class); if (skipFieldAnnotation != null) { // Field is marked as not parcelabel, so continue with the next continue; } if (!allFields) { ParcelableThisPlease fieldAnnotated = member.getAnnotation(ParcelableThisPlease.class); if (fieldAnnotated == null) { // Not all fields should parcelable, // and this field is not annotated as parcelable, so skip this field continue; } } // Check the visibility of the field and modifiers Set<Modifier> modifiers = member.getModifiers(); if (modifiers.contains(Modifier.STATIC)) { // Static fields are skipped continue; } if (modifiers.contains(Modifier.PRIVATE)) { if (ignorePrivateFields) { continue; } ProcessorMessage.error(member, "The field %s in %s is private. At least default package visibility is required " + "or annotate this field as not been parcelable with @%s " + "or configure this class to ignore private fields " + "with @%s( ignorePrivateFields = true )", member.getSimpleName(), element.getSimpleName(), ParcelableNoThanks.class.getSimpleName(), ParcelablePlease.class.getSimpleName()); } if (modifiers.contains(Modifier.FINAL)) { ProcessorMessage.error(member, "The field %s in %s is final. Final can not be Parcelable", element.getSimpleName(), member.getSimpleName()); } // If we are here the field is be parcelable fields.add(new ParcelableField((VariableElement) member, elementUtils, typeUtils)); } } // // Generate the code // try { codeGenerator.generate((TypeElement) element, fields); } catch (Exception e) { e.printStackTrace(); ProcessorMessage.error(lastElement, "An error has occurred while processing %s : %s", element.getSimpleName(), e.getMessage()); } } // End for loop return true; }