use of org.eclipse.xtext.common.types.JvmDeclaredType in project xtext-xtend by eclipse.
the class XtendCompilerTest method compileAllClassesWithTheSameFileHeader.
@Test
public void compileAllClassesWithTheSameFileHeader() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("/**");
_builder.newLine();
_builder.append(" ");
_builder.append("* Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.");
_builder.newLine();
_builder.append(" ");
_builder.append("* This program and the accompanying materials are made available under the");
_builder.newLine();
_builder.append(" ");
_builder.append("* terms of the Eclipse Public License 2.0 which is available at");
_builder.newLine();
_builder.append(" ");
_builder.append("* http://www.eclipse.org/legal/epl-2.0.");
_builder.newLine();
_builder.append(" ");
_builder.append("* ");
_builder.newLine();
_builder.append(" ");
_builder.append("* SPDX-License-Identifier: EPL-2.0");
_builder.newLine();
_builder.append(" ");
_builder.append("*/");
_builder.newLine();
_builder.append("package foo");
_builder.newLine();
_builder.newLine();
_builder.append("class bar {");
_builder.newLine();
_builder.append(" ");
_builder.append("String name = \'foobar\'");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class baz {");
_builder.newLine();
_builder.append(" ");
_builder.append("String name = \'foobaz\'");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final String input = _builder.toString();
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("/**");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("* Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("* This program and the accompanying materials are made available under the");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("* terms of the Eclipse Public License 2.0 which is available at");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("* http://www.eclipse.org/legal/epl-2.0.");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("* ");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("* SPDX-License-Identifier: EPL-2.0");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("*/");
_builder_1.newLine();
_builder_1.append("package foo;");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@SuppressWarnings(\"all\")");
_builder_1.newLine();
_builder_1.append("public class bar {");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("private String name = \"foobar\";");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
final String expectedBarClass = _builder_1.toString();
StringConcatenation _builder_2 = new StringConcatenation();
_builder_2.append("/**");
_builder_2.newLine();
_builder_2.append(" ");
_builder_2.append("* Copyright (c) 2011 itemis AG (http://www.itemis.eu) and others.");
_builder_2.newLine();
_builder_2.append(" ");
_builder_2.append("* This program and the accompanying materials are made available under the");
_builder_2.newLine();
_builder_2.append(" ");
_builder_2.append("* terms of the Eclipse Public License 2.0 which is available at");
_builder_2.newLine();
_builder_2.append(" ");
_builder_2.append("* http://www.eclipse.org/legal/epl-2.0.");
_builder_2.newLine();
_builder_2.append(" ");
_builder_2.append("* ");
_builder_2.newLine();
_builder_2.append(" ");
_builder_2.append("* SPDX-License-Identifier: EPL-2.0");
_builder_2.newLine();
_builder_2.append(" ");
_builder_2.append("*/");
_builder_2.newLine();
_builder_2.append("package foo;");
_builder_2.newLine();
_builder_2.newLine();
_builder_2.append("@SuppressWarnings(\"all\")");
_builder_2.newLine();
_builder_2.append("public class baz {");
_builder_2.newLine();
_builder_2.append(" ");
_builder_2.append("private String name = \"foobaz\";");
_builder_2.newLine();
_builder_2.append("}");
_builder_2.newLine();
final String expectedBazClass = _builder_2.toString();
final XtendFile file = this.file(input.toString(), true);
final JvmDeclaredType barType = IterableExtensions.<JvmDeclaredType>head(Iterables.<JvmDeclaredType>filter(file.eResource().getContents(), JvmDeclaredType.class));
final JvmDeclaredType bazType = IterableExtensions.<JvmDeclaredType>last(Iterables.<JvmDeclaredType>filter(file.eResource().getContents(), JvmDeclaredType.class));
final GeneratorConfig generatorConfig = this.generatorConfigProvider.get(barType);
CharSequence barJavaCode = this.generator.generateType(barType, generatorConfig);
barJavaCode = this.postProcessor.postProcess(null, barJavaCode);
CharSequence bazJavaCode = this.generator.generateType(bazType, generatorConfig);
bazJavaCode = this.postProcessor.postProcess(null, bazJavaCode);
XtendCompilerTest.assertEquals(expectedBarClass.toString(), barJavaCode.toString());
XtendCompilerTest.assertEquals(expectedBazClass.toString(), bazJavaCode.toString());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.common.types.JvmDeclaredType in project xtext-xtend by eclipse.
the class XtendCompilerErrorHandlingTest method assertCompilesTo.
public void assertCompilesTo(final CharSequence input, final CharSequence expected, final boolean shouldBeSyntacticallyValid) {
try {
final XtendFile file = this.file(input.toString(), false, shouldBeSyntacticallyValid);
final Resource resource = file.eResource();
try {
this.issueProviderFactory.attachData(resource);
final JvmDeclaredType inferredType = IterableExtensions.<JvmDeclaredType>head(Iterables.<JvmDeclaredType>filter(resource.getContents(), JvmDeclaredType.class));
CharSequence javaCode = this.generator.generateType(inferredType, this.generatorConfigProvider.get(inferredType));
javaCode = this.postProcessor.postProcess(null, javaCode);
Assert.assertEquals(expected.toString(), javaCode.toString());
} finally {
this.issueProviderFactory.detachData(resource);
}
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.common.types.JvmDeclaredType in project xtext-xtend by eclipse.
the class XtendJvmModelInferrer method infer.
@Override
public void infer(/* @Nullable */
EObject object, final /* @NonNull */
IJvmDeclaredTypeAcceptor acceptor, boolean preIndexingPhase) {
if (!(object instanceof XtendFile))
return;
final Set<JvmDeclaredType> types = new LinkedHashSet<JvmDeclaredType>();
final IJvmDeclaredTypeAcceptor wrapper = new IJvmDeclaredTypeAcceptor() {
@SuppressWarnings("deprecation")
@Override
public <T extends JvmDeclaredType> IPostIndexingInitializing<T> accept(T type) {
types.add(type);
return acceptor.accept(type);
}
@Override
public <T extends JvmDeclaredType> void accept(T type, Procedure1<? super T> lateInitialization) {
types.add(type);
acceptor.accept(type, lateInitialization);
}
};
final XtendFile xtendFile = (XtendFile) object;
generatorConfig = generatorConfigProvider.get(xtendFile);
final List<Runnable> doLater = newArrayList();
for (final XtendTypeDeclaration declaration : xtendFile.getXtendTypes()) {
inferTypeSceleton(declaration, wrapper, preIndexingPhase, xtendFile, doLater, null);
}
ActiveAnnotationContexts contexts = null;
BatchLinkableResource resource = (BatchLinkableResource) xtendFile.eResource();
try {
compilerPhases.setIndexing(xtendFile, true);
try {
contexts = contextProvider.computeContext(xtendFile);
} catch (Throwable t) {
operationCanceledManager.propagateAsErrorIfCancelException(t);
logger.error("Couldn't create annotation contexts", t);
return;
}
try {
contexts.before(ActiveAnnotationContexts.AnnotationCallback.INDEXING);
for (ActiveAnnotationContext ctx : contexts.getContexts().values()) {
try {
annotationProcessor.indexingPhase(ctx, wrapper, CancelIndicator.NullImpl);
} catch (Throwable t) {
operationCanceledManager.propagateAsErrorIfCancelException(t);
ctx.handleProcessingError(xtendFile.eResource(), t);
}
}
} finally {
contexts.after(ActiveAnnotationContexts.AnnotationCallback.INDEXING);
}
} finally {
compilerPhases.setIndexing(xtendFile, false);
resource.getCache().clear(resource);
}
if (!preIndexingPhase) {
final ActiveAnnotationContexts finalContexts = contexts;
Runnable lateInit = new Runnable() {
@Override
public void run() {
for (Runnable runnable : doLater) {
runnable.run();
}
try {
finalContexts.before(ActiveAnnotationContexts.AnnotationCallback.INFERENCE);
for (ActiveAnnotationContext ctx : finalContexts.getContexts().values()) {
try {
annotationProcessor.inferencePhase(ctx, CancelIndicator.NullImpl);
} catch (Throwable t) {
operationCanceledManager.propagateAsErrorIfCancelException(t);
ctx.handleProcessingError(xtendFile.eResource(), t);
}
}
} finally {
finalContexts.after(ActiveAnnotationContexts.AnnotationCallback.INFERENCE);
}
}
};
resource.addJvmMemberInitializer(lateInit);
}
}
use of org.eclipse.xtext.common.types.JvmDeclaredType in project xtext-xtend by eclipse.
the class XtendJvmModelInferrer method initialize.
protected void initialize(XtendClass source, JvmGenericType inferredJvmType) {
inferredJvmType.setVisibility(source.getVisibility());
boolean isStatic = source.isStatic() && !isTopLevel(source);
if (!isStatic) {
JvmDeclaredType declaringType = inferredJvmType.getDeclaringType();
if (declaringType instanceof JvmGenericType) {
if (((JvmGenericType) declaringType).isInterface())
isStatic = true;
} else if (declaringType instanceof JvmAnnotationType) {
isStatic = true;
}
}
inferredJvmType.setStatic(isStatic);
inferredJvmType.setAbstract(source.isAbstract());
inferredJvmType.setStrictFloatingPoint(source.isStrictFloatingPoint());
if (!inferredJvmType.isAbstract()) {
inferredJvmType.setFinal(source.isFinal());
}
translateAnnotationsTo(source.getAnnotations(), inferredJvmType);
JvmTypeReference extendsClause = source.getExtends();
if (extendsClause == null || extendsClause.getType() == null) {
JvmTypeReference typeRefToObject = typeReferences.getTypeForName(Object.class, source);
if (typeRefToObject != null)
inferredJvmType.getSuperTypes().add(typeRefToObject);
} else {
inferredJvmType.getSuperTypes().add(jvmTypesBuilder.cloneWithProxies(extendsClause));
}
for (JvmTypeReference intf : source.getImplements()) {
inferredJvmType.getSuperTypes().add(jvmTypesBuilder.cloneWithProxies(intf));
}
fixTypeParameters(inferredJvmType);
addDefaultConstructor(source, inferredJvmType);
for (XtendMember member : source.getMembers()) {
if (member instanceof XtendField || (member instanceof XtendFunction && ((XtendFunction) member).getName() != null) || member instanceof XtendConstructor) {
transform(member, inferredJvmType, true);
}
}
appendSyntheticDispatchMethods(source, inferredJvmType);
jvmTypesBuilder.copyDocumentationTo(source, inferredJvmType);
nameClashResolver.resolveNameClashes(inferredJvmType);
}
use of org.eclipse.xtext.common.types.JvmDeclaredType in project xtext-xtend by eclipse.
the class XtendValidator method checkDuplicateAndOverriddenFunctions.
@Check
public void checkDuplicateAndOverriddenFunctions(XtendTypeDeclaration xtendType) {
final JvmDeclaredType inferredType = associations.getInferredType(xtendType);
if (inferredType instanceof JvmGenericType) {
JavaVersion targetVersion = getGeneratorConfig(xtendType).getJavaSourceVersion();
ResolvedFeatures resolvedFeatures = overrideHelper.getResolvedFeatures(inferredType, targetVersion);
Set<EObject> flaggedOperations = Sets.newHashSet();
doCheckDuplicateExecutables((JvmGenericType) inferredType, resolvedFeatures, flaggedOperations);
doCheckOverriddenMethods(xtendType, (JvmGenericType) inferredType, resolvedFeatures, flaggedOperations);
doCheckFunctionOverrides(resolvedFeatures, flaggedOperations);
}
}
Aggregations