2

I have this model object representing a Java source file.

It has a constructor like so:

private SourceFile(File file)

I want this constructor to actually make sure (as much as it can) that the File it's being given is actually a Java source.

I have a batch operation that takes a lot of text files. Some of them are Java sources, I wan't a good way to differentiate them (other than file extension).

So has anyone been in this situation before and can you recommend a good way to check plausibility (not validity, for a validity check I'd need to compile it) ?

durron597
  • 31,968
  • 17
  • 99
  • 158
Simeon
  • 7,582
  • 15
  • 64
  • 101

3 Answers3

2

I'd do two things:

  1. Check that the file ends in .java.
  2. Check that the file declares a class that has the same name as the file (see here).
Community
  • 1
  • 1
poida
  • 3,403
  • 26
  • 26
  • That does not ensure that it is a Java source file. There are many languages that have "public class" keywords. And I guess he doesn't want to rely on a file extension. – Willi Mentzel Nov 03 '14 at 12:41
  • By the way: This could have been a comment! – Willi Mentzel Nov 03 '14 at 12:43
  • But how many languages that aren't java have files that end in `.java` and contain a `public class` declaration? – poida Nov 03 '14 at 12:47
  • None comes to my mind but I ensure you "public class" won't be sufficient. On the other hand you don't actually need the class to be public (in Java) and if you would only search for the keyword "class" it could be Scala as well. – Willi Mentzel Nov 03 '14 at 12:56
  • You're right, a java class doesn't have to be public and I updated my answer. I don't know scala, but my superficial search of the web tells me that they typically aren't saved as .java files. – poida Nov 03 '14 at 13:03
  • Yes but you have to consider that a user would want to fool this automatic check. I don't know what the OP is using this for but if you use it for automatic assignment checking your test would complete succesfully without the file actually being a java source. – Willi Mentzel Nov 03 '14 at 13:05
  • Yeah I'm not sure what the OP is using it for either, my rationale for this answer is that it would be a simple and fast way to check that it looks like a java source file. It is by no means fool proof or comprehensive. – poida Nov 03 '14 at 13:07
  • I don't see reason for argument here. We all agree that there is no certain way to check other than compiling it. So the question is, how much validations are we willing to do in order to make sure. In this line of though I agree with @poida here a extension check + checks from proper class start (class, public class, comment prefix etc.) might be enough. – Simeon Nov 03 '14 at 13:14
1

It depends on how accurate you want to be. If you want 100% you have to compile it. If you would be happy with something low you can check printable characters. Reasonable level may be achieved by key work check. And so on...

Dmytro
  • 496
  • 7
  • 17
  • This could have been a comment! – Willi Mentzel Nov 03 '14 at 12:43
  • It is not a comment. It is the answer. Because the question is out of "yes-no" boolean field. The question is about what could be considered a java file. And the answer depends on consideration point. So, I bring this to the attention of Simeon for him to understand there is no simple answer. – Dmytro Nov 03 '14 at 12:50
  • Crozin wrote a comment before you with exactly the same kinds of ideas and he made it a comment. – Willi Mentzel Nov 03 '14 at 12:54
  • No he puts only one proposition. Also it is good it does not reveal that it is not a technical question but depends on what Simeon is trying to solve. Even if file is successfully parsed it may never be compile which means it may be or may be not java. – Dmytro Nov 03 '14 at 13:00
  • You could have added a second proposition as comment. – Willi Mentzel Nov 03 '14 at 13:02
0

Use javaparser, on given link is wiki how to use it. But in Java 1.6 the compiler has an API build in the JDK, through it you can access the results of the Java parser.

1ac0
  • 2,875
  • 3
  • 33
  • 47
  • This looks to be exactly what I need thanks. I will update when I try it out – Simeon Nov 03 '14 at 13:19
  • 3
    @Simeon if it is solution for you then accept answer please ;) – 1ac0 Nov 03 '14 at 13:21
  • @Simeon If answer looks exactly what you need accept answer, it's good habit here. –  Nov 08 '14 at 21:37
  • @marekrendel I will accept the answer once I am able to check that it IS what I need, not that it LOOKS LIKE what I need. You can judge my acceptance ratio by my profile and yes I have (given my 4 years here) read what good practices are :) but I will take you advice into consideration... – Simeon Nov 09 '14 at 11:03