Revert "Don't go through RSContext for the Java reflection."

This reverts commit cc1b9699446aea20773e4c3c6ff5759fedd8ab51.

Change-Id: I330f8beb02ba4868ce115848543c506d5dea46bf
diff --git a/slang_rs.cpp b/slang_rs.cpp
index 5caa5e5..eb2bdbb 100644
--- a/slang_rs.cpp
+++ b/slang_rs.cpp
@@ -36,7 +36,6 @@
 #include "slang_rs_context.h"
 #include "slang_rs_export_type.h"
 
-#include "slang_rs_reflection.h"
 #include "slang_rs_reflection_cpp.h"
 
 namespace slang {
@@ -75,9 +74,19 @@
   }
 }
 
+bool SlangRS::reflectToJava(const std::string &OutputPathBase,
+                            const std::string &RSPackageName,
+                            bool EmbedBitcodeInJava) {
+  return mRSContext->reflectToJava(OutputPathBase,
+                                   RSPackageName,
+                                   getInputFileName(),
+                                   getOutputFileName(),
+                                   EmbedBitcodeInJava);
+}
+
 bool SlangRS::generateJavaBitcodeAccessor(const std::string &OutputPathBase,
-                                          const std::string &PackageName,
-                                          const std::string *LicenseNote) {
+                                      const std::string &PackageName,
+                                      const std::string *LicenseNote) {
   RSSlangReflectUtils::BitCodeAccessorContext BCAccessorContext;
 
   BCAccessorContext.rsFileName = getInputFileName().c_str();
@@ -349,19 +358,9 @@
             return false;
           }
       } else {
-        if (!RSPackageName.empty()) {
-          mRSContext->setRSPackageName(RSPackageName);
-        }
 
-        RSReflectionJava R(mRSContext, &mGeneratedFileNames);
-        bool ret = R.reflect(JavaReflectionPathBase, mRSContext->getRSPackageName(), RSPackageName,
-                             getInputFileName(), getOutputFileName(), (BitcodeStorage == BCST_JAVA_CODE));
-        if (!ret) {
-          // TODO Is this needed or will the error message have been printed
-          // already? and why not for the C++ case?
-          fprintf(stderr,
-                  "RSContext::reflectToJava : failed to do reflection (%s)\n",
-                  R.getLastError());
+        if (!reflectToJava(JavaReflectionPathBase, RSPackageName, (BitcodeStorage == BCST_JAVA_CODE))) {
+          return false;
         }
 
         for (std::vector<std::string>::const_iterator
diff --git a/slang_rs.h b/slang_rs.h
index bce77fa..65653b6 100644
--- a/slang_rs.h
+++ b/slang_rs.h
@@ -62,6 +62,10 @@
   typedef llvm::StringMap<ReflectedDefinitionTy> ReflectedDefinitionListTy;
   ReflectedDefinitionListTy ReflectedDefinitions;
 
+  bool reflectToJava(const std::string &OutputPathBase,
+                     const std::string &RSPackageName,
+                     bool EmbedBitcodeInJava);
+
   bool generateJavaBitcodeAccessor(const std::string &OutputPathBase,
                                    const std::string &PackageName,
                                    const std::string *LicenseNote);
diff --git a/slang_rs_context.cpp b/slang_rs_context.cpp
index 3401552..59929e2 100644
--- a/slang_rs_context.cpp
+++ b/slang_rs_context.cpp
@@ -278,6 +278,31 @@
   }
 }
 
+bool RSContext::reflectToJava(const std::string &OutputPathBase,
+                              const std::string &RSPackageName,
+                              const std::string &InputFileName,
+                              const std::string &OutputBCFileName,
+                              bool EmbedBitcodeInJava) {
+  if (!RSPackageName.empty()) {
+    mRSPackageName = RSPackageName;
+  }
+
+  // If we are not targeting the actual Android Renderscript classes,
+  // we should reflect code that works with the compatibility library.
+  if (mRSPackageName.compare("android.renderscript") != 0) {
+    mIsCompatLib = true;
+  }
+
+  RSReflectionJava *R = new RSReflectionJava(this, mGeneratedFileNames);
+  bool ret = R->reflect(OutputPathBase, mReflectJavaPackageName, mRSPackageName,
+                        InputFileName, OutputBCFileName, EmbedBitcodeInJava);
+  if (!ret)
+    fprintf(stderr, "RSContext::reflectToJava : failed to do reflection "
+                    "(%s)\n", R->getLastError());
+  delete R;
+  return ret;
+}
+
 RSContext::~RSContext() {
   delete mLicenseNote;
   delete mDataLayout;
diff --git a/slang_rs_context.h b/slang_rs_context.h
index 6e14c4e..0b70bdf 100644
--- a/slang_rs_context.h
+++ b/slang_rs_context.h
@@ -143,11 +143,6 @@
 
   inline void setRSPackageName(const std::string &S) {
     mRSPackageName = S;
-    // If we are not targeting the actual Android Renderscript classes,
-    // we should reflect code that works with the compatibility library.
-    if (mRSPackageName.compare("android.renderscript") != 0) {
-      mIsCompatLib = true;
-    }
   }
   inline const std::string &getRSPackageName() const {
     return mRSPackageName;
@@ -219,6 +214,12 @@
   // and return true.
   bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
 
+  bool reflectToJava(const std::string &OutputPathBase,
+                     const std::string &RSPackageName,
+                     const std::string &InputFileName,
+                     const std::string &OutputBCFileName,
+                     bool EmbedBitcodeInJava);
+
   int getVersion() const { return version; }
   void setVersion(int v) {
     version = v;