Skip to content

Commit

Permalink
2.7.6 Refactor + Enhancement (#5772)
Browse files Browse the repository at this point in the history
* Polish /#5745 : Increasing the stack size in the start.sh

* Polish /#5297 : Only one of the multiple registration centers using nacos can register

* Polish /#5442 : VERSION_KEY和GROUP_KEY为空时,注册到NACOS的服务名与alibaba实现不一致,导致无法消费

* Polish /#5442 : Merge upstream/master

* Polish /apache/dubbo##5239 : Mock字段注入异常

* Polish /apache/dubbo##5239 : Mock字段注入异常

* Polish /#5770 : Removing the interinal JDK API from FileSystemDynamicConfiguration

* Polish /#5771 : [Enhancement] Refactor the APT test-cases implementation of dubbo-metadata-processor in Java 9+

* Bugfix for the test-cases
  • Loading branch information
mercyblitz authored Feb 21, 2020
1 parent 12d99d6 commit 56f1c18
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.dubbo.common.utils.NamedThreadFactory;
import org.apache.dubbo.common.utils.StringUtils;

import com.sun.nio.file.SensitivityWatchEventModifier;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -527,20 +526,15 @@ private static <T> T[] of(T... values) {
}

private static Integer initDelay(WatchEvent.Modifier[] modifiers) {
return Stream.of(modifiers)
.filter(modifier -> modifier instanceof SensitivityWatchEventModifier)
.map(SensitivityWatchEventModifier.class::cast)
.map(SensitivityWatchEventModifier::sensitivityValueInSeconds)
.max(Integer::compareTo)
.orElse(null);
if (isBasedPoolingWatchService()) {
return 2;
} else {
return null;
}
}

private static WatchEvent.Modifier[] initWatchEventModifiers() {
if (isBasedPoolingWatchService()) { // If based on PollingWatchService, High sensitivity will be used
return of(SensitivityWatchEventModifier.HIGH);
} else {
return of();
}
return of();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,29 @@
package org.apache.dubbo.metadata.annotation.processing;

import org.apache.dubbo.metadata.annotation.processing.util.TypeUtils;
import org.apache.dubbo.metadata.tools.Compiler;
import org.apache.dubbo.metadata.tools.TestProcessor;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.LinkedHashSet;
import java.util.Set;

/**
* Abstract {@link Annotation} Processing Test case
*
* @since 2.7.6
*/
@ExtendWith(CompilerInvocationInterceptor.class)
public abstract class AbstractAnnotationProcessingTest {

static ThreadLocal<AbstractAnnotationProcessingTest> testInstanceHolder = new ThreadLocal<>();

protected ProcessingEnvironment processingEnv;

protected Elements elements;
Expand All @@ -46,17 +48,12 @@ public abstract class AbstractAnnotationProcessingTest {

@BeforeEach
public final void init() throws IOException {
Set<Class<?>> classesToBeCompiled = new LinkedHashSet<>();
classesToBeCompiled.add(getClass());
addCompiledClasses(classesToBeCompiled);
TestProcessor testProcessor = new TestProcessor();
Compiler compiler = new Compiler();
compiler.processors(testProcessor);
compiler.compile(classesToBeCompiled.toArray(new Class[0]));
processingEnv = testProcessor.getProcessingEnvironment();
elements = processingEnv.getElementUtils();
types = processingEnv.getTypeUtils();
beforeEach();
testInstanceHolder.set(this);
}

@AfterEach
public final void destroy() {
testInstanceHolder.remove();
}

protected abstract void addCompiledClasses(Set<Class<?>> classesToBeCompiled);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dubbo.metadata.annotation.processing;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import java.lang.reflect.Method;
import java.util.Set;

import static javax.lang.model.SourceVersion.latestSupported;

@SupportedAnnotationTypes("*")
public class AnnotationProcessingTestProcessor extends AbstractProcessor {

private final AbstractAnnotationProcessingTest abstractAnnotationProcessingTest;
private final InvocationInterceptor.Invocation<Void> invocation;

private final ReflectiveInvocationContext<Method> invocationContext;

private final ExtensionContext extensionContext;

public AnnotationProcessingTestProcessor(AbstractAnnotationProcessingTest abstractAnnotationProcessingTest, InvocationInterceptor.Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) {
this.abstractAnnotationProcessingTest = abstractAnnotationProcessingTest;
this.invocation = invocation;
this.invocationContext = invocationContext;
this.extensionContext = extensionContext;
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) {
prepare();
abstractAnnotationProcessingTest.beforeEach();
try {
invocation.proceed();
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
return false;
}

private void prepare() {
abstractAnnotationProcessingTest.processingEnv = super.processingEnv;
abstractAnnotationProcessingTest.elements = super.processingEnv.getElementUtils();
abstractAnnotationProcessingTest.types = super.processingEnv.getTypeUtils();
}

@Override
public SourceVersion getSupportedSourceVersion() {
return latestSupported();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dubbo.metadata.annotation.processing;

import org.apache.dubbo.metadata.tools.Compiler;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.InvocationInterceptor;
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;

import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.Set;

import static org.apache.dubbo.metadata.annotation.processing.AbstractAnnotationProcessingTest.testInstanceHolder;

public class CompilerInvocationInterceptor implements InvocationInterceptor {

@Override
public void interceptTestMethod(Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) throws Throwable {
Set<Class<?>> classesToBeCompiled = new LinkedHashSet<>();
AbstractAnnotationProcessingTest abstractAnnotationProcessingTest = testInstanceHolder.get();
classesToBeCompiled.add(getClass());
abstractAnnotationProcessingTest.addCompiledClasses(classesToBeCompiled);
Compiler compiler = new Compiler();
compiler.processors(new AnnotationProcessingTestProcessor(abstractAnnotationProcessingTest, invocation, invocationContext, extensionContext));
compiler.compile(classesToBeCompiled.toArray(new Class[0]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,16 @@ private File javaSourceFile(String sourceClassName) {

public boolean compile(Class<?>... sourceClasses) {
JavaCompiler.CompilationTask task = javaCompiler.getTask(null, this.javaFileManager, null,
asList("-parameters"),
asList("-parameters", "-Xlint:unchecked", "-nowarn", "-Xlint:deprecation"),
// null,
null, getJavaFileObjects(sourceClasses));
if (!processors.isEmpty()) {
task.setProcessors(processors);
}
return task.call();
}

public JavaCompiler getJavaCompiler() {
return javaCompiler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import java.util.Set;

import static javax.lang.model.SourceVersion.latestSupported;

/**
* {@link Processor} for test
*
* @since 2.7.6
*/
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class TestProcessor extends AbstractProcessor {

@Override
Expand All @@ -43,4 +43,8 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
public ProcessingEnvironment getProcessingEnvironment() {
return super.processingEnv;
}

public SourceVersion getSupportedSourceVersion(){
return latestSupported();
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

<properties>
<!-- Test libs -->
<junit_jupiter_version>5.4.0</junit_jupiter_version>
<junit_jupiter_version>5.6.0</junit_jupiter_version>
<hazelcast_version>3.11.1</hazelcast_version>
<hamcrest_version>1.3</hamcrest_version>
<hibernate_validator_version>5.2.4.Final</hibernate_validator_version>
Expand Down

0 comments on commit 56f1c18

Please sign in to comment.