List of usage examples for org.eclipse.jdt.core.dom Modifier STATIC
int STATIC
To view the source code for org.eclipse.jdt.core.dom Modifier STATIC.
Click Source Link
From source file:astview.Binding.java
License:Open Source License
private static StringBuffer getModifiersString(int flags, boolean isMethod) { StringBuffer sb = new StringBuffer().append("0x").append(Integer.toHexString(flags)).append(" ("); int prologLen = sb.length(); int rest = flags; rest &= ~appendFlag(sb, flags, Modifier.PUBLIC, "public "); rest &= ~appendFlag(sb, flags, Modifier.PRIVATE, "private "); rest &= ~appendFlag(sb, flags, Modifier.PROTECTED, "protected "); rest &= ~appendFlag(sb, flags, Modifier.STATIC, "static "); rest &= ~appendFlag(sb, flags, Modifier.FINAL, "final "); if (isMethod) { rest &= ~appendFlag(sb, flags, Modifier.SYNCHRONIZED, "synchronized "); rest &= ~appendFlag(sb, flags, Modifier.DEFAULT, "default "); } else {//from ww w . j av a2s. c o m rest &= ~appendFlag(sb, flags, Modifier.VOLATILE, "volatile "); rest &= ~appendFlag(sb, flags, Modifier.TRANSIENT, "transient "); } rest &= ~appendFlag(sb, flags, Modifier.NATIVE, "native "); rest &= ~appendFlag(sb, flags, Modifier.ABSTRACT, "abstract "); rest &= ~appendFlag(sb, flags, Modifier.STRICTFP, "strictfp "); if (rest != 0) sb.append("unknown:0x").append(Integer.toHexString(rest)).append(" "); int len = sb.length(); if (len != prologLen) sb.setLength(len - 1); sb.append(")"); return sb; }
From source file:cn.ieclipse.adt.ext.jdt.SourceGenerator.java
License:Apache License
private static void merge(CompilationUnit unit, String pkgName, String typeName, String auth, String dbName, List<String> tableCreators) { unit.recordModifications();/* w ww .j a v a 2 s.c o m*/ AST ast = unit.getAST(); TypeDeclaration type = (TypeDeclaration) unit.types().get(0); ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName("cn.ieclipse.aorm.Session")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.content.UriMatcher")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.database.sqlite.SQLiteDatabase")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.database.sqlite.SQLiteOpenHelper")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("java.net.Uri")); unit.imports().add(id); id = ast.newImportDeclaration(); id.setName(ast.newName("android.content.ContentValue")); unit.imports().add(id); VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("AUTH")); StringLiteral sl = ast.newStringLiteral(); sl.setLiteralValue(auth); vdf.setInitializer(sl); FieldDeclaration fd = ast.newFieldDeclaration(vdf); fd.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL))); fd.setType(ast.newSimpleType(ast.newSimpleName("String"))); int i = 0; type.bodyDeclarations().add(i++, fd); // URI = Uri.parse("content://" + AUTH); vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("URI")); MethodInvocation mi = ast.newMethodInvocation(); mi.setExpression(ast.newSimpleName("Uri")); mi.setName(ast.newSimpleName("parse")); InfixExpression fix = ast.newInfixExpression(); fix.setOperator(InfixExpression.Operator.PLUS); sl = ast.newStringLiteral(); sl.setLiteralValue("content://"); fix.setLeftOperand(sl); fix.setRightOperand(ast.newSimpleName("AUTH")); mi.arguments().add(fix); vdf.setInitializer(mi); fd = ast.newFieldDeclaration(vdf); fd.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL))); fd.setType(ast.newSimpleType(ast.newSimpleName("Uri"))); type.bodyDeclarations().add(i++, fd); // private mOpenHelper; vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("mOpenHelper")); fd = ast.newFieldDeclaration(vdf); fd.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD)); fd.setType(ast.newSimpleType(ast.newName("SQLiteOpenHelper"))); type.bodyDeclarations().add(i++, fd); // private static session; vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("session")); fd = ast.newFieldDeclaration(vdf); fd.modifiers().addAll(ast.newModifiers((Modifier.PRIVATE | Modifier.STATIC))); fd.setType(ast.newSimpleType(ast.newName("Session"))); type.bodyDeclarations().add(i++, fd); // public static Session getSession(){ // return session; // } MethodDeclaration md = ast.newMethodDeclaration(); md.modifiers().addAll(ast.newModifiers((Modifier.PUBLIC | Modifier.STATIC))); md.setReturnType2(ast.newSimpleType(ast.newName("Session"))); md.setName(ast.newSimpleName("getSession")); Block methodBlock = ast.newBlock(); ReturnStatement returnStmt = ast.newReturnStatement(); returnStmt.setExpression(ast.newSimpleName("session")); methodBlock.statements().add(returnStmt); md.setBody(methodBlock); type.bodyDeclarations().add(i, md); // modify onCreate rewriteOnCreate(unit, dbName, tableCreators); }
From source file:com.alex.example.fixlicense.actions.SampleAction.java
License:Open Source License
private FieldDeclaration createLiceseInLineField(AST ast) { VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName("COPYRIGHT")); StringLiteral sl = ast.newStringLiteral(); sl.setLiteralValue(license_inline);//from w ww. j ava 2 s . c o m vdf.setInitializer(sl); FieldDeclaration fd = ast.newFieldDeclaration(vdf); fd.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL)); fd.setType(ast.newSimpleType(ast.newSimpleName("String"))); return fd; }
From source file:com.google.devtools.j2cpp.gen.CppImplementationGenerator.java
License:Open Source License
private void printProperties(FieldDeclaration[] fields) { int nPrinted = 0; for (FieldDeclaration field : fields) { if ((field.getModifiers() & Modifier.STATIC) == 0) { @SuppressWarnings("unchecked") List<VariableDeclarationFragment> vars = field.fragments(); // safe by definition for (VariableDeclarationFragment var : vars) { if (var.getName().getIdentifier().startsWith("this$") && superDefinesVariable(var)) { // Don't print, as it shadows an inner field in a super class. continue; }//from www. j a va2 s . c o m String name = NameTable.getName(var.getName()); ITypeBinding type = Types.getTypeBinding(field.getType()); String typeString = NameTable.javaRefToCpp(type); if (!typeString.endsWith("*")) { typeString += " "; } // Don't emit the getter when there is already a method with the // same name. // TODO(user,user): Update when getters are merged with property // accessors (see issues). boolean noGetter = false; ITypeBinding declaringClass = Types.getTypeBinding(field.getParent()); if (declaringClass != null) { IMethodBinding[] methods = declaringClass.getDeclaredMethods(); for (IMethodBinding method : methods) { if (method.getName().equals(name) && method.getParameterTypes().length == 0) { noGetter = true; break; } } } String objCFieldName = NameTable.javaFieldToCpp(name); // Getter if (!noGetter) { printf(String.format("- (%s)%s {\n return %s;\n}\n\n", typeString.trim(), name, objCFieldName)); } // Setter printf(String.format("- (void)set%s:(%s)new%s {\n", NameTable.capitalize(name), typeString.trim(), NameTable.capitalize(name))); if (type.isPrimitive()) { printf(String.format(" %s = new%s;\n}\n\n", objCFieldName, NameTable.capitalize(name))); } else if (Options.useReferenceCounting() && !Types.isWeakReference(Types.getVariableBinding(var))) { String retentionMethod = type.isEqualTo(Types.getNSString()) ? "copy" : "retain"; printf(String.format(" [%s autorelease];\n %s = [new%s %s];\n}\n\n", objCFieldName, objCFieldName, NameTable.capitalize(name), retentionMethod)); } else { printf(String.format(" %s = new%s;\n}\n\n", objCFieldName, NameTable.capitalize(name))); } nPrinted++; } } } if (nPrinted > 0) { newline(); } }
From source file:com.google.devtools.j2cpp.translate.AnonymousClassConverter.java
License:Open Source License
/** * Convert the anonymous class into an inner class. Fields are added for * final variables that are referenced, and a constructor is added. * * Note: endVisit is used for a depth-first traversal, to make it easier * to scan their containing nodes for references. *//* w w w . j ava 2 s. c o m*/ @Override @SuppressWarnings("unchecked") public void endVisit(AnonymousClassDeclaration node) { ASTNode parent = node.getParent(); ClassInstanceCreation newInvocation = null; EnumConstantDeclaration enumConstant = null; List<Expression> parentArguments; String newClassName; ITypeBinding innerType; boolean isStatic = staticParent(node); int modifiers = isStatic ? Modifier.STATIC : 0; if (parent instanceof ClassInstanceCreation) { newInvocation = (ClassInstanceCreation) parent; parentArguments = newInvocation.arguments(); innerType = Types.getTypeBinding(newInvocation); newClassName = innerType.getName(); innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType, modifiers); } else if (parent instanceof EnumConstantDeclaration) { enumConstant = (EnumConstantDeclaration) parent; parentArguments = enumConstant.arguments(); innerType = Types.getTypeBinding(node); newClassName = Types.getTypeBinding(node).getName(); innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType, modifiers); } else { throw new AssertionError("unknown anonymous class declaration parent: " + parent.getClass().getName()); } // Create a type declaration for this anonymous class. AST ast = node.getAST(); TypeDeclaration typeDecl = ast.newTypeDeclaration(); if (isStatic) { typeDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD)); } Types.addBinding(typeDecl, innerType); typeDecl.setName(ast.newSimpleName(newClassName)); Types.addBinding(typeDecl.getName(), innerType); typeDecl.setSourceRange(node.getStartPosition(), node.getLength()); Type superType = Types.makeType(Types.mapType(innerType.getSuperclass())); typeDecl.setSuperclassType(superType); for (ITypeBinding interfaceType : innerType.getInterfaces()) { typeDecl.superInterfaceTypes().add(Types.makeType(Types.mapType(interfaceType))); } for (Object bodyDecl : node.bodyDeclarations()) { BodyDeclaration decl = (BodyDeclaration) bodyDecl; typeDecl.bodyDeclarations().add(NodeCopier.copySubtree(ast, decl)); } typeDecl.accept(new InitializationNormalizer()); // Fix up references to external types, if necessary. Set<IVariableBinding> methodVars = getMethodVars(node); final List<ReferenceDescription> references = findReferences(node, methodVars); final List<Expression> invocationArgs = parentArguments; if (!references.isEmpty() || !invocationArgs.isEmpty()) { // is there anything to fix-up? List<IVariableBinding> innerVars = getInnerVars(references); if (!innerVars.isEmpty() || !invocationArgs.isEmpty()) { GeneratedMethodBinding defaultConstructor = addInnerVars(typeDecl, innerVars, references, parentArguments); Types.addBinding(parent, defaultConstructor); for (IVariableBinding var : innerVars) { if (!isConstant(var)) { parentArguments.add(makeFieldRef(var, ast)); } } assert defaultConstructor.getParameterTypes().length == parentArguments.size(); typeDecl.accept(new ASTVisitor() { @Override public void endVisit(SimpleName node) { IVariableBinding var = Types.getVariableBinding(node); if (var != null) { for (ReferenceDescription ref : references) { if (var.isEqualTo(ref.binding)) { if (ref.innerField != null) { setProperty(node, makeFieldRef(ref.innerField, node.getAST())); } else { // In-line constant. Object o = var.getConstantValue(); assert o != null; Expression literal = makeLiteral(o, var.getType().getQualifiedName(), node.getAST()); setProperty(node, literal); } return; } } } } }); } } // If invocation, replace anonymous class invocation with the new constructor. if (newInvocation != null) { newInvocation.setAnonymousClassDeclaration(null); newInvocation.setType(Types.makeType(innerType)); IMethodBinding oldBinding = Types.getMethodBinding(newInvocation); if (oldBinding == null) { oldBinding = newInvocation.resolveConstructorBinding(); } if (oldBinding != null) { GeneratedMethodBinding invocationBinding = new GeneratedMethodBinding(oldBinding); invocationBinding.setDeclaringClass(innerType); Types.addBinding(newInvocation, invocationBinding); } } else { enumConstant.setAnonymousClassDeclaration(null); } // Add type declaration to enclosing type. ITypeBinding outerType = innerType.getDeclaringClass(); if (outerType.isAnonymous()) { // Get outerType node. ASTNode n = parent.getParent(); while (!(n instanceof AnonymousClassDeclaration) && !(n instanceof TypeDeclaration)) { n = n.getParent(); } if (n instanceof AnonymousClassDeclaration) { AnonymousClassDeclaration outerDecl = (AnonymousClassDeclaration) n; outerDecl.bodyDeclarations().add(typeDecl); } } else { AbstractTypeDeclaration outerDecl = (AbstractTypeDeclaration) unit.findDeclaringNode(outerType); outerDecl.bodyDeclarations().add(typeDecl); } Symbols.scanAST(typeDecl); super.endVisit(node); }
From source file:com.google.devtools.j2cpp.translate.ClassConverter.java
License:Open Source License
protected void addInnerParameters(MethodDeclaration constructor, GeneratedMethodBinding binding, List<IVariableBinding> innerFields, AST ast, boolean methodVars) { // Add parameters and initializers for each field. @SuppressWarnings("unchecked") // safe by definition List<SingleVariableDeclaration> parameters = constructor.parameters(); List<SingleVariableDeclaration> newParams = createConstructorArguments(innerFields, Types.getMethodBinding(constructor), ast, "outer$"); Block body = constructor.getBody();//w w w. j ava 2 s .c o m @SuppressWarnings("unchecked") // safe by definition List<Statement> statements = body.statements(); Statement first = statements.size() > 0 ? statements.get(0) : null; int offset = first != null && (first instanceof SuperConstructorInvocation || first instanceof ConstructorInvocation) ? 1 : 0; boolean firstIsThisCall = first != null && first instanceof ConstructorInvocation; // If superclass constructor takes an outer$ parameter, create or edit // an invocation for it first if (!innerFields.isEmpty() && !methodVars) { if (firstIsThisCall) { ConstructorInvocation thisCall = (ConstructorInvocation) first; IMethodBinding cons = Types.getMethodBinding(thisCall); GeneratedMethodBinding newCons = new GeneratedMethodBinding(cons.getMethodDeclaration()); // Create a new this invocation to the updated constructor. @SuppressWarnings("unchecked") List<Expression> args = ((ConstructorInvocation) first).arguments(); int index = 0; for (SingleVariableDeclaration param : newParams) { IVariableBinding paramBinding = Types.getVariableBinding(param); newCons.addParameter(index, Types.getTypeBinding(param)); args.add(index++, makeFieldRef(paramBinding, ast)); } Types.addBinding(thisCall, newCons); } else { ITypeBinding superType = binding.getDeclaringClass().getSuperclass().getTypeDeclaration(); if ((superType.getDeclaringClass() != null || superType.getDeclaringMethod() != null) && (superType.getModifiers() & Modifier.STATIC) == 0) { // There may be more than one outer var supplied, find the right one. IVariableBinding outerVar = null; for (SingleVariableDeclaration param : newParams) { IVariableBinding paramBinding = Types.getVariableBinding(param); if (paramBinding.getType().isAssignmentCompatible(superType.getDeclaringClass())) { outerVar = paramBinding; } } assert outerVar != null; IMethodBinding cons = null; if (offset > 0) { cons = Types.getMethodBinding(statements.get(0)); } else { for (IMethodBinding method : superType.getDeclaredMethods()) { // The super class's constructor may or may not have been already // modified. if (method.isConstructor()) { if (method.getParameterTypes().length == 0) { cons = method; break; } else if (method.getParameterTypes().length == 1 && outerVar.getType().isAssignmentCompatible(method.getParameterTypes()[0]) && method instanceof GeneratedMethodBinding) { cons = method; break; } } } } assert cons != null; if (!updatedConstructors.contains(cons)) { GeneratedMethodBinding newSuperCons = new GeneratedMethodBinding( cons.getMethodDeclaration()); newSuperCons.addParameter(0, superType.getDeclaringClass()); cons = newSuperCons; } SimpleName outerRef = makeFieldRef(outerVar, ast); SuperConstructorInvocation superInvocation = offset == 0 ? ast.newSuperConstructorInvocation() : (SuperConstructorInvocation) statements.get(0); @SuppressWarnings("unchecked") List<Expression> args = superInvocation.arguments(); // safe by definition args.add(0, outerRef); if (offset == 0) { statements.add(0, superInvocation); offset = 1; } Types.addBinding(superInvocation, cons); } } } for (int i = 0; i < newParams.size(); i++) { SingleVariableDeclaration parameter = newParams.get(i); // Only add an assignment statement for fields. if (innerFields.get(i).isField()) { statements.add(i + offset, createAssignment(innerFields.get(i), Types.getVariableBinding(parameter), ast)); } // Add methodVars at the end of the method invocation. if (methodVars) { parameters.add(parameter); binding.addParameter(Types.getVariableBinding(parameter)); } else { parameters.add(i, parameter); binding.addParameter(i, Types.getVariableBinding(parameter)); } } Symbols.scanAST(constructor); updatedConstructors.add(binding); assert constructor.parameters().size() == binding.getParameterTypes().length; }
From source file:com.google.devtools.j2cpp.translate.DestructorGenerator.java
License:Open Source License
private boolean isStatic(FieldDeclaration f) { return (f.getModifiers() & Modifier.STATIC) != 0; }
From source file:com.google.devtools.j2cpp.translate.InitializationNormalizer.java
License:Open Source License
void addClassInitializer(ITypeBinding type, List<BodyDeclaration> members, List<Statement> classInitStatements, AST ast) {/*from w w w . ja v a 2 s . co m*/ int modifiers = Modifier.PUBLIC | Modifier.STATIC; addMethod(NameTable.CLINIT_NAME, modifiers, type, classInitStatements, members, ast, false); }
From source file:com.google.devtools.j2cpp.translate.Rewriter.java
License:Open Source License
/** * Convert an array initializer into a init method on the equivalent * IOSArray. This init method takes a C array and count, like * NSArray.arrayWithObjects:count:. For example, "int[] a = { 1, 2, 3 };" * translates to "[IOSIntArray initWithInts:(int[]){ 1, 2, 3 } count:3];". *//*from www . j a v a 2 s. co m*/ private MethodInvocation createIOSArrayInitializer(ITypeBinding arrayType, ArrayInitializer arrayInit) { AST ast = arrayInit.getAST(); int dimensions = arrayType.getDimensions(); ITypeBinding componentType; IOSArrayTypeBinding iosArrayBinding; if (dimensions > 2) { // This gets resolved into IOSObjectArray, for an array of arrays. componentType = iosArrayBinding = Types.resolveArrayType(arrayType); } else if (dimensions == 2) { // Creates a single-dimension array type. componentType = Types.resolveArrayType(arrayType.getElementType()); iosArrayBinding = Types.resolveArrayType(componentType); } else { componentType = Types.getTypeBinding(arrayInit).getComponentType(); iosArrayBinding = Types.resolveArrayType(componentType); } // Create IOS message. MethodInvocation message = ast.newMethodInvocation(); SimpleName receiver = ast.newSimpleName(iosArrayBinding.getName()); Types.addBinding(receiver, iosArrayBinding); message.setExpression(receiver); String methodName = iosArrayBinding.getInitMethod(); SimpleName messageName = ast.newSimpleName(methodName); GeneratedMethodBinding methodBinding = new GeneratedMethodBinding(methodName, Modifier.PUBLIC | Modifier.STATIC, iosArrayBinding, iosArrayBinding, false, false, true); Types.addBinding(messageName, methodBinding); message.setName(messageName); Types.addBinding(message, methodBinding); // Pass array initializer as C-style array to message. @SuppressWarnings("unchecked") List<Expression> args = message.arguments(); // safe by definition ArrayInitializer newArrayInit = NodeCopier.copySubtree(ast, arrayInit); @SuppressWarnings("unchecked") List<Expression> exprs = newArrayInit.expressions(); for (int i = 0; i < exprs.size(); i++) { // Convert any elements that are also array initializers. Expression expr = exprs.get(i); if (expr instanceof ArrayInitializer) { exprs.set(i, createIOSArrayInitializer(componentType, (ArrayInitializer) expr)); } } args.add(newArrayInit); GeneratedVariableBinding argBinding = new GeneratedVariableBinding(arrayType, false, true, null, methodBinding); methodBinding.addParameter(argBinding); NumberLiteral arraySize = ast.newNumberLiteral(Integer.toString(arrayInit.expressions().size())); Types.addBinding(arraySize, ast.resolveWellKnownType("int")); args.add(arraySize); argBinding = new GeneratedVariableBinding(ast.resolveWellKnownType("int"), false, true, null, methodBinding); methodBinding.addParameter(argBinding); // Specify type for object arrays. if (iosArrayBinding.getName().equals("IOSObjectArray")) { TypeLiteral typeLiteral = ast.newTypeLiteral(); typeLiteral.setType(Types.makeType(componentType)); Types.addBinding(typeLiteral, Types.getIOSClass()); args.add(typeLiteral); argBinding = new GeneratedVariableBinding("type", 0, Types.getIOSClass(), false, true, null, methodBinding); methodBinding.addParameter(argBinding); } return message; }
From source file:com.google.devtools.j2cpp.types.Types.java
License:Open Source License
public static boolean isStaticVariable(IVariableBinding binding) { return (binding.getModifiers() & Modifier.STATIC) > 0; }