Skip to content

Commit

Permalink
Previous commit (bc5dbad) removed jdt, along with support of the -pro…
Browse files Browse the repository at this point in the history
…cessor flag.

Reimplement support for the flag, using the javac pre-processing step
  • Loading branch information
omerp committed Nov 15, 2018
1 parent eb8f909 commit a87bbf0
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 12 deletions.
1 change: 1 addition & 0 deletions translator/Makefile
Expand Up @@ -285,6 +285,7 @@ RESOURCE_FILES = $(RESOURCES:%=$(CLASS_DIR)/$(BASE_PACKAGE)/%)
TEST_RESOURCES = \
com/google/devtools/j2objc/mappings.j2objc \
com/google/devtools/j2objc/testMappings.j2objc \
com/google/devtools/j2objc/annotations/ExplicitProcessor.jar \
com/google/devtools/j2objc/annotations/Processor.jar \
com/google/devtools/j2objc/util/example.jar \
com/google/devtools/j2objc/util/hello.aar \
Expand Down
Expand Up @@ -145,6 +145,11 @@ private List<String> getJavacOptions(boolean processAnnotations) {
if (lintArgument != null) {
javacOptions.add(lintArgument);
}
String explicitProcessors = options.getProcessors();
if (explicitProcessors != null) {
javacOptions.add("-processor");
javacOptions.add(explicitProcessors);
}
if (processAnnotations) {
javacOptions.add("-proc:only");
} else {
Expand Down Expand Up @@ -235,7 +240,7 @@ public ProcessingResult processAnnotations(Iterable<String> fileArgs,
PathClassLoader loader = new PathClassLoader(options.fileUtil().getClassPathEntries());
loader.addPaths(options.getProcessorPathEntries());
Iterator<Processor> serviceIterator = ServiceLoader.load(Processor.class, loader).iterator();
if (serviceIterator.hasNext()) {
if (serviceIterator.hasNext() || options.getProcessors() != null) {
List<File> inputFiles = new ArrayList<>();
for (ProcessingContext input : inputs) {
inputFiles.add(new File(input.getFile().getAbsolutePath()));
Expand Down
72 changes: 61 additions & 11 deletions translator/src/test/java/com/google/devtools/j2objc/J2ObjCTest.java
Expand Up @@ -195,15 +195,7 @@ public void testSourceDirsOption() throws Exception {
makeAssertionsForJavaFiles(exampleH, exampleM, packageInfoH, packageInfoM);
}

// Test a simple annotation processor on the classpath.
public void testAnnotationProcessing() throws Exception {
String processorPath = getResourceAsFile("annotations/Processor.jar");
options.fileUtil().getClassPathEntries().add(processorPath);

String examplePath = addSourceFile(EXAMPLE_JAVA_SOURCE, "annotations/Example.java");
J2ObjC.run(Collections.singletonList(examplePath), options);
assertErrorCount(0);

private void assertServiceAnnotationProcessorOutput() throws IOException {
String translatedAnnotationHeader = getTranslatedFile("ProcessingResult.h");
String translatedAnnotationImpl = getTranslatedFile("ProcessingResult.m");

Expand All @@ -215,6 +207,30 @@ public void testAnnotationProcessing() throws Exception {
assertTranslation(translatedAnnotationImpl, "return @\"ObjectiveCName\"");
}

private void assertSpecifiedAnnotationProcessorOutput() throws IOException {
String translatedAnnotationHeader = getTranslatedFile("ExplicitProcessingResult.h");
String translatedAnnotationImpl = getTranslatedFile("ExplicitProcessingResult.m");

// Our dummy annotation processor is very simple--it always creates a class with no package,
// ProcessingResult, with a minimal implementation.
assertTranslation(translatedAnnotationHeader, "@interface ExplicitProcessingResult : NSObject");
assertTranslation(translatedAnnotationHeader, "- (NSString *)getResultForExplicitProcessor;");
assertTranslation(translatedAnnotationImpl, "@implementation ExplicitProcessingResult");
assertTranslation(translatedAnnotationImpl, "return @\"ObjectiveCName\"");
}

// Test a simple annotation processor on the classpath.
public void testAnnotationProcessing() throws Exception {
String processorPath = getResourceAsFile("annotations/Processor.jar");
options.fileUtil().getClassPathEntries().add(processorPath);

String examplePath = addSourceFile(EXAMPLE_JAVA_SOURCE, "annotations/Example.java");
J2ObjC.run(Collections.singletonList(examplePath), options);
assertErrorCount(0);

assertServiceAnnotationProcessorOutput();
}

// Test a simple annotation processor on the processor path.
public void testAnnotationProcessingWithProcessorPath() throws Exception {
String processorPath = getResourceAsFile("annotations/Processor.jar");
Expand All @@ -223,17 +239,51 @@ public void testAnnotationProcessingWithProcessorPath() throws Exception {
String examplePath = addSourceFile(EXAMPLE_JAVA_SOURCE, "annotations/Example.java");
J2ObjC.run(Collections.singletonList(examplePath), options);
assertErrorCount(0);

assertServiceAnnotationProcessorOutput();
}

// Test a specified annotation processor.
public void testSpecifiedAnnotationProcessing() throws Exception {
String processorPath = getResourceAsFile("annotations/Processor.jar");
String processorPath = getResourceAsFile("annotations/ExplicitProcessor.jar");
options.fileUtil().getClassPathEntries().add(processorPath);
options.setProcessors("com.google.devtools.j2objc.annotations.J2ObjCTestProcessor");
options.setProcessors("com.google.devtools.j2objc.annotations.J2ObjCTestExplicitProcessor");

String examplePath = addSourceFile(EXAMPLE_JAVA_SOURCE, "annotations/Example.java");
J2ObjC.run(Collections.singletonList(examplePath), options);
assertErrorCount(0);

assertSpecifiedAnnotationProcessorOutput();
}

// Test an explicitly invoked annotation processor on the processor path.
public void testSpecifiedAnnotationProcessingWithProcessorPath() throws Exception {
String processorPath = getResourceAsFile("annotations/ExplicitProcessor.jar");
options.getProcessorPathEntries().add(processorPath);
options.setProcessors("com.google.devtools.j2objc.annotations.J2ObjCTestExplicitProcessor");

String examplePath = addSourceFile(EXAMPLE_JAVA_SOURCE, "annotations/Example.java");
J2ObjC.run(Collections.singletonList(examplePath), options);
assertErrorCount(0);

assertSpecifiedAnnotationProcessorOutput();
}

// Test both types of processor detection at the same time, see that -processor indeed bypasses
// default discovery
public void testSpecifiedAnnotationProcessingBypass() throws Exception {
String serviceProcessorPath = getResourceAsFile("annotations/Processor.jar");
String explicitProcessorPath = getResourceAsFile("annotations/ExplicitProcessor.jar");
options.fileUtil().getClassPathEntries().add(serviceProcessorPath);
options.fileUtil().getClassPathEntries().add(explicitProcessorPath);
options.setProcessors("com.google.devtools.j2objc.annotations.J2ObjCTestExplicitProcessor");

String examplePath = addSourceFile(EXAMPLE_JAVA_SOURCE, "annotations/Example.java");
J2ObjC.run(Collections.singletonList(examplePath), options);
assertErrorCount(0);

assertSpecifiedAnnotationProcessorOutput();
assertFalse("Overridden processor generated output", getTempFile("ProcessingResult.h").exists());
}

// Test for warning if compiling jar with -g.
Expand Down
Binary file not shown.
@@ -0,0 +1,68 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.devtools.j2objc.annotations;

import java.io.IOException;
import java.io.Writer;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
import javax.lang.model.SourceVersion;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;

/**
* A test processor for annotation processing. This is the processor in Processor.jar.
*/
@SupportedAnnotationTypes("com.google.j2objc.annotations.ObjectiveCName")
public class J2ObjCTestExplicitProcessor extends AbstractProcessor {

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement type: annotations) {
Writer w = null;
try {
JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile("ExplicitProcessingResult");
w = sourceFile.openWriter();
w.write("public class ExplicitProcessingResult {"
+ " public String getResultForExplicitProcessor() {"
+ " return \"" + type.getSimpleName() + "\";"
+ " }"
+ "}");
} catch (IOException e) {
processingEnv.getMessager().printMessage(
Diagnostic.Kind.ERROR, "Could not process annotations");
e.printStackTrace();
return false;
} finally {
if (w != null) {
try {
w.close();
} catch (IOException e) {}
}
}
}

return false;
}
}

0 comments on commit a87bbf0

Please sign in to comment.