本文整理汇总了Java中org.aikodi.chameleon.oo.statement.Block类的典型用法代码示例。如果您正苦于以下问题:Java Block类的具体用法?Java Block怎么用?Java Block使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Block类属于org.aikodi.chameleon.oo.statement包,在下文中一共展示了Block类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createDefaultConstructorWithoutAccessModifier
import org.aikodi.chameleon.oo.statement.Block; //导入依赖的package包/类
/**
* Create a default default constructor without an access modifier.
*/
protected JavaMethod createDefaultConstructorWithoutAccessModifier(boolean rebuildCache) {
// FIXME Because this code is ran when a regular Java type is constructed,
// we cannot ask the
// language for the factory. Management of the constructor should be done
// lazily. When
// the type is actually used, we can assume that a language is attached.
// Otherwise, we
// throw an exception.
JavaMethod cons = new JavaMethod(new SimpleNameMethodHeader(name(), new BasicJavaTypeReference(name())));
cons.addModifier(new Constructor());
Block body = new Block();
cons.setImplementation(new RegularImplementation(body));
body.addStatement(new StatementExpression(new SuperConstructorDelegation()));
cons.setUniParent(this);
setDefaultDefaultConstructor(cons);
return cons;
}
示例2: createDefaultConstructor
import org.aikodi.chameleon.oo.statement.Block; //导入依赖的package包/类
protected void createDefaultConstructor(Document javaDocument) {
javaDocument.apply(Type.class, t -> {
if (!isGenerated(t)) {
boolean hasConstructor = t.directlyDeclaredMembers().stream().anyMatch(m -> {
Declaration jloMember = (Declaration) m.origin();
return !isGenerated(m) && jloMember.isTrue(jlo(jloMember).CONSTRUCTOR);
} );
if (!hasConstructor) {
Method method = new JavaMethod(new SimpleNameMethodHeader("init$new", java(t).createTypeReference(t)));
method.addModifier(new Static());
method.addModifier(new Public());
t.add(method);
Block body = new Block();
method.setImplementation(new RegularImplementation(body));
body.addStatement(new ReturnStatement(new ConstructorInvocation(java(javaDocument).createTypeReference(implementationName(t)), null)));
}
}
} );
}
示例3: toCodeBlock
import org.aikodi.chameleon.oo.statement.Block; //导入依赖的package包/类
public String toCodeBlock(Block block) {
StringBuffer result = new StringBuffer();
result.append("{\n");
indent();
Iterator iter = block.statements().iterator();
while(iter.hasNext()) {
result.append(startLine());
result.append(toCode((Element)iter.next()));
result.append("\n");
}
undent();
result.append(startLine());
result.append("}");
return result.toString();
}
示例4: createMainInterface
import org.aikodi.chameleon.oo.statement.Block; //导入依赖的package包/类
protected void createMainInterface(Method javaMethod) {
Java7 java = java(javaMethod);
Type javaParentType = javaMethod.nearestAncestor(Type.class);
ObjectOrientedFactory factory = java.plugin(ObjectOrientedFactory.class);
Type javaMainType = factory.createRegularType(javaMethod.name());
javaMainType.addModifier(new Interface());
javaParentType.add(javaMainType);
Method javaMainMethod = factory.createNormalMethod("main", java.createTypeReference("void"));
javaMainType.add(javaMainMethod);
javaMainMethod.addModifier(new Public());
javaMainMethod.addModifier(new Static());
Block body = new Block();
javaMainMethod.setImplementation(new RegularImplementation(body));
javaMainMethod.header().addFormalParameter(new FormalParameter("args", new ArrayTypeReference(java.createTypeReference("String"))));
}
示例5: replaceExpressionImplementations
import org.aikodi.chameleon.oo.statement.Block; //导入依赖的package包/类
protected void replaceExpressionImplementations(Document javaDocument) {
javaDocument.apply(ExpressionImplementation.class, implementation -> {
Block body = new Block();
// We move the expression instead of cloning it
body.addStatement(new ReturnStatement(implementation.expression()));
RegularImplementation replacement = new RegularImplementation(body);
implementation.replaceWith(replacement);
} );
}
示例6: addFields
import org.aikodi.chameleon.oo.statement.Block; //导入依赖的package包/类
private void addFields(Type javaType, Type jloType) throws LookupException {
Set<Type> allSuperTypes = jloType.getSelfAndAllSuperTypesView();
List<RegularMemberVariable> collect = allSuperTypes.stream().<RegularMemberVariable>flatMap(x -> {
try {
return x.localMembers(RegularMemberVariable.class).stream();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ChameleonProgrammerException(e);
}
}).collect(Collectors.toList());
for(RegularMemberVariable v : collect) {
MemberVariableDeclarator jloMemberVariableDeclarator = v.nearestAncestor(MemberVariableDeclarator.class);
MemberVariableDeclarator f = new MemberVariableDeclarator(clone(jloMemberVariableDeclarator.typeReference()));
VariableDeclaration variableDeclaration = (VariableDeclaration) v.origin();
String fieldName = fieldName(variableDeclaration);
// Util.debug(fieldName.contains(IMPLEMENTATION_SUFFIX));
f.add(new VariableDeclaration(fieldName));
f.addModifier(new Private());
javaType.add(f);
Method getter = createGetterTemplate(jloMemberVariableDeclarator);
createGetterImplementation(fieldName, getter);
javaType.add(getter);
Method setter = createSetterTemplate(jloMemberVariableDeclarator);
setter.addModifier(new Public());
Block setterBody = new Block();
setter.setImplementation(new RegularImplementation(setterBody));
setterBody.addStatement(new StatementExpression(new AssignmentExpression(new NameExpression(fieldName), new NameExpression("value"))));
javaType.add(setter);
}
}
示例7: visitBlock
import org.aikodi.chameleon.oo.statement.Block; //导入依赖的package包/类
@Override
public Block visitBlock(BlockContext ctx) {
Block block = new Block();
for(StatementContext sctx: ctx.statement()) {
block.addStatement((Statement) visit(sctx));
}
return block;
}
示例8: analyze
import org.aikodi.chameleon.oo.statement.Block; //导入依赖的package包/类
@Override
protected void analyze(AssignmentExpression assignment) throws LookupException {
Verification result = Valid.create();
final Method method = assignment.nearestAncestor(Method.class);
if(method != null && method.isTrue(method.language(Java7.class).PUBLIC)) {
Variable v = assignment.variable();
if(v instanceof RegularMemberVariable) {
Expression e = assignment.getValue();
if(e instanceof CrossReference) {
final Declaration rhs = ((CrossReference) e).getElement();
if(rhs instanceof FormalParameter) {
Type booleanType = assignment.view(JavaView.class).primitiveType("boolean");
if(! ((FormalParameter) rhs).getType().sameAs(booleanType)) {
boolean notMentioned = true;
Statement stat = assignment.lexical().farthestAncestor(new UniversalPredicate<Statement,Nothing>(Statement.class) {
@Override
public boolean uncheckedEval(Statement s) {
// If s.parent() == the implementation object, then we have reached the
// block of the implementation, so we have to stop before that to object
// the child statement of the block of the implementation
return (! (s.parent() instanceof Implementation)) && s.nearestAncestor(Method.class) == method;
}
});
Block b = (Block) stat.parent();
List<Statement> befores = b.statementsBefore(stat);
for(Statement before : befores) {
List<CrossReference> crefs = before.descendants(CrossReference.class, cref -> cref.getElement().sameAs(rhs));
if(! crefs.isEmpty()) {
notMentioned = false;
}
}
if(notMentioned) {
result = new NonDefensiveFieldAssignmentResult((FormalParameter)rhs,v);
}
}
}
}
}
}
setResult(result().and(result));
}
示例9: isBlock
import org.aikodi.chameleon.oo.statement.Block; //导入依赖的package包/类
public boolean isBlock(Element element) {
return element instanceof Block;
}
示例10: testMethodCalls
import org.aikodi.chameleon.oo.statement.Block; //导入依赖的package包/类
public void testMethodCalls() throws LookupException, ProjectException {
Java7 language = (Java7)view().language();
Namespace ns = view().namespace();
DirectScanner scanner = new DirectScanner();
view().addSource(scanner);
Document document = new Document();
NamespaceDeclaration nsd = new JavaNamespaceDeclaration("test");
document.add(nsd);
RegularJavaType type = new RegularJavaType("Test");
nsd.add(type);
DirectDocumentLoader loader = new DirectDocumentLoader(scanner, document);
Type test = ns.find("test.Test", Type.class);
NormalMethod caller = new NormalMethod(new SimpleNameMethodHeader("m", new BasicJavaTypeReference("void")));
Block body = new Block();
caller.setImplementation(new RegularImplementation(body));
BasicJavaTypeReference listRef = new BasicJavaTypeReference("java.util.List");
listRef.addArgument(new PureWildcard());
LocalVariableDeclarator declarator = new LocalVariableDeclarator(listRef);
declarator.add(new JavaVariableDeclaration("first"));
declarator.add(new JavaVariableDeclaration("second"));
body.addStatement(declarator);
JavaMethodInvocation call = new JavaMethodInvocation("two", null);
call.addArgument(new JavaNameExpression("first"));
call.addArgument(new JavaNameExpression("second"));
body.addStatement(new StatementExpression(call));
type.add(caller);
SimpleNameMethodHeader calleeHeader = new SimpleNameMethodHeader("two", new BasicJavaTypeReference("void"));
calleeHeader.addTypeParameter(new FormalTypeParameter("T"));
BasicJavaTypeReference listTRef = new BasicJavaTypeReference("java.util.List");
listTRef.addArgument(new EqualityTypeArgument(new BasicJavaTypeReference("T")));
calleeHeader.addFormalParameter(new FormalParameter("first", listTRef.clone(listTRef)));
calleeHeader.addFormalParameter(new FormalParameter("second", listTRef.clone(listTRef)));
NormalMethod callee = new NormalMethod(calleeHeader);
callee.setImplementation(new RegularImplementation(new Block()));
type.add(callee);
try {
call.getElement();
assertTrue(false);
} catch(LookupException exc) {
}
}
示例11: createGetterImplementation
import org.aikodi.chameleon.oo.statement.Block; //导入依赖的package包/类
/**
* <p>Turn the given method into a getter for a field
* with the given name.</p>
*
* <ul>
* <li>The method gets a public modifier</li>
* <li>The implementation is a block with a return statement.</li>
* <li>The return statement returns the given field.</li>
* </ul>
*
* @param fieldName The name of the field that must be returned
* by the getter.
* @param getter The getter to be modified.
*/
private void createGetterImplementation(String fieldName, Method getter) {
getter.addModifier(new Public());
Block getterBody = new Block();
getter.setImplementation(new RegularImplementation(getterBody));
getterBody.addStatement(new ReturnStatement(new NameExpression(fieldName)));
}