@@ -50,19 +50,14 @@ public AndroidManifestFinder(ProcessingEnvironment processingEnv) {
5050 this .processingEnv = processingEnv ;
5151 }
5252
53- public AndroidManifest extractAndroidManifest () {
54- try {
55- return extractAndroidManifestThrowing ();
56- } catch (Exception e ) {
57- if (e instanceof RuntimeException ) {
58- throw (RuntimeException ) e ;
59- }
60- throw new RuntimeException (e );
53+ public Option <AndroidManifest > extractAndroidManifest () {
54+ Option <File > androidManifestFileOption = findManifestFile ();
55+
56+ if (androidManifestFileOption .isAbsent ()) {
57+ return Option .absent ();
6158 }
62- }
6359
64- private AndroidManifest extractAndroidManifestThrowing () throws Exception {
65- File androidManifestFile = findManifestFileThrowing ();
60+ File androidManifestFile = androidManifestFileOption .get ();
6661
6762 String projectDirectory = androidManifestFile .getParent ();
6863
@@ -71,39 +66,41 @@ private AndroidManifest extractAndroidManifestThrowing() throws Exception {
7166 boolean libraryProject = false ;
7267 if (projectProperties .exists ()) {
7368 Properties properties = new Properties ();
74- properties .load (new FileInputStream (projectProperties ));
75-
76- if (properties .containsKey ("android.library" )) {
77- String androidLibraryProperty = properties .getProperty ("android.library" );
78- libraryProject = androidLibraryProperty .equals ("true" );
79-
80- Messager messager = processingEnv .getMessager ();
81- messager .printMessage (Kind .NOTE , "Found android.library property in project.properties, value: " + libraryProject );
69+ try {
70+ properties .load (new FileInputStream (projectProperties ));
71+ if (properties .containsKey ("android.library" )) {
72+ String androidLibraryProperty = properties .getProperty ("android.library" );
73+ libraryProject = androidLibraryProperty .equals ("true" );
74+
75+ Messager messager = processingEnv .getMessager ();
76+ messager .printMessage (Kind .NOTE , "Found android.library property in project.properties, value: " + libraryProject );
77+ }
78+ } catch (IOException ignored ) {
8279 }
83-
8480 }
8581
86- return parseThrowing (androidManifestFile , libraryProject );
82+ return parse (androidManifestFile , libraryProject );
8783 }
8884
89- private File findManifestFileThrowing () throws Exception {
85+ private Option < File > findManifestFile () {
9086 if (processingEnv .getOptions ().containsKey (ANDROID_MANIFEST_FILE_OPTION )) {
9187 return findManifestInSpecifiedPath ();
9288 } else {
9389 return findManifestInParentsDirectories ();
9490 }
9591 }
9692
97- private File findManifestInSpecifiedPath () {
93+ private Option < File > findManifestInSpecifiedPath () {
9894 String path = processingEnv .getOptions ().get (ANDROID_MANIFEST_FILE_OPTION );
9995 File androidManifestFile = new File (path );
10096 Messager messager = processingEnv .getMessager ();
10197 if (!androidManifestFile .exists ()) {
102- throw new IllegalStateException ("Could not find the AndroidManifest.xml file in specified path : " + path );
98+ messager .printMessage (Kind .ERROR , "Could not find the AndroidManifest.xml file in specified path : " + path );
99+ return Option .absent ();
103100 } else {
104101 messager .printMessage (Kind .NOTE , "AndroidManifest.xml file found: " + androidManifestFile .toString ());
105102 }
106- return androidManifestFile ;
103+ return Option . of ( androidManifestFile ) ;
107104 }
108105
109106 /**
@@ -113,10 +110,18 @@ private File findManifestInSpecifiedPath() {
113110 * find the AndroidManifest.xml file. Any better solution will be
114111 * appreciated.
115112 */
116- private File findManifestInParentsDirectories () throws IOException , URISyntaxException {
113+ private Option < File > findManifestInParentsDirectories () {
117114 Filer filer = processingEnv .getFiler ();
118115
119- JavaFileObject dummySourceFile = filer .createSourceFile ("dummy" + System .currentTimeMillis ());
116+ Messager messager = processingEnv .getMessager ();
117+
118+ JavaFileObject dummySourceFile ;
119+ try {
120+ dummySourceFile = filer .createSourceFile ("dummy" + System .currentTimeMillis ());
121+ } catch (IOException ignored ) {
122+ messager .printMessage (Kind .ERROR , "Could not find the AndroidManifest.xml file, unable to create a dummy source file to locate the source folder" );
123+ return Option .absent ();
124+ }
120125 String dummySourceFilePath = dummySourceFile .toUri ().toString ();
121126
122127 if (dummySourceFilePath .startsWith ("file:" )) {
@@ -127,10 +132,13 @@ private File findManifestInParentsDirectories() throws IOException, URISyntaxExc
127132 dummySourceFilePath = "file://" + dummySourceFilePath ;
128133 }
129134
130- Messager messager = processingEnv .getMessager ();
131- messager .printMessage (Kind .NOTE , "Dummy source file: " + dummySourceFilePath );
132-
133- URI cleanURI = new URI (dummySourceFilePath );
135+ URI cleanURI ;
136+ try {
137+ cleanURI = new URI (dummySourceFilePath );
138+ } catch (URISyntaxException e ) {
139+ messager .printMessage (Kind .ERROR , "Could not find the AndroidManifest.xml file, path to dummy source file cannot be parsed: " + dummySourceFilePath );
140+ return Option .absent ();
141+ }
134142
135143 File dummyFile = new File (cleanURI );
136144
@@ -153,27 +161,36 @@ private File findManifestInParentsDirectories() throws IOException, URISyntaxExc
153161 }
154162
155163 if (!androidManifestFile .exists ()) {
156- throw new IllegalStateException ("Could not find the AndroidManifest.xml file, going up from path [" + sourcesGenerationFolder .getAbsolutePath () + "] found using dummy file [" + dummySourceFilePath + "] (max atempts: " + MAX_PARENTS_FROM_SOURCE_FOLDER + ")" );
164+ messager .printMessage (Kind .ERROR , "Could not find the AndroidManifest.xml file, going up from path [" + sourcesGenerationFolder .getAbsolutePath () + "] found using dummy file [" + dummySourceFilePath + "] (max atempts: " + MAX_PARENTS_FROM_SOURCE_FOLDER + ")" );
165+ return Option .absent ();
157166 } else {
158167 messager .printMessage (Kind .NOTE , "AndroidManifest.xml file found: " + androidManifestFile .toString ());
159168 }
160169
161- return androidManifestFile ;
170+ return Option . of ( androidManifestFile ) ;
162171 }
163172
164- private AndroidManifest parseThrowing (File androidManifestFile , boolean libraryProject ) throws Exception {
173+ private Option < AndroidManifest > parse (File androidManifestFile , boolean libraryProject ) {
165174
166175 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance ();
167- DocumentBuilder docBuilder = docBuilderFactory .newDocumentBuilder ();
168- Document doc = docBuilder .parse (androidManifestFile );
176+
177+ Document doc ;
178+ try {
179+ DocumentBuilder docBuilder = docBuilderFactory .newDocumentBuilder ();
180+ doc = docBuilder .parse (androidManifestFile );
181+ } catch (Exception e ) {
182+ Messager messager = processingEnv .getMessager ();
183+ messager .printMessage (Kind .ERROR , "Could not parse the AndroidManifest.xml file at path " + androidManifestFile );
184+ return Option .absent ();
185+ }
169186
170187 Element documentElement = doc .getDocumentElement ();
171188 documentElement .normalize ();
172189
173190 String applicationPackage = documentElement .getAttribute ("package" );
174191
175192 if (libraryProject ) {
176- return AndroidManifest .createLibraryManifest (applicationPackage );
193+ return Option . of ( AndroidManifest .createLibraryManifest (applicationPackage ) );
177194 }
178195
179196 NodeList applicationNodes = documentElement .getElementsByTagName ("application" );
@@ -214,7 +231,7 @@ private AndroidManifest parseThrowing(File androidManifestFile, boolean libraryP
214231 componentQualifiedNames .addAll (receiverQualifiedNames );
215232 componentQualifiedNames .addAll (providerQualifiedNames );
216233
217- return AndroidManifest .createManifest (applicationPackage , applicationQualifiedName , componentQualifiedNames );
234+ return Option . of ( AndroidManifest .createManifest (applicationPackage , applicationQualifiedName , componentQualifiedNames ) );
218235 }
219236
220237 private List <String > extractComponentNames (String applicationPackage , NodeList componentNodes ) {
0 commit comments