0

I'm trying to look into seperate compilation of Java, specifically, compiling a class without all the classes/interfaces it depends on.

For example, let's say I have the following file Foo.java:

public class Foo extends Bar{
  //class body
}

But don't have a java/class file for Bar. I'm looking for tools that would allow me to compile Foo.java alone, and add its dependencies at a later stage.

Anything about how to do so helps, from papers about the subject and how it can/can't be done to existing, working tools/frameworks that allow this.

  • 1
    I have to ask - what's the scenario where this is important? – Oliver Charlesworth Jul 09 '17 at 19:59
  • 2
    *"I'm looking for tools"* ... you should first look what kind of question are on-topic here. – Tom Jul 09 '17 at 20:01
  • 1
    The purpose of an interface is to provide information about the methods that can be called on an object. Without having (at least) the bytecode, how should the compiler know what methods exist and what their signatures look like? What you may think of is a separate *API* jar that comes with all the interfaces and can be used along with one ore more *impl* jars providing implementations for that interfaces. – Timothy Truckle Jul 09 '17 at 20:03
  • Possible duplicate of [Converting .java in to .class without Compiling](https://stackoverflow.com/questions/35288632/converting-java-in-to-class-without-compiling) – Kh.Taheri Jul 09 '17 at 20:04
  • you cannot, unless you provide a dummy class/interface with a proper package and class name and supply expected methods and properties used in your code: it can work well for interfaces, but you may run into many issues trying to mock a class that you do not know - those will end up with `NoClassDefFoundError` at runtime – Vladimir L. Jul 09 '17 at 20:05
  • 2
    In this scenario, it is impossible to determine if `Foo` is valid without knowing anything about `Bar`. Specifically, we need to know if `Bar` is final, which would render `Foo` invalid in its entirety. – Joe C Jul 09 '17 at 20:06
  • As @OliverCharlesworth said, the scenario here is important. If this is just in general, then the answer is that it's not possible. But if you are looking to modify a compiled JAR or something like that then there are (kinda complicated) options available. – rococo Jul 09 '17 at 20:07
  • 1
    Joe has the correct explanation why you should simply forget about this idea. Assume Bar defines toString as final. And your Bar tries to override toString. Without knowing Foo that would be legal - but when looking into Foo... It is invalid. Now what? My suggestion : forget about this idea. – GhostCat Jul 09 '17 at 20:09

2 Answers2

0

It cannot be done, as there should never be unverified bytecode.

However one could do syntactical verification by some grammar tool.

One could could compile against interfaces bypassing the actual implementing classes.

In java 9 there comes the interactive shell JShell, REPL, which allows isolated creating declarations and statements; partial code.

It is doubtful whether one of these is what you are looking for. Maybe switch to the nether regions of JavaScript.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
-1

Normally in java if you code using reflection api,then only ur code can be independently build,as all resolutions are done at run time.e.g. Class.forName doesnt require the class to be present during compile time.But this would mean you will have to rewrite the simple code of a class in a very complex way which sometimes is an overhead when trying to define a simple POJO class which is your case in this instance.Do explain the entire scenario or the requirement to do so,as this might help others to suggest something else as looking at the bigger picture helps sometimes

now30
  • 40
  • 6