use of org.codehaus.groovy.ast.ClassNode in project grails-core by grails.
the class AbstractGrailsArtefactTransformer method performStaticImplementationInjection.
protected void performStaticImplementationInjection(ClassNode classNode, Map<String, ClassNode> genericsPlaceholders, Class staticImplementation) {
ClassNode staticImplementationNode = GrailsASTUtils.replaceGenericsPlaceholders(ClassHelper.make(staticImplementation), genericsPlaceholders);
final List<MethodNode> declaredMethods = staticImplementationNode.getMethods();
final String staticImplementationSimpleName = staticImplementation.getSimpleName();
String apiInstanceProperty = STATIC_PREFIX + staticImplementationSimpleName;
final String lookupMethodName = CURRENT_PREFIX + staticImplementationSimpleName;
if (!requiresStaticLookupMethod()) {
final ConstructorCallExpression constructorCallExpression = new ConstructorCallExpression(staticImplementationNode, ZERO_ARGS);
addApiLookupFieldAndSetter(classNode, staticImplementationNode, apiInstanceProperty, constructorCallExpression);
}
MethodNode lookupMethod = createStaticLookupMethod(classNode, staticImplementationNode, apiInstanceProperty, lookupMethodName);
MethodCallExpression apiLookupMethod = new MethodCallExpression(new ClassExpression(classNode), lookupMethodName, ZERO_ARGS);
apiLookupMethod.setMethodTarget(lookupMethod);
for (MethodNode declaredMethod : declaredMethods) {
if (isStaticCandidateMethod(classNode, declaredMethod)) {
addDelegateStaticMethod(classNode, apiLookupMethod, declaredMethod, genericsPlaceholders);
}
}
}
use of org.codehaus.groovy.ast.ClassNode in project grails-core by grails.
the class AbstractGrailsArtefactTransformer method populateAutowiredApiLookupMethod.
protected MethodNode populateAutowiredApiLookupMethod(ClassNode classNode, ClassNode implementationNode, String apiProperty, String methodName, BlockStatement methodBody) {
addApiLookupFieldAndSetter(classNode, implementationNode, apiProperty, null);
VariableExpression apiVar = new VariableExpression(apiProperty, implementationNode);
BlockStatement ifBlock = new BlockStatement();
ArgumentListExpression arguments = new ArgumentListExpression();
arguments.addExpression(new ConstantExpression("Method on class [" + classNode + "] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly."));
ifBlock.addStatement(new ThrowStatement(new ConstructorCallExpression(new ClassNode(IllegalStateException.class), arguments)));
BlockStatement elseBlock = new BlockStatement();
elseBlock.addStatement(new ReturnStatement(apiVar));
methodBody.addStatement(new IfStatement(new BooleanExpression(new BinaryExpression(apiVar, GrailsASTUtils.EQUALS_OPERATOR, GrailsASTUtils.NULL_EXPRESSION)), ifBlock, elseBlock));
MethodNode methodNode = new MethodNode(methodName, PUBLIC_STATIC_MODIFIER, implementationNode, ZERO_PARAMETERS, null, methodBody);
return methodNode;
}
use of org.codehaus.groovy.ast.ClassNode in project grails-core by grails.
the class DefaultGrailsDomainClassInjector method createPropertiesForBelongsToOrHasOneExpression.
private Collection<PropertyNode> createPropertiesForBelongsToOrHasOneExpression(Expression e, ClassNode classNode) {
List<PropertyNode> properties = new ArrayList<>();
if (e instanceof MapExpression) {
MapExpression me = (MapExpression) e;
for (MapEntryExpression mme : me.getMapEntryExpressions()) {
String key = mme.getKeyExpression().getText();
final Expression expression = mme.getValueExpression();
ClassNode type;
if (expression instanceof ClassExpression) {
type = expression.getType();
} else {
type = ClassHelper.make(expression.getText());
}
properties.add(new PropertyNode(key, Modifier.PUBLIC, type, classNode, null, null, null));
}
}
return properties;
}
use of org.codehaus.groovy.ast.ClassNode in project grails-core by grails.
the class DefaultGrailsDomainClassInjector method injectVersionProperty.
private void injectVersionProperty(ClassNode classNode) {
final boolean hasVersion = GrailsASTUtils.hasOrInheritsProperty(classNode, GormProperties.VERSION);
if (!hasVersion) {
ClassNode parent = GrailsASTUtils.getFurthestUnresolvedParent(classNode);
parent.addProperty(GormProperties.VERSION, Modifier.PUBLIC, new ClassNode(Long.class), null, null, null);
}
}
use of org.codehaus.groovy.ast.ClassNode in project grails-core by grails.
the class TraitInjectionUtils method addTrait.
private static boolean addTrait(ClassNode classNode, Class trait) {
boolean traitsAdded = false;
boolean implementsTrait = false;
boolean traitNotLoaded = false;
ClassNode traitClassNode = ClassHelper.make(trait);
try {
implementsTrait = classNode.declaresInterface(traitClassNode);
} catch (Throwable e) {
// if we reach this point, the trait injector could not be loaded due to missing dependencies (for example missing servlet-api). This is ok, as we want to be able to compile against non-servlet environments.
traitNotLoaded = true;
}
if (!implementsTrait && !traitNotLoaded) {
final GenericsType[] genericsTypes = traitClassNode.getGenericsTypes();
final Map<String, ClassNode> parameterNameToParameterValue = new LinkedHashMap<String, ClassNode>();
if (genericsTypes != null) {
for (GenericsType gt : genericsTypes) {
parameterNameToParameterValue.put(gt.getName(), classNode);
}
}
classNode.addInterface(GrailsASTUtils.replaceGenericsPlaceholders(traitClassNode, parameterNameToParameterValue, classNode));
traitsAdded = true;
}
return traitsAdded;
}
Aggregations